def create_cat(self):
inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0))
self.cat_body = pymunk.Body(1, inertia)
self.cat_body.position = 50, height - 100
self.cat_shape = pymunk.Circle(self.cat_body, 30)
self.cat_shape.color = THECOLORS["orange"]
self.cat_shape.elasticity = 1.0
self.cat_shape.angle = 0.5
direction = Vec2d(1, 0).rotated(self.cat_body.angle)
self.space.add(self.cat_body, self.cat_shape)
python类Color()的实例源码
def create_car(self, x, y, r):
inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0))
self.car_body = pymunk.Body(1, inertia)
self.car_body.position = x, y
self.car_shape = pymunk.Circle(self.car_body, 25)
self.car_shape.color = THECOLORS["green"]
self.car_shape.elasticity = 1.0
self.car_body.angle = r
driving_direction = Vec2d(1, 0).rotated(self.car_body.angle)
self.car_body.apply_impulse(driving_direction)
self.space.add(self.car_body, self.car_shape)
def _clear(self):
self.screen.fill(color["black"])
def __init__(self, pong, position):
self.pong = pong
self.area = pong.res
if position == 'left':
self.rect = pymunk.Segment(pong.space.static_body,
(0, self.area[1] / 2 + 3*scale),
(0, self.area[1] / 2 - 3*scale), 1.0)
else:
self.rect = pymunk.Segment(pong.space.static_body,
(self.area[0] - 2, self.area[1] / 2 + 3*scale),
(self.area[0] - 2, self.area[1] / 2 - 3*scale), 1.0)
self.speed = 2*scale
self.rect.elasticity = .99
self.rect.color = color["white"]
self.rect.collision_type = 1
def create_ball(self, radius=3):
inertia = pymunk.moment_for_circle(1, 0, radius, (0, 0))
body = pymunk.Body(1, inertia)
position = np.array(self.initial_position) + self.initial_std * np.random.normal(size=(2,))
position = np.clip(position, self.dd + radius + 1, self.res[0] - self.dd - radius - 1)
body.position = position
shape = pymunk.Circle(body, radius, (0, 0))
shape.elasticity = .9
shape.color = color["white"]
return shape
def create_obstacle(self, x, y, r):
c_body = pymunk.Body(pymunk.inf, pymunk.inf)
c_shape = pymunk.Circle(c_body, r)
c_shape.elasticity = 1.0
c_body.position = x, y
c_shape.color = THECOLORS["blue"]
self.space.add(c_body, c_shape)
return c_body
def create_cat(self):
inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0))
self.cat_body = pymunk.Body(1, inertia)
self.cat_body.position = 50, height - 100
self.cat_shape = pymunk.Circle(self.cat_body, 30)
self.cat_shape.color = THECOLORS["orange"]
self.cat_shape.elasticity = 1.0
self.cat_shape.angle = 0.5
direction = Vec2d(1, 0).rotated(self.cat_body.angle)
self.space.add(self.cat_body, self.cat_shape)
def create_car(self, x, y, r):
inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0))
self.car_body = pymunk.Body(1, inertia)
self.car_body.position = x, y
self.car_shape = pymunk.Circle(self.car_body, 25)
self.car_shape.color = THECOLORS["green"]
self.car_shape.elasticity = 1.0
self.car_body.angle = r
driving_direction = Vec2d(1, 0).rotated(self.car_body.angle)
self.car_body.apply_impulse(driving_direction)
self.space.add(self.car_body, self.car_shape)
def __init__(self, game):
# Global-ish.
self.game = game
self.crashed = False
# Physics stuff.
self.space = pymunk.Space()
self.space.gravity = pymunk.Vec2d(0., 0.)
# Create the car.
self.create_car(100, 100, 0.5)
# Record steps.
self.num_steps = 0
# Create walls.
height = self.game.height
width = self.game.width
thick = 1
static = [
pymunk.Segment(
self.space.static_body,
(0, thick), (0, height), thick),
pymunk.Segment(
self.space.static_body,
(thick, height), (width, height), thick),
pymunk.Segment(
self.space.static_body,
(width-thick, height), (width-thick, thick), thick),
pymunk.Segment(
self.space.static_body,
(thick, thick), (width, thick), thick)
]
for s in static:
s.friction = 1.
s.group = 1
s.collision_type = 1
s.color = THECOLORS['red']
self.space.add(static)
# Create some obstacles, semi-randomly.
# We'll create three and they'll move around to prevent over-fitting.
self.obstacles = []
self.obstacles.append(self.create_obstacle(200, 350, 100))
self.obstacles.append(self.create_obstacle(700, 200, 125))
self.obstacles.append(self.create_obstacle(600, 600, 35))
# Create a cat and food and hud
self.create_cat()
self.hud = "HUD"
self.show_hud()
def get_reward(self, done):
base_reward = 200
my_color = self.color
my_angle = self.car_body.angle
my_x, my_y = self.car_body.position
goalx = self.redGoal[0][0]
ySep = int((self.redGoal[0][1]+self.greenGoal[0][1])/2.)
# ySep = int((self.redGoal[0][1]+self.greenGoal[0][1])/2.)
if done:
if my_color == 0:
if my_y > ySep:
reward = base_reward*6
else:
reward = base_reward*-6
else:
if my_y < ySep:
reward = base_reward*6
else:
reward = base_reward*-6
return reward
else:
if self.car_is_crashed():
reward = -3*base_reward
return reward
if my_x >= goalx-self.car_radius:
if my_color == 0:
angle1 = self._get_angle(self.eRedGoal[0])
angle2 = self._get_angle(self.eRedGoal[1])
reward = 2*base_reward*(self.direct/(self.vmax))*max(math.cos(my_angle-angle1), math.cos(my_angle-angle2))
else:
angle1 = self._get_angle(self.eGreenGoal[0])
angle2 = self._get_angle(self.eGreenGoal[1])
reward = 2*base_reward*(self.direct/(self.vmax))*max(math.cos(my_angle-angle1), math.cos(my_angle-angle2))
return reward
if my_color == 0:
angle1 = self._get_angle(self.redGoal[0])
angle2 = self._get_angle(self.redGoal[1])
reward = (1./2)*base_reward*(self.direct/(self.vmax))*max(math.cos(my_angle-angle1), math.cos(my_angle-angle2))
else:
angle1 = self._get_angle(self.greenGoal[0])
angle2 = self._get_angle(self.greenGoal[1])
reward = (1./2)*base_reward*(self.direct/(self.vmax))*max(math.cos(my_angle-angle1), math.cos(my_angle-angle2))
return reward
# def _euclidean_dist(self, target):
# x_ , y_ = self.car_body.position
# x = np.array([x_, y_])
# t = np.array(target)
# dist = np.sqrt(np.sum((t-x)**2))
# return dist