feat: complete the game

This commit is contained in:
Stevan Freeborn
2026-05-22 09:34:56 -05:00
parent 6f9672beb5
commit 2ad2dfb6f8
9 changed files with 317 additions and 2 deletions
+60
View File
@@ -0,0 +1,60 @@
import pygame
from circleshape import CircleShape
from constants import (LINE_WIDTH, PLAYER_RADIUS, PLAYER_SHOOT_COOLDOWN_SECONDS, PLAYER_SHOOT_SPEED,
PLAYER_SPEED, PLAYER_TURN_SPEED, SHOT_RADIUS)
from shot import Shot
class Player(CircleShape):
def __init__(self, x, y) -> None:
super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0
self.cooldown = 0
def triangle(self) -> list[pygame.Vector2]:
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]
def draw(self, screen: pygame.Surface) -> None:
pygame.draw.polygon(screen, "white", self.triangle(), LINE_WIDTH)
def rotate(self, dt: float) -> None:
self.rotation += PLAYER_TURN_SPEED * dt
def update(self, dt: float) -> None:
self.cooldown -= 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_s]:
self.move(dt)
if keys[pygame.K_w]:
self.move(-dt)
if keys[pygame.K_SPACE]:
self.shoot()
def move(self, dt: float) -> None:
unit_vector = pygame.Vector2(0, 1)
rotated_vector = unit_vector.rotate(self.rotation)
rotated_with_speed_vector = rotated_vector * PLAYER_SPEED * dt
self.position += rotated_with_speed_vector
def shoot(self):
if self.cooldown > 0:
return
s = Shot(self.position.x, self.position.y, SHOT_RADIUS)
s.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOOT_SPEED
self.cooldown = PLAYER_SHOOT_COOLDOWN_SECONDS