def update_vectors(self,time_passed_seconds): '''updates the position after a time has passed''' #Update roation vector rotation = self.rotation_direction * self.rotation_speed * time_passed_seconds rotation_matrix = Matrix44.xyz_rotation(*rotation) self.camera_matrix *= rotation_matrix # Calcluate movment and add it to camera matrix translate heading = Vector3(self.camera_matrix.forward) movement = heading * self.movement_direction.z * self.movement_speed #Find the x and y of how far forward we move movement_x = movement[0] * time_passed_seconds movement_y = movement[2] * time_passed_seconds #Move us in the aproropiate direction if we won't hit a wall if(movement_y != 0): wall_buffer= rendering_opts['wall_backoff'] if movement_y > 0 else -rendering_opts['wall_backoff'] if(self.game_map.can_go(self.x,self.y + movement_y + wall_buffer )): self.camera_matrix[3,2] = self.y + movement_y self.y = self.y + movement_y if(movement_x != 0): wall_buffer= rendering_opts['wall_backoff'] if movement_x > 0 else -rendering_opts['wall_backoff'] if(self.game_map.can_go(self.x + movement_x +wall_buffer,self.y )): self.camera_matrix[3,0] = self.x + movement_x self.x = self.x + movement_x if(movement_x != 0 or movement_y != 0): self.game_map.handle_pickups(self)
def start(): pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE, pygame.HWSURFACE|pygame.OPENGL|pygame.DOUBLEBUF) resize(*SCREEN_SIZE) init() clock = pygame.time.Clock() # This object renders the 'map' map = Map() # Camera transform matrix cam_matrix = Matrix44() cam_matrix.translate = (10.0, 0.6, 10.0) # Initialize speeds and directions rotation_direction = Vector3() rotation_speed = radians(90.0) movement_direction = Vector3() movement_speed = 5.0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: pygame.quit() quit() # Clear the screen, and z-buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) time_passed = clock.tick() time_passed_seconds = time_passed / 1000.0 pressed_keys = pygame.key.get_pressed() # Reset rotation and movement directions rotation_direction.set(0.0, 0.0, 0.0) movement_direction.set(0.0, 0.0, 0.0) # Modify direction vectors for key presses if pressed_keys[pygame.K_LEFT]: rotation_direction.y = +1.0 elif pressed_keys[pygame.K_RIGHT]: rotation_direction.y = -1.0 if pressed_keys[pygame.K_UP]: rotation_direction.x = -1.0 elif pressed_keys[pygame.K_DOWN]: rotation_direction.x = +1.0 if pressed_keys[pygame.K_z]: rotation_direction.z = -1.0 elif pressed_keys[pygame.K_x]: rotation_direction.z = +1.0 if pressed_keys[pygame.K_q]: movement_direction.z = -1.0 elif pressed_keys[pygame.K_a]: movement_direction.z = +1.0 # Calculate rotation matrix and multiply by camera matrix rotation = rotation_direction * rotation_speed * time_passed_seconds rotation_matrix = Matrix44.xyz_rotation(*rotation) cam_matrix *= rotation_matrix # Calcluate movment and add it to camera matrix translate heading = Vector3(cam_matrix.forward) movement = heading * movement_direction.z * movement_speed cam_matrix.translate += movement * time_passed_seconds # Upload the inverse camera matrix to OpenGL glLoadMatrixd(cam_matrix.get_inverse().to_opengl()) # Light must be transformed as well glLight(GL_LIGHT0, GL_POSITION, (0, 1.5, 1, 0)) map.render() pygame.display.flip()