Home > other >  Pyame mobile block collision problem
Pyame mobile block collision problem

Time:09-16

The import math
The import sys
The import numpy as np
The import pygame


Class Maze (object) :
State_dim=4
Action_dim=1
Action_bound=[0, 2 * math. PI]

Def __init__ (self) :
Pygame. The init ()
The self. The screen=pygame. Display. Set_mode ((600, 500))
# self. A=np. Random. Random_sample () * (2 * math. PI) # action range [0, 2 PI]
The self. The env=pygame. The draw. The rect (self) screen, (0, 0, 0), (45, 30, 500, 450), 2)
# initialization agent information
Self. Agent_pos_x=48
Self. Agent_pos_y=34
Self. V=0.7
Self. Vel_x=0
Self. Vel_y=0
Self. Agent_pos=self. Agent_pos_x, self agent_pos_y, 50, 45

# start point
Self. Start_pos_x=48
Self. Start_pos_y=34
Self. Start_pos=self. Start_pos_x, self start_pos_y, 50, 45

# hell location information
Self. Hell_pos_x=300 # hell initial position
Self. Hell_pos_y=250
Self. Vel_xh=0 #. 1 * np. Random. Random_sample () # make hell move
Self. Vel_yh=0 #. 1 * np. Random. Random_sample ()
# hell point
Self. Hell_pos_x +=self. Vel_xh
Self. Hell_pos_y +=self. Vel_yh
Self. Hell_pos=self. Hell_pos_x, self hell_pos_y, 50, 45

If self. Hell_pos_x & gt; 490 or self. Hell_pos_x & lt; 48:
Self. Vel_xh=- self. Vel_xh
If self. Hell_pos_y & gt; 430 or self. Hell_pos_y & lt; 34:
Self. Vel_yh=- self. Vel_yh
# end point
Self. End_pos_x=490
Self. End_pos_y=430
Self. End_pos=self. End_pos_x, self end_pos_y, 50, 45
# self. End=pygame. The draw. The rect (self) screen, (255, 0, 0), the self. The end_pos, 0)

# pygame. Display. Set_caption (" Simple Moving ")
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Def step (self, action) :
The done=False
# action=np. Clip (action, a_min=0, a_max math.=2 * PI)
Action=np. Clip (action, * self. Action_bound)
Self. Vel_x=self. V * math. Cos (action)
Self. Vel_y=self. V * math.h sin (action)
Self. Agent_pos_x=self. Agent_pos_x + self. Vel_x
Self. Agent_pos_y=self. Agent_pos_y + self. Vel_y

Dist1=[(self. End_pos_x - self. Agent_pos_x), (self. End_pos_y - self. Agent_pos_y)]

If self. Agent. Colliderect (self. Hell) : # agent into the obstructions whether
Reward=1
Print (reward)
Print (" misson failed ")
The done=True
Elif self. Agent. Colliderect self. (end) :
Reward=1
Print (" victory ")
The done=True
The else:
Reward=0
The done=False
S=np. Concatenate (([self agent_pos_x, self. Agent_pos_y], dist1))
If done:
Pygame. Quit ()
Sys. The exit ()
Return s, reward, done

Def outside (self) :
If self. Agent_pos_y & gt; 490 or self. Agent_pos_y & lt; 48: # touching the border rebound
Self. Vel_x=- self. Vel_x
If self. Agent_pos_y & gt; 430 or self. Agent_pos_y & lt; 34:
Self. Vel_y=- self. Vel_y

Def reset (self) :
Self. Agent_pos_x=48
Self. Agent_pos_y=34
Self. Vel_x=0
Self. Vel_y=0
Self. Agent_pos=self. Agent_pos_x, self agent_pos_y, 50, 45
The self. The agent=pygame. The draw. The rect (self) screen, (255, 255, 0), (self. Agent_pos_x, self agent_pos_y, 50, 45), 0)
Dist1=[(self. End_pos_x - self. Agent_pos_x), (self. End_pos_y - self. Agent_pos_y)]
S=np. Concatenate (([self agent_pos_x, self. Agent_pos_y], dist1))
Return s

Def render (self) :
The self. The screen=pygame. Display. Set_mode ((600, 500))
The self. The screen. The fill ((255, 255, 255))
Pygame. The draw. The rect (self. Screen, (0, 0, 0), (45, 30, 500, 450), 2)
Pygame. The draw. The rect (self. Screen, (0, 0, 0), the self. The start_pos, 2)
Self. Hell=pygame. The draw. The rect (self) screen, (0, 0, 0), the self. The hell_pos, 0)
The self. The end=pygame. The draw. The rect (self) screen, (255, 0, 0), the self. End_pos, 0)
The self. The agent=pygame. The draw. The rect (self) screen, (255, 255, 0), (self. Agent_pos_x, self agent_pos_y, 50, 45), 0)
Pygame. Display. Flip ()

Def sample_action (self) :
Return np. Random. Random_sample () * (2 * math. PI) # action range [0, 2 PI]



If __name__=="__main__ ':
Env=Maze ()
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
While True:
For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
Pygame. Quit ()
The exit (0)
Env. Render ()
Env. Outside ()
Env. Step (env. Sample_action ())

# the run ()
Why my mobile square will be removed I set the borders, not have defined the outside, to solve
  • Related