From 2fadb9143f267357499062c4bce79d4dc8499c7d Mon Sep 17 00:00:00 2001 From: Florian Schmitt Date: Tue, 8 Oct 2024 20:00:59 +0300 Subject: [PATCH] feat: player triangle and formatter --- README.md | 4 +++- circleshape.py | 22 ++++++++++++++++++++++ constants.py | 1 + main.py | 12 ++++++++++-- player.py | 20 ++++++++++++++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 circleshape.py create mode 100644 player.py diff --git a/README.md b/README.md index 8a7a863..27fe401 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,7 @@ ```bash - +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt ``` diff --git a/circleshape.py b/circleshape.py new file mode 100644 index 0000000..1b96af8 --- /dev/null +++ b/circleshape.py @@ -0,0 +1,22 @@ +import pygame + + +class CircleShape(pygame.sprite.Sprite): + def __init__(self, x, y, radius): + # we will be using this later + if hasattr(self, "containers"): + super().__init__(self.containers) + else: + super().__init__() + + self.position = pygame.Vector2(x, y) + self.velocity = pygame.Vector2(0, 0) + self.radius = radius + + def draw(self, screen): + # sub-classes must override + pass + + def update(self, dt): + # sub-classes must override + pass diff --git a/constants.py b/constants.py index 48312a2..3d15a2e 100644 --- a/constants.py +++ b/constants.py @@ -5,3 +5,4 @@ ASTEROID_MIN_RADIUS = 20 ASTEROID_KINDS = 3 ASTEROID_SPAWN_RATE = 0.8 # seconds ASTEROID_MAX_RADIUS = ASTEROID_MIN_RADIUS * ASTEROID_KINDS +PLAYER_RADIUS = 20 diff --git a/main.py b/main.py index 8ab8942..8b8d371 100644 --- a/main.py +++ b/main.py @@ -1,18 +1,26 @@ import pygame +from player import Player from constants import * + def main(): - print('Starting asteroids!') + print("Starting asteroids!") print(f"Screen width: {SCREEN_WIDTH}") print(f"Screen height: {SCREEN_HEIGHT}") pygame.init() + clock = pygame.time.Clock() + dt = 0 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) + player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return - pygame.Surface.fill(screen, (0,0,0)) + pygame.Surface.fill(screen, (0, 0, 0)) + player.draw(screen) pygame.display.flip() + t = clock.tick(60) + dt = t / 1000 if __name__ == "__main__": diff --git a/player.py b/player.py new file mode 100644 index 0000000..89be933 --- /dev/null +++ b/player.py @@ -0,0 +1,20 @@ +import pygame +from circleshape import CircleShape +from constants import * + + +class Player(CircleShape): + def __init__(self, x, y): + super().__init__(x, y, PLAYER_RADIUS) + self.rotation = 0 + + 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] + + def draw(self, screen): + pygame.draw.polygon(screen, "white", self.triangle(), 2)