def init_striker(space, x, passthrough, action, player):
inertia = pymunk.moment_for_circle(STRIKER_MASS, 0, STRIKER_RADIUS, (0, 0))
body = pymunk.Body(STRIKER_MASS, inertia)
if player == 1:
body.position = (action[0], 145)
if player == 2:
body.position = (action[0], BOARD_SIZE - 136)
body.apply_force_at_world_point((cos(action[1]) * action[2], sin(
action[1]) * action[2]), body.position + (STRIKER_RADIUS * 0, STRIKER_RADIUS * 0))
shape = pymunk.Circle(body, STRIKER_RADIUS, (0, 0))
shape.elasticity = STRIKER_ELASTICITY
shape.color = STRIKER_COLOR
mask = pymunk.ShapeFilter.ALL_MASKS ^ passthrough.filter.categories
sf = pymunk.ShapeFilter(mask=mask)
shape.filter = sf
shape.collision_type = 2
space.add(body, shape)
return [body, shape]
# Adds coins to the board at the given coordinates
python类Color()的实例源码
def init_striker(space, x, passthrough, action, player):
inertia = pymunk.moment_for_circle(STRIKER_MASS, 0, STRIKER_RADIUS, (0, 0))
body = pymunk.Body(STRIKER_MASS, inertia)
if player == 1:
body.position = (action[0], 140)
if player == 2:
body.position = (action[0], BOARD_SIZE - 140)
body.apply_force_at_world_point((cos(action[1]) * action[2], sin(
action[1]) * action[2]), body.position + (STRIKER_RADIUS * 0, STRIKER_RADIUS * 0))
shape = pymunk.Circle(body, STRIKER_RADIUS, (0, 0))
shape.elasticity = STRIKER_ELASTICITY
shape.color = STRIKER_COLOR
mask = pymunk.ShapeFilter.ALL_MASKS ^ passthrough.filter.categories
sf = pymunk.ShapeFilter(mask=mask)
shape.filter = sf
shape.collision_type = 2
space.add(body, shape)
return [body, shape]
# Adds coins to the board at the given coordinates
def init_pockets(space):
pockets = []
for i in [(44.1, 43.1), (756.5, 43), (756.5, 756.5), (44, 756.5)]:
inertia = pymunk.moment_for_circle(0.1, 0, POCKET_RADIUS, (0, 0))
body = pymunk.Body(0.1, inertia)
body.position = i
shape = pymunk.Circle(body, POCKET_RADIUS, (0, 0))
shape.color = POCKET_COLOR
shape.collision_type = 2
shape.filter = pymunk.ShapeFilter(categories=0b1000)
space.add(body, shape)
pockets.append(shape)
del body
del shape
return pockets
# Initialize striker with force
def add_walls(self):
self.static_lines = []
# Add floor
self.static_lines.append(pymunk.Segment(self.space.static_body,
(0, 1),
(self.res[1], 1), .0))
# Add roof
self.static_lines.append(pymunk.Segment(self.space.static_body,
(0, self.res[1]),
(self.res[1], self.res[1]), .0))
# Set properties
for line in self.static_lines:
line.elasticity = .99
line.color = color["white"]
self.space.add(self.static_lines)
return True
def create_obstacle(self, x, y, r):
# Create a body in PyMunk to represent a big, heavy obstacle
c_body = pymunk.Body(1000000, 1000000) # was pymunk.inf
c_shape = pymunk.Circle(c_body, r*self.game.scale)
c_shape.elasticity = 1.0
c_body.position = x*self.game.scale, y*self.game.scale
c_shape.color = THECOLORS["blue"]
self.space.add(c_body, c_shape)
return c_body
def create_cat(self):
# Create a lighter body in PyMunk to represent a fast moving cat
inertia = pymunk.moment_for_circle(1, 0, 14*self.game.scale, (0, 0))
self.cat_body = pymunk.Body(1, inertia)
self.cat_body.position = 50*self.game.scale, self.game.height - 100*self.game.scale
self.cat_shape = pymunk.Circle(self.cat_body, 30*self.game.scale)
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):
# moment has mass, inner_radius, outer_radius, offset=(0, 0))
inertia = pymunk.moment_for_circle(1, 0, 14*self.game.scale, (0, 0))
# mass, moment
self.car_body = pymunk.Body(1, inertia)
self.car_body.position = x*self.game.scale, y*self.game.scale
# body, radius
self.car_shape = pymunk.Circle(self.car_body, 25*self.game.scale)
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_at_local_point(driving_direction)
self.space.add(self.car_body, self.car_shape)
def update_screen(self, color):
self.space.step(1./10)
self.game.clock.tick()
if self.game.draw_screen:
self.game.screen.fill(THECOLORS[color])
self.space.debug_draw(self.game.options)
self.show_hud()
pygame.display.flip()
def _getLanes(self):
# about lane
static = [
pymunk.Segment(self.space.static_body,
self.l_lane1Start, self.l_lane1Stop, 1),
pymunk.Segment(self.space.static_body,
self.l_lane1Stop, self.m_llaneJoint, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneJoint, self.l_lane2Start, 1),
pymunk.Segment(self.space.static_body,
self.l_lane2Start, self.l_lane2Stop, 1),
pymunk.Segment(self.space.static_body,
self.l_lane2Stop, self.m_llaneStop, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneStop, self.m_rlaneStop, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneStop, self.e_lane2Stop, 1),
pymunk.Segment(self.space.static_body,
self.e_lane2Stop, self.e_lane2Start, 1),
pymunk.Segment(self.space.static_body,
self.e_lane2Start, self.m_rlaneJoint, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneJoint, self.e_lane1Stop, 1),
pymunk.Segment(self.space.static_body,
self.e_lane1Stop, self.e_lane1Start, 1),
pymunk.Segment(self.space.static_body,
self.e_lane1Start, self.m_rlaneStart, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneStart, self.m_llaneStart, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneStart, self.l_lane1Start, 1)
]
for s in static:
s.friction = 1.
s.group = 1
s.collision_type = 1
s.color = THECOLORS['black']
return static
def _draw_blackBackground(self):
color = (0, 0, 0)
pygame.draw.polygon(screen, color, self.point_list_left)
pygame.draw.polygon(screen, color, self.point_list_right)
pygame.draw.polygon(screen, color, self.point_list_up)
pygame.draw.polygon(screen, color, self.point_list_down)
def getBack(self):
backPoint = self.goal[0][0][0] # x-axis
checkingList = []
for i in xrange(self.numCar):
if self.Cars[i].check_getback(backPoint):
checkingList.append(i)
nCar = len(checkingList)
if nCar != 0:
choise = self.newY
shuffle(choise)
color_pro = np.random.sample(nCar) < 0.5
for i, index in enumerate(checkingList):
if color_pro[i]: color = 0
else : color = 1
pos_y = choise[i%len(choise)]
self.Cars[index].resetCar(color, 0, pos_y)
# val = raw_input("Stop checkGetBack : ")
# for j in checkingList:
# def resetCar(self, color)
def get_reward(self):
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.)
done = False
if self.car_is_crashed():
reward = -800
return reward, done
if my_x >= goalx:
done = True
if my_color == 0:
if my_y > ySep:
reward = 700
else:
reward = -500
else:
if my_y < ySep:
reward = 700
else:
reward = -500
return reward, done
if my_color == 0:
angle1 = self._get_angle(self.redGoal[0])
angle2 = self._get_angle(self.redGoal[1])
reward = self.direct*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 = self.direct*max(math.cos(my_angle-angle1), math.cos(my_angle-angle2))
return reward, done
# 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
def resetCar(self, color, x, y):
self.color = color
self.car_body.position = x, y
self.car_body.angle = 0.
if self.color == 0:
self.car_shape.color = THECOLORS["green"]
else:
self.car_shape.color = THECOLORS["red"]
def check_dest(self):
my_color = self.color
my_angle = self.car_body.angle
my_velocity = self.car_body.velocity
if my_color == 0:
angle1 = self._get_angle(self.redGoal[0])
angle2 = self._get_angle(self.redGoal[1])
reward = max(my_angle*math.cos(angle1), my_angle*math.cos(angle2))
else:
angle1 = self._get_angle(self.greenGoal[0])
angle2 = self._get_angle(self.greenGoal[1])
reward = max(my_angle*math.cos(angle1), my_angle*math.cos(angle2))
return reward
def check_getback(self, x_point):
'''
Check car have to be back.
x_point : back point
'''
x, y = self.car_body.position
if x >= x_point: return True
else: return False
# def testFindGoal(self):
# if self.color == 0:
# dist1 = self._euclidean_dist(self.redGoal[0])
# dist2 = self._euclidean_dist(self.redGoal[1])
# if dist1 < dist2:
# angle = self._get_angle(self.redGoal[0])
# else:
# angle = self._get_angle(self.redGoal[1])
# # angle = self._get_angle(self.redGoal[0])
# else:
# # angle = self._get_angle(self.greenGoal[0])
# dist1 = self._euclidean_dist(self.greenGoal[0])
# dist2 = self._euclidean_dist(self.greenGoal[1])
# if dist1 < dist2:
# angle = self._get_angle(self.greenGoal[0])
# else:
# angle = self._get_angle(self.greenGoal[1])
# return angle
def _getLanes(self):
# about lane
static = [
pymunk.Segment(self.space.static_body,
self.l_lane1Start, self.l_lane1Stop, 1),
pymunk.Segment(self.space.static_body,
self.l_lane1Stop, self.m_llaneJoint, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneJoint, self.l_lane2Start, 1),
pymunk.Segment(self.space.static_body,
self.l_lane2Start, self.l_lane2Stop, 1),
pymunk.Segment(self.space.static_body,
self.l_lane2Stop, self.m_llaneStop, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneStop, self.m_rlaneStop, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneStop, self.e_lane2Stop, 1),
pymunk.Segment(self.space.static_body,
self.e_lane2Stop, self.e_lane2Start, 1),
pymunk.Segment(self.space.static_body,
self.e_lane2Start, self.m_rlaneJoint, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneJoint, self.e_lane1Stop, 1),
pymunk.Segment(self.space.static_body,
self.e_lane1Stop, self.e_lane1Start, 1),
pymunk.Segment(self.space.static_body,
self.e_lane1Start, self.m_rlaneStart, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneStart, self.m_llaneStart, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneStart, self.l_lane1Start, 1)
]
for s in static:
s.friction = 1.
s.group = 1
s.collision_type = 1
s.color = THECOLORS['black']
return static
def get_position_sonar_readings(self):
color = float(self.color)
x, y = self.car_body.position
angle = self.car_body.angle
vel = self.direct/self.vmax
car_info = [color, x, y, vel, angle]
arm = self.make_sonar_arm()
readings = []
# Rotate them and get readings.
for i in range(-90, 91, 30):
readings.append(self.get_arm_distance(arm, pi_unit*i))
if show_sensors:
pygame.display.update()
self.readings = np.array(readings)
return car_info+readings
def check_getback(self, x_point):
'''
Check car have to be back.
x_point : back point
'''
x, y = self.car_body.position
if x >= x_point: return True # x-axis: return True
else: return False
# def testFindGoal(self):
# if self.color == 0:
# dist1 = self._euclidean_dist(self.redGoal[0])
# dist2 = self._euclidean_dist(self.redGoal[1])
# if dist1 < dist2:
# angle = self._get_angle(self.redGoal[0])
# else:
# angle = self._get_angle(self.redGoal[1])
# # angle = self._get_angle(self.redGoal[0])
# else:
# # angle = self._get_angle(self.greenGoal[0])
# dist1 = self._euclidean_dist(self.greenGoal[0])
# dist2 = self._euclidean_dist(self.greenGoal[1])
# if dist1 < dist2:
# angle = self._get_angle(self.greenGoal[0])
# else:
# angle = self._get_angle(self.greenGoal[1])
# return angle
def _getLanes(self):
# about lane
static = [
pymunk.Segment(self.space.static_body,
self.l_lane1Start, self.l_lane1Stop, 1),
pymunk.Segment(self.space.static_body,
self.l_lane1Stop, self.m_llaneJoint, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneJoint, self.l_lane2Start, 1),
pymunk.Segment(self.space.static_body,
self.l_lane2Start, self.l_lane2Stop, 1),
pymunk.Segment(self.space.static_body,
self.l_lane2Stop, self.m_llaneStop, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneStop, self.m_rlaneStop, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneStop, self.e_lane2Stop, 1),
pymunk.Segment(self.space.static_body,
self.e_lane2Stop, self.e_lane2Start, 1),
pymunk.Segment(self.space.static_body,
self.e_lane2Start, self.m_rlaneJoint, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneJoint, self.e_lane1Stop, 1),
pymunk.Segment(self.space.static_body,
self.e_lane1Stop, self.e_lane1Start, 1),
pymunk.Segment(self.space.static_body,
self.e_lane1Start, self.m_rlaneStart, 1),
pymunk.Segment(self.space.static_body,
self.m_rlaneStart, self.m_llaneStart, 1),
pymunk.Segment(self.space.static_body,
self.m_llaneStart, self.l_lane1Start, 1)
]
for s in static:
s.friction = 1.
s.group = 1
s.collision_type = 1
s.color = THECOLORS['black']
return static
def _draw_blackBackground(self):
color = (0, 0, 0)
pygame.draw.polygon(screen, color, self.point_list_left)
pygame.draw.polygon(screen, color, self.point_list_right)
pygame.draw.polygon(screen, color, self.point_list_up)
pygame.draw.polygon(screen, color, self.point_list_down)
def get_position_sonar_readings(self):
color = float(self.color)
x, y = self.car_body.position
angle = self.car_body.angle
vel = self.direct/self.vmax
car_info = [color, x, y, vel, angle]
arm = self.make_sonar_arm()
readings = []
# Rotate them and get readings.
for i in range(-90, 91, 30):
readings.append(self.get_arm_distance(arm, pi_unit*i))
if show_sensors:
pygame.display.update()
self.readings = np.array(readings)
return car_info+readings
def resetCar(self, color, x, y):
self.color = color
self.car_body.position = x, y
self.car_body.angle = 0.
if self.color == 0:
self.car_shape.color = THECOLORS["green"]
else:
self.car_shape.color = THECOLORS["red"]
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 = 800, 200
self.cat_shape = pymunk.Circle(self.cat_body, 30)
self.cat_shape.color = THECOLORS["orange"]
self.car_shape.elasticity = 1.0
self.cat_shape.angle = 0.5
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, 15)
self.car_shape.color = THECOLORS["green"]
self.car_shape.elasticity = 1.0
self.car_body.angle = r
self.driving_direction = Vec2d(1, 0).rotated(self.car_body.angle)
self.car_body.apply_impulse(self.driving_direction)
self.space.add(self.car_body, self.car_shape)
def init_walls(space): # Initializes the four outer walls of the board
body = pymunk.Body(body_type=pymunk.Body.STATIC)
walls = [pymunk.Segment(body, (0, 0), (0, BOARD_SIZE), BOARD_WALLS_SIZE),
pymunk.Segment(body, (0, 0), (BOARD_SIZE, 0), BOARD_WALLS_SIZE),
pymunk.Segment(
body, (BOARD_SIZE, BOARD_SIZE), (BOARD_SIZE, 0), BOARD_WALLS_SIZE),
pymunk.Segment(
body, (BOARD_SIZE, BOARD_SIZE), (0, BOARD_SIZE), BOARD_WALLS_SIZE)
]
for wall in walls:
wall.color = BOARD_WALLS_COLOR
wall.elasticity = WALLS_ELASTICITY
space.add(walls)
# Initialize pockets
def init_walls(space): # Initializes the four outer walls of the board
body = pymunk.Body(body_type=pymunk.Body.STATIC)
walls = [pymunk.Segment(body, (0, 0), (0, BOARD_SIZE), BOARD_WALLS_SIZE),
pymunk.Segment(body, (0, 0), (BOARD_SIZE, 0), BOARD_WALLS_SIZE),
pymunk.Segment(
body, (BOARD_SIZE, BOARD_SIZE), (BOARD_SIZE, 0), BOARD_WALLS_SIZE),
pymunk.Segment(
body, (BOARD_SIZE, BOARD_SIZE), (0, BOARD_SIZE), BOARD_WALLS_SIZE)
]
for wall in walls:
wall.color = BOARD_WALLS_COLOR
wall.elasticity = WALLS_ELASTICITY
space.add(walls)
# Initialize pockets
def init_walls(space): # Initializes the four outer walls of the board
body = pymunk.Body(body_type=pymunk.Body.STATIC)
walls = [pymunk.Segment(body, (0, 0), (0, BOARD_SIZE), BOARD_WALLS_SIZE),
pymunk.Segment(body, (0, 0), (BOARD_SIZE, 0), BOARD_WALLS_SIZE),
pymunk.Segment(
body, (BOARD_SIZE, BOARD_SIZE), (BOARD_SIZE, 0), BOARD_WALLS_SIZE),
pymunk.Segment(
body, (BOARD_SIZE, BOARD_SIZE), (0, BOARD_SIZE), BOARD_WALLS_SIZE)
]
for wall in walls:
wall.color = BOARD_WALLS_COLOR
wall.elasticity = WALLS_ELASTICITY
space.add(walls)
# Initialize pockets
def init_striker(space, passthrough, action, player):
inertia = pymunk.moment_for_circle(STRIKER_MASS, 0, STRIKER_RADIUS, (0, 0))
body = pymunk.Body(STRIKER_MASS, inertia)
if player == 1:
body.position = (action[0], 140)
if player == 2:
body.position = (action[0], BOARD_SIZE - 140)
# print " Final Position: ", body.position
# body.position=(100,)
body.apply_force_at_world_point((cos(action[1]) * action[2], sin(
action[1]) * action[2]), body.position + (STRIKER_RADIUS * 0, STRIKER_RADIUS * 0))
shape = pymunk.Circle(body, STRIKER_RADIUS, (0, 0))
shape.elasticity = STRIKER_ELASTICITY
shape.color = STRIKER_COLOR
mask = pymunk.ShapeFilter.ALL_MASKS ^ passthrough.filter.categories
sf = pymunk.ShapeFilter(mask=mask)
shape.filter = sf
shape.collision_type = 2
space.add(body, shape)
return [body, shape]
# Adds coins to the board at the given coordinates
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