From adad70eaa55ad8fd02c75ff53b962ee408398afb Mon Sep 17 00:00:00 2001 From: htamas1210 Date: Fri, 4 Jul 2025 20:58:34 +0200 Subject: [PATCH] drawing player --- __init__.py | 0 circleshape.py | 22 ++++++++++++++++++++++ constants.py | 2 +- main.py | 6 ++++++ player.py | 22 ++++++++++++++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 __init__.py create mode 100644 circleshape.py create mode 100644 player.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/circleshape.py b/circleshape.py new file mode 100644 index 0000000..eb08882 --- /dev/null +++ b/circleshape.py @@ -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 diff --git a/constants.py b/constants.py index 48312a2..9ac14a7 100644 --- a/constants.py +++ b/constants.py @@ -1,6 +1,6 @@ SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720 - +PLAYER_RADIUS = 20 ASTEROID_MIN_RADIUS = 20 ASTEROID_KINDS = 3 ASTEROID_SPAWN_RATE = 0.8 # seconds diff --git a/main.py b/main.py index 0f12299..7f7c14d 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import pygame import sys from constants import * +from player import Player def main(): print("Starting Asteroids!") @@ -19,12 +20,17 @@ def main(): time = pygame.time.Clock() dt = 0 + 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)) + + player.draw(screen) + pygame.display.flip() #refresh screen dt = time.tick(60) / 1000 #conveted to ms diff --git a/player.py b/player.py new file mode 100644 index 0000000..520ad3a --- /dev/null +++ b/player.py @@ -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) +