feat: player triangle and formatter

This commit is contained in:
mrflos 2024-10-08 20:00:59 +03:00
parent 1e74245d86
commit 2fadb9143f
5 changed files with 56 additions and 3 deletions

View file

@ -4,5 +4,7 @@
```bash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```

22
circleshape.py Normal file
View file

@ -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

View file

@ -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

12
main.py
View file

@ -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__":

20
player.py Normal file
View file

@ -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)