def update(dt): global time_left, gameover time_left -= dt time_display.text = "%d seconds remaining" % (time_left) if not gameover and time_left <= 0: gameover_display.text = "Gameover, press ENTER to restart" gameover = True #return elif not gameover and player.getBody().getPosition()[0] > 3600: gameover_display.text = "You won! Press ENTER to play again" gameover = True player.update() # Position everything but try to keep player in the middle of the screen player_pos = player.body.getPosition()[0:2] player_offset = (window.width / 2 - player_pos[0], window.height / 2 - player_pos[1]) window_offset = [0, 0] if player_offset[0] > 0: window_offset[0] = player_offset[0] elif player_offset[0] < -(background.image.width - window.width): window_offset[0] = (background.image.width - window.width + player_offset[0]) if player_offset[1] > 0: window_offset[1] = player_offset[1] elif player_offset[1] < -(background.image.height - window.height): window_offset[1] = (background.image.height - window.height + player_offset[1]) object_offset = (player_offset[0] - window_offset[0], player_offset[1] - window_offset[1]) player.sprite.x, player.sprite.y = (window.width / 2 - window_offset[0], window.height / 2 - window_offset[1]) background.position = object_offset # Convert world positions to screen positions for physics in level_physics: physics.update() physics.getSprite().position = get_window_position(physics.getPosition(), object_offset) for overlay in level_overlays: overlay.getSprite().position = get_window_position( overlay.position, object_offset) # Physics global physics_dt physics_dt += dt # ODE recomends constant world steps to stop jitter while physics_dt > PHYSICS_STEP: space.collide(None, near_callback) world.step(PHYSICS_STEP) contactgroup.empty() physics_dt -= PHYSICS_STEP
def update(): dt = clock.tick(settings.fps) / 1000 # in seconds camera.main.update(dt) physics.update() for entity in EntityManager.entities: entity.update(dt) if settings.debug: print(1 / dt, " FPS")
frame_count = 0 # SUBPROBLEM 4: uncomment the code below. # preallocate locks for objects locks_ptr = preallocate_locks(num_balls) num_to_record = 100 num_recorded = 0 list_of_fps = [] while True: # so we can do a quantitative comparison with Timer() as t: update(positions, velocities, grid, radius, grid_spacing, locks_ptr, physics_step) # udpate our estimate of how fast the simulator runs physics_step = 0.9 * physics_step + 0.1 * t.interval total_time += t.interval frame_count += 1 ball_indices = np.arange(num_balls) if total_time > anim_step: animator.update(positions) fps = frame_count / total_time list_of_fps.append(fps) print("{} simulation frames per second".format(fps))
animator = Animator(positions, radius * 2) # simulation/animation time variablees physics_step = 1.0 / 100 # estimate of real-time performance of simulation anim_step = 1.0 / 30 # FPS total_time = 0 frame_count = 0 # SUBPROBLEM 4: uncomment the code below. # preallocate locks for objects locks_ptr = preallocate_locks(num_balls) while True: with Timer() as t: update(positions, velocities, grid, radius, grid_size, locks_ptr, physics_step) # udpate our estimate of how fast the simulator runs physics_step = 0.9 * physics_step + 0.1 * t.interval total_time += t.interval frame_count += 1 if total_time > anim_step: animator.update(positions) print("{} simulation frames per second".format(frame_count / total_time)) frame_count = 0 total_time = 0 # SUBPROBLEM 3: sort objects by location. Be sure to update the # grid if objects' indices change! Also be sure to sort the # velocities with their object positions!
def update(self): physics.update(self.bodies) self.do_collisions()