Пример #1
0
class Player(Entity):
    def __init__(self, image_path, screen, initpos, camera):
        super(Player, self).__init__(image_path, screen, initpos, camera)
        self.speed = 5
        self.base_image = pygame.transform.rotate(self.image, 180)
        self.flashlight = Light((0, 0, 0), self.rpos, self.rpos, 0.0, 30.0)
        self.headlight = Light((255, 0, 0), self.rpos, self.rpos, 0.0, 50.0)

        self.lights.append(self.headlight)
        self.lights.append(self.flashlight)

    def update(self, time_passed, level, currentCam):
        super(Player, self).update(time_passed, level, currentCam)

        # draw a vector between the player and the cursor
        # and rotate the player image to face it.
        # Have to use the current camera in order
        # to get the player's screen coordinates, (sx, sy)
        sx, sy = currentCam.real_to_screen(self.rpos.x, self.rpos.y)
        mouse_sx, mouse_sy = pygame.mouse.get_pos()
        mouse_rx, mouse_ry = currentCam.screen_to_real(mouse_sx, mouse_sy)

        dx = sx - mouse_sx
        dy = sy - mouse_sy
        player_to_mouse = Vec2d(dx, dy)

        self.image = pygame.transform.rotate(self.base_image, -math.degrees(player_to_mouse.angle))

        # with the entity's position updated, bring the light emitter with it
        self.flashlight.emitter_pos = self.rpos
        self.flashlight.proj_pos = Vec2d(mouse_rx, mouse_ry)
        self.flashlight.update_surface(level)

        self.headlight.emitter_pos = self.rpos
        self.headlight.proj_pos = self.rpos
        self.headlight.update_surface(level)

        # offset the camera location to place the player
        # in the middle of the screen
        offsetX = self.rpos.x - self.camera.screen_width / 2
        offsetY = self.rpos.y - self.camera.screen_height / 2

        self.camera.move((offsetX, offsetY))

    def movement_handler(self, keydown):
        self.vel.x, self.vel.y = {
            pygame.K_w: (self.vel.x, -1),
            pygame.K_s: (self.vel.x, 1),
            pygame.K_a: (-1, self.vel.y),
            pygame.K_d: (1, self.vel.y),
        }[keydown]
        self.vel = self.vel.normalized()