def main(): # initializing and drawing background UI elements background = UIBox(g_const.screen_size, (0, 0, 0), (0, 0)) arena = UIBox(g_const.arena_size, (50, 50, 50), g_const.arena_pos) sidebar = UIBox(g_const.sidebar_size, (50, 50, 50), g_const.sidebar_pos) background.draw() sidebar.draw() evt_man = EventManager() terrain = Terrain() game_state = GameState(terrain) start = time.time() evt_man.start_world_update() while True: dt = time.time() - start if dt >= frame_length: start = time.time() # reset start tick events = evt_man.processEvents() # handle events game_state.update(events) # update game state # draw objects arena.draw() game_state.curr_shape.draw() terrain.draw() # update display pyg.display.update()
class Context: def __init__(self, physicsWorld): pygame.init() # constants self.PPM = 20.0 # pixels per meter self.TARGET_FPS = 60 self.TIME_STEP = 1.0 / self.TARGET_FPS self.SCREEN_WIDTH = 1200 self.SCREEN_HEIGHT = 800 self.screenSize = (self.SCREEN_WIDTH, self.SCREEN_HEIGHT) self.zoom = 1.0 self.offset = (0.0, 0.0) self.viewCenter = (self.SCREEN_WIDTH/2, self.SCREEN_HEIGHT/2) self.clock = pygame.time.Clock() # core objects self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT)) self.world = physicsWorld # world objects self.terrain = Terrain(self, 0, self.SCREEN_HEIGHT/self.PPM/2, 2, 25, TerrainGenerator.Composer(1, math.pi)) self.car = Car(self, 0, 0) self.keys = pygame.key.get_pressed() def update(self): self.keys = pygame.key.get_pressed() self.terrain.update() self.car.update() self.world.Step(self.TIME_STEP, 10, 10) def draw(self): self.screen.fill((0, 0, 0, 0)) self.terrain.draw() self.car.draw() pygame.display.flip() self.clock.tick(self.TARGET_FPS)
# Move up # Here should be the API call # Let's just move the player for now direction = DIRECTIONS['UP'] if event.key == pygame.K_DOWN: direction = DIRECTIONS['DOWN'] if event.key == pygame.K_LEFT: direction = DIRECTIONS['LEFT'] if event.key == pygame.K_RIGHT: direction = DIRECTIONS['RIGHT'] terrain.refresh_environment(direction) # Draw tiles on terrain terrain.update() # Draw each player on terrain terrain.update_player(window) # Draw terrain on window terrain.draw(window) # Display window on the screen pygame.display.update() # End of the game loop pygame.quit()
lidar.setX(robot.getX()) lidar.setY(robot.getY()) lidar.setTheta(robot.getTheta()) lidar.fire() if (t * dT) % (1.0 / 30.0) < 0.001: # Drawing plt.clf() # plt.axis('equal') plt.text(10, 1900, "t: {0:.3f} s".format((t + 1) * dT), fontsize=12) plt.text(10, 1800, "X: {0:.0f} mm".format(robot.getX()), fontsize=12) plt.text(10, 1700, "Y: {0:.0f} mm".format(robot.getY()), fontsize=12) plt.text(10, 1600, "T: {0:.3f} rad".format(robot.getTheta()), fontsize=12) robotpos[0] = robotpos[0] + [robot.getX()] robotpos[1] = robotpos[1] + [robot.getY()] # print(robotpos) plt.plot(robotpos[0], robotpos[1], 'k+') # lidar.draw() terrain.draw() robot.draw() robotControl.draw() #draw trajectory # plt.plot(trajectory[0], trajectory[1], 'k') plt.pause(0.0001) plt.show()