def main(): pygame.init() screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("The ball drops") clock = pygame.time.Clock() draw_options = DrawOptions(screen) space = pymunk.Space() space.gravity = 0, -100 x = random.randint(120, 380) ground = Ground(space) copter = Dualcopter((x, 550), space) thrust_angle = 0 thrust = 0 i = 0 while True: i += 1 print ('================') print ('velocity', copter.shape.body.velocity) print ('angle', copter.shape.body.angle) print ('rv', copter.shape.body.rotation_vector) #if i%20==0: pygame.image.save(screen, "/tmp/out-%d.jpeg" % i) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) elif event.type == pygame.KEYDOWN: print ('thrust',thrust,'angle',thrust_angle) if event.key == 274: print ('down') thrust -= 20 elif event.key == 273: print ('up') thrust += 20 elif event.key == 275: print ('right') thrust_angle += 10 elif event.key == 276: thrust_angle -= 10 print ('left') T = thrust / 6.0 copter.shape.body.apply_force_at_local_point((0, T), (-10, -25)) copter.shape.body.apply_force_at_local_point((0, T), (-5, -25)) copter.shape.body.apply_force_at_local_point((0, T), (5, -25)) copter.shape.body.apply_force_at_local_point((0, T), (10, -25)) F = (T*np.sin(np.deg2rad(thrust_angle)), T*np.cos(np.deg2rad(thrust_angle))) copter.shape.body.apply_force_at_local_point(F, (-10, 25)) copter.shape.body.apply_force_at_local_point(F, (-5, 25)) copter.shape.body.apply_force_at_local_point(F, (5, 25)) copter.shape.body.apply_force_at_local_point(F, (10, 25)) screen.fill((0, 0, 0)) space.debug_draw(draw_options) space.step(1/50.0) pygame.display.update() clock.tick(50)
def __init__(self): self.sim_steps = 0 # counter incremented each step for debug pg.init() self.screen_width, self.screen_height = 1366, 768 self.screen = pg.display.set_mode( (self.screen_width, self.screen_height)) # self.screen.fill(WHITE) pg.display.set_caption("PyGame Display") self.clock = pg.time.Clock() self.space = pm.Space() self.draw_options = DrawOptions(self.screen) self.CENTER = (self.screen_width / 2, self.screen_height / 2) NORTH, SOUTH, EAST, WEST = math.pi / 2, 3 * math.pi / 2, 0, math.pi # PLEASE ONLY USE EAST AT THIS TIME self.robot = Robot(mass=20, pos=self.CENTER, ori=EAST) self.space.add(self.robot.body) self.space.add(self.robot.shape) for sensor_shape in self.robot.sensors: self.space.add(sensor_shape) self.wall_shapes = self.assemble_walls(self.screen_width, self.screen_height, 180) self.goal_body, self.goal_shape = self.add_goal() self.last_goal_position = -1 self.move_goal() self.time_since_collision = 0
def main(): pygame.init() screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("The ball drops") clock = pygame.time.Clock() draw_options = DrawOptions(screen) space = pymunk.Space() space.gravity = 0, -100 x = random.randint(120, 380) ground = Ground(space) ball = Ball((x, 550), space) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: sys.exit(0) """ This is the code that applies force to the body of the drone """ if ball.shape.body.position.y < 200: """ (0, 400) means apply 400 units of force in the dirction of y (0,0) is the co-ordinate to apply the force too""" ball.shape.body.apply_force_at_local_point((0, 400), (0, 0)) screen.fill((0, 0, 0)) space.debug_draw(draw_options) space.step(1 / 50.0) pygame.display.update() clock.tick(50)
def __init__(self): self.window = None self.width = 800 self.height = 600 # init framebuffer for observations pygame.init() self.display = None self.vscreen = pygame.Surface((self.width, self.height)) self.clock = pygame.time.Clock() self.draw_options = DrawOptions(self.vscreen) self.space = pymunk.Space() self.sim_steps = 10 # number of simulation steps per env step self.step_time = 0.05 # amount of simulation time per env step (seconds) self.player1 = Paddle(id='player1', position=(20, self.height / 2), angle=0, space=self.space, window_height=self.height) self.player2 = Paddle(id='player2', position=(self.width - 20, self.height / 2), angle=radians(180), space=self.space, window_height=self.height) self.puck = Puck((self.width / 2, self.height / 2), (-500, 0), self.space) self.top_rail = Rail(position=(self.width / 2, self.height - 20), space=self.space, width=self.width) self.bottom_rail = Rail(position=(self.width / 2, 20), space=self.space, width=self.width) self.done = False self.reward = 0 self.action_set = [0, 1, 2] self.action_space = spaces.Discrete(3) self.collision_handler = self.space.add_default_collision_handler() self.collision_handler.begin = self.coll_begin self.last_hit = None
def render(self, mode='human'): if mode is 'human': if not self.display: self.display = pygame.display.set_mode( (self.width, self.height)) self.draw_options = DrawOptions(self.display) pygame.display.set_caption("Pong") pygame.display.flip()
def _setupPygame(self, screenX, screenY, gameName): pygame.init() pygame.display.set_mode((screenX, screenY)) pygame.display.set_caption(gameName) self.screen = pygame.display.get_surface() self.screenX = screenX self.screenY = screenY self.options = DrawOptions(self.screen) self.clock = pygame.time.Clock()
def render(self, mode='human'): if not self.display: self.display = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption("AlphaRacer") self.draw_options = DrawOptions(self.display) self.display.fill((0, 0, 0)) self.space.debug_draw(self.draw_options) if mode is 'human': pygame.display.update() if mode is 'rgb_array': obs = pygame.surfarray.array3d(self.display) obs = obs.swapaxes(0, 1) return obs
def __init__(self, record=True): self.armies = [] self.objects = [] self.video = [] self.fps = 60.0 self.done = False self.record_mouse = False self.mouse_traj = [] self.record = record self.create_screen() self.create_space() self.draw_options = DrawOptions(self.screen)
def __init__(self): # Pygame stuff pygame.init() self.size = (1000, 600) self.screen = pygame.display.set_mode(self.size) self.screen.set_alpha(None) self.clock = pygame.time.Clock() self.bg = pygame.image.load('map1.png') self.draw_options = DrawOptions(self.screen) # Physics stuff self.space = pymunk.Space() self.space.gravity = Vec2d(0., 0.) self.steps = 0 self.exit = False #self.create_car(420, 480 ,0.0) # starting point for car map 2 self.create_car(360, 530, 0.0) # starting point for car map 1 self.show_sonar = True
def _setupPygame(self, screenX, screenY, gameName): pygame.mixer.pre_init(22050, -16, 2, 512) pygame.mixer.init() pygame.init() pygame.display.set_mode((screenX, screenY)) pygame.display.set_caption(gameName) self.screen = pygame.display.get_surface() self.screenX = screenX self.screenY = screenY self.options = DrawOptions(self.screen) self.clock = pygame.time.Clock() songs = [ "sounds\music\drive_with_me_looped.wav", "sounds\music\motivation_looped.wav" ] dir_path = os.path.dirname(os.path.realpath(__file__)) pygame.mixer.music.load(os.path.join(dir_path, random.choice(songs))) pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.set_volume(0.35)
def main(): pygame.init() screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption("Joints. Just wait and the L will tip over") clock = pygame.time.Clock() space = pymunk.Space() space.gravity = (0.0, 0.0) balls = [] draw_options = DrawOptions(screen) # ball, ball_shape = add_ball(space) ticks_to_next_ball = 10 while True: for event in pygame.event.get(): # print(event.type) if event.type == QUIT: sys.exit(0) elif event.type == MOVE: pygame.draw.circle(screen, (255, 0, 0), (300, 300), 30) # ball.angle += (np.pi/4) # ball.position = ball.position[0] + 2*np.cos(ball.angle) , ball.position[1] + 2*np.sin(ball.angle) # ticks_to_next_ball -= 1 # if ticks_to_next_ball <= 0: # ticks_to_next_ball = 25 # ball_shape = add_ball(space) # balls.append(ball_shape) space.step(1 / 50.0) screen.fill((255, 255, 255)) space.debug_draw(draw_options) pygame.display.flip() clock.tick(50)
def visualize_simulation(xl1, yl1, θl1, xl2, yl2, θl2): pygame.init() screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption("Ball in the bucket game") clock = pygame.time.Clock() space = pymunk.Space() space.gravity = (0.0, -900.0) lines = add_static_L(space, xl1, yl1, θl1) lines = add_static_L(space, xl2, yl2, θl2) lines = add_bucket(space) ball = add_ball_static(space) draw_options = DrawOptions(screen) stop = False while not stop: for event in pygame.event.get(): if event.type == QUIT: stop=True elif event.type == KEYDOWN and event.key == K_ESCAPE: stop=True if ball.body.position.x > 410 and ball.body.position.y < 240: print("Ball in!") stop=True screen.fill((255,255,255)) space.debug_draw(draw_options) space.step(1/50.0) pygame.display.flip() clock.tick(50)
def draw(screen, space): space.debug_draw(DrawOptions(screen))
import pymunk as pm from pygame.color import * from pygame.locals import * from pymunk import Vec2d from pymunk.pygame_util import DrawOptions """ I'm using SO_Circles as an example on how to integrate physics into my pygame sprites. """ dim = (600,600) win = pg.display.set_mode(dim) pg.display.caption("Pymunk Test") options = DrawOptions(win) def flipy(p): """ Change chipmunk physics to pg coordinates """ return Vec2d((p[0], -p[1]+dim[0])) class Entity(pg.sprite.Sprite): def __init__(self, pos, space, mass=1): super().__init__() # pygame stuff self.orig_image = pg.image.load('data/adventurer-run3-00.png') self.image = self.orig_image self.rect = self.image.get_rect(topleft=pos)
return rot_image WIDTH = 800 HEIGHT = 600 SIZE = (WIDTH, HEIGHT) pygame.init() screen = pygame.display.set_mode(SIZE) clock = pygame.time.Clock() space = pymunk.Space() space.gravity = 0, 0 space.gravity = 0, 2000 options = DrawOptions(screen) radius = 30 all = pygame.sprite.Group() Candy.groups = [all] red_lolli_img = pygame.image.load("sandbox/lollipopRed.png") red_lolli_img = pygame.transform.smoothscale(red_lolli_img, (radius * 2, radius * 2)) Candy.images = [red_lolli_img] candy = Candy(3 * WIDTH // 4, 50, space) segment_shape1 = pymunk.Segment(space.static_body, (0, 0), (800, 80), 2) segment_shape1.body.position = 0, HEIGHT // 2 segment_shape1.elasticity = 0.6
def create_an_expmple(map_random, config, log_counter): crashed = False ep = Experience_Pool() pygame.init() screen = pygame.display.set_mode((SCREEN_HEIGHT, SCREEN_WIDTH)) pygame.display.set_caption("Joints. Just wait and the L will tip over") clock = pygame.time.Clock() space = pymunk.Space() space.gravity = (0.0, 0.0) draw_options = DrawOptions(screen) ### create the world map ### create the car car, carshape = create_car(space, config['cars']) sensors = create_sensors(space, car, carshape, config['sensors'][0], 'trio') if map_random == False: ### create walls # walls = create_walls(space, config['walls']) ### create stones stones, stones_shape = create_stones(space, config['stones']) ### create cats cats, cats_shape = create_cats(space, config['cats']) else: stones, stones_shape = create_random_stones(space, 3) cats, cats_shape = create_random_cats(space, 2) ### initialize old_State old_state = Vec2d(BORDER, BORDER) while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) elif event.type == MOVE: car_angel_changed(car, np.pi / 4, sensors) elif event.type == RESET: reset_game(map_random, space, car, sensors, np.pi / 4) car_move(car, sensors) for cat in cats: cats_move(cat, log_counter) # is_detected(space,car,carshape,stones,stones_shape,cats,cats_shape,100) sensors_rectify(space, car, carshape, sensors, [-np.pi / 4, 0, np.pi / 4], 1) if (car.position[0] < BORDER - 5 or car.position[0] > SCREEN_WIDTH - BORDER + 5) or ( car.position[1] < BORDER - 5 or car.position[1] > SCREEN_HEIGHT - BORDER + 5): car_angel_changed(car, np.pi / 2, sensors) space.step(1 / 50.0) screen.fill(THECOLORS['whitesmoke']) space.debug_draw(draw_options) pygame.display.flip() clock.tick(50) new_state = old_state if log_counter.step_count % 100 == 0: new_state = car.position action = car.angle reward = 50 experience = Experience(old_state, action, new_state, reward) print(old_state, ' ', new_state) ep.experienct_pool.append(experience) old_state = new_state ep.show_all() log_counter.step_count += 1
def main(): pygame.init() pygame.font.init() screen = pygame.display.set_mode((width, height)) clock = pygame.time.Clock() tunings = (27, 2, 10) pid = PID(*tunings, output_limits=(-8000, 8000), proportional_on_measurement=True) pid.sample_time = 1 / REFRESH draw_options = DrawOptions(screen) pymunk.pygame_util.positive_y_is_up = True font = pygame.font.SysFont('Lucida Console', 11) space = pymunk.Space() space.gravity = 0, -100 x = random.randint(120, 380) drone = Drone((x, 450), space) ground = Ground(space) setpoint = 450 ticks = 0 while True: for event in pygame.event.get(): # print(event) if event.type == pygame.QUIT: sys.exit(0) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: setpoint += 5 elif event.key == pygame.K_DOWN: setpoint -= 5 elif event.key == pygame.K_ESCAPE: sys.exit(0) elif event.key == pygame.K_1: p, i, d = pid.tunings pid.tunings = (p + 0.5, i, d) elif event.key == pygame.K_q: p, i, d = pid.tunings pid.tunings = (p - 0.5, i, d) elif event.key == pygame.K_2: p, i, d = pid.tunings pid.tunings = (p, i + 0.5, d) elif event.key == pygame.K_w: p, i, d = pid.tunings pid.tunings = (p, i - 0.5, d) elif event.key == pygame.K_3: p, i, d = pid.tunings pid.tunings = (p, i, d + 0.5) elif event.key == pygame.K_e: p, i, d = pid.tunings pid.tunings = (p, i, d - 0.5) elif event.type == pygame.MOUSEBUTTONUP: setpoint = 600 - event.pos[1] # ticks=0 screen.fill((0, 0, 0)) error = drone.body.position.y - setpoint output = pid(error) space.debug_draw(draw_options) # if drone.shape.body.position.y < setpoint: if True: # output > 0: # drone.shape.body.apply_force_at_local_point((0, 340), (0, 0)) drone.shape.body.apply_force_at_local_point((0, output), (0, 0)) c = to_pygame(drone.body.position, screen) pygame.draw.aaline(screen, (0, 128, 255, .75), c, (c[0], c[1] - output // 10)) pygame.draw.aaline(screen, (128, 255, 128, .125), to_pygame((50, setpoint), screen), to_pygame((600 - 50, setpoint), screen)) # pygame.draw.aaline(screen, (255, 255, 255, .5), to_pygame((0,0), screen), to_pygame((100,100), screen)) textsurface = font.render( f"pos:{drone.body.position.y:7.1f} set:{setpoint} error:{error:7.1f} o:{output:7.1f}", False, WHITE) screen.blit(textsurface, (10, 10)) textsurface = font.render( f"pid:{['{:7.1f}'.format(p) for p in pid.components]}", False, WHITE) screen.blit(textsurface, (10, 25)) textsurface = font.render( f"tun:{['{:7.1f}'.format(p) for p in pid.tunings]}", False, WHITE) screen.blit(textsurface, (10, 40)) textsurface = font.render(f"ticks:{ticks}", False, WHITE) screen.blit(textsurface, (10, 55)) space.step(1 / REFRESH) pygame.display.update() clock.tick(REFRESH) pygame.display.set_caption(f"{drone.body.position.y:.1f} {setpoint}") ticks += 1