我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pygame.color()。
def makeCars(self): index1= list(range(len(self.lanePos1))) index2 = list(range(len(self.lanePos2))) shuffle(index1) shuffle(index2) color_pro = np.random.sample(self.numCar) < 0.5 lane_pro = np.random.sample(self.numCar) < 0.5 car = [] for i in xrange(0, self.numCar): color = 1 if color_pro[i]: color = 0 if lane_pro[i]: ## make use pop method. idx = index1.pop() lane = self.lanePos1[idx] else: idx = index2.pop() lane = self.lanePos2[idx] car.append(Car(0, lane[0], lane[1], self.space, self.width, self.height, goal = self.goal, car_radius=self.car_radius, agentId=i, color=color)) return car
def __init__(self, r, x, y, space, width, height, goal, car_radius=25, agentId=0, color=0): self.color = color # 0:green, 1:red self.agentId = agentId self.car_radius = car_radius self.space = space self.width = width self.height = height self.vmax = 200 self.redGoal = goal[0] self.greenGoal = goal[1] mass = 1 inertia = pymunk.moment_for_circle(mass, 0, 14, (0, 0)) self.car_body = pymunk.Body(mass, inertia) self.car_body.position = x, y self.car_shape = pymunk.Circle(self.car_body, car_radius) if self.color == 0: self.car_shape.color = THECOLORS["green"] else: self.car_shape.color = THECOLORS["red"] self.space.add(self.car_body, self.car_shape)
def makeCars(self): index1= list(range(len(self.lanePos1))) index2 = list(range(len(self.lanePos2))) shuffle(index1) shuffle(index2) color_pro = np.random.sample(self.numCar) < 0.5 lane_pro = np.random.sample(self.numCar) < 0.5 car = [] for i in xrange(0, self.numCar): color = 1 if color_pro[i]: color = 0 if lane_pro[i]: ## make use pop method. idx = index1.pop() lane = self.lanePos1[idx] else: idx = index2.pop() lane = self.lanePos2[idx] car.append(Car(0, lane[0], lane[1], self.space, self.width, self.height, goal = self.goal, goal2 = self.goal2, car_radius=self.car_radius, agentId=i, color=color)) return car
def getBack(self, test=False): checkingList = [] if test: backPoint = self.backPoint-300 else: backPoint = self.backPoint 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)
def __init__(self, r, x, y, space, width, height, goal, goal2, car_radius=25, agentId=0, color=0): self.color = color # 0:green, 1:red self.agentId = agentId self.car_radius = car_radius self.space = space self.width = width self.height = height self.vmax = 200 self.redGoal = goal[0] self.greenGoal = goal[1] self.eRedGoal = goal2[0] self.eGreenGoal = goal2[1] mass = 1 inertia = pymunk.moment_for_circle(mass, 0, 14, (0, 0)) self.car_body = pymunk.Body(mass, inertia) self.car_body.position = x, y self.car_shape = pymunk.Circle(self.car_body, car_radius) if self.color == 0: self.car_shape.color = THECOLORS["green"] else: self.car_shape.color = THECOLORS["red"] self.space.add(self.car_body, self.car_shape)
def check_dest(self): """ I think I do not this function. """ 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 init_pockets(space): pockets = [] for i in [(44.1, 44.1), (755.9, 44.1), (755.9, 755.9), (44.1, 755.9)]: 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 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
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 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 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_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_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 _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 __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