drawing player

This commit is contained in:
2025-07-04 20:58:34 +02:00
parent 73309cb310
commit adad70eaa5
5 changed files with 51 additions and 1 deletions

0
__init__.py Normal file
View File

22
circleshape.py Normal file
View File

@@ -0,0 +1,22 @@
import pygame
# Base class for game objects
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

@@ -1,6 +1,6 @@
SCREEN_WIDTH = 1280 SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720 SCREEN_HEIGHT = 720
PLAYER_RADIUS = 20
ASTEROID_MIN_RADIUS = 20 ASTEROID_MIN_RADIUS = 20
ASTEROID_KINDS = 3 ASTEROID_KINDS = 3
ASTEROID_SPAWN_RATE = 0.8 # seconds ASTEROID_SPAWN_RATE = 0.8 # seconds

View File

@@ -2,6 +2,7 @@ import pygame
import sys import sys
from constants import * from constants import *
from player import Player
def main(): def main():
print("Starting Asteroids!") print("Starting Asteroids!")
@@ -19,12 +20,17 @@ def main():
time = pygame.time.Clock() time = pygame.time.Clock()
dt = 0 dt = 0
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
return return
pygame.Surface.fill(screen, (0,0,0)) pygame.Surface.fill(screen, (0,0,0))
player.draw(screen)
pygame.display.flip() #refresh screen pygame.display.flip() #refresh screen
dt = time.tick(60) / 1000 #conveted to ms dt = time.tick(60) / 1000 #conveted to ms

22
player.py Normal file
View File

@@ -0,0 +1,22 @@
import pygame
from circleshape import CircleShape
from constants import PLAYER_RADIUS
class Player(CircleShape):
def __init__(self, x, y):
super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0
# in the player class
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, (255,255,255), self.triangle(), 2)