58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import pygame
|
|
from constants import PLAYER_RADIUS, PLAYER_TURN_SPEED, PLAYER_SPEED, PLAYER_SHOT_SPEED, PLAYER_SHOOT_COOLDOWN
|
|
from circleshape import CircleShape
|
|
from shot import Shot
|
|
|
|
class Player(CircleShape):
|
|
def __init__(self, x, y):
|
|
super().__init__(x, y, PLAYER_RADIUS)
|
|
self.rotation = 0
|
|
self.shot_cooldown_counter = 0
|
|
|
|
def draw(self, screen):
|
|
pygame.draw.polygon(screen, "white", self.triangle(), 2)
|
|
|
|
def update(self, dt):
|
|
self.shot_cooldown_counter -= dt
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_a]:
|
|
self.rotate(-dt)
|
|
|
|
if keys[pygame.K_d]:
|
|
self.rotate(dt)
|
|
|
|
if keys[pygame.K_w]:
|
|
self.move(dt)
|
|
|
|
if keys[pygame.K_s]:
|
|
self.move(-dt)
|
|
|
|
if keys[pygame.K_SPACE]:
|
|
self.shoot()
|
|
|
|
|
|
def rotate(self, dt):
|
|
self.rotation += PLAYER_TURN_SPEED * dt
|
|
|
|
def move(self, dt):
|
|
forward = pygame.Vector2(0, 1).rotate(self.rotation)
|
|
self.position += forward * PLAYER_SPEED * dt
|
|
|
|
def shoot(self):
|
|
if self.shot_cooldown_counter > 0:
|
|
return
|
|
shot = Shot(self.position[0], self.position[1])
|
|
shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOT_SPEED
|
|
self.shot_cooldown_counter = PLAYER_SHOOT_COOLDOWN
|
|
|
|
|
|
def triangle(self):
|
|
forward = pygame.Vector2(0, 1).rotate(self.rotation)
|
|
right = pygame.Vector2(0, 1).rotate(self.rotation + 90) * self.radius / 1.5
|
|
a = self.position + forward * self.radius
|
|
b = self.position - forward * self.radius - right
|
|
c = self.position - forward * self.radius + right
|
|
return [a, b, c]
|