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..5cec962 100644 --- a/constants.py +++ b/constants.py @@ -1,6 +1,8 @@ SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720 +PLAYER_RADIUS = 20 +PLAYER_TURN_SPEED = 300 ASTEROID_MIN_RADIUS = 20 ASTEROID_KINDS = 3 ASTEROID_SPAWN_RATE = 0.8 # seconds diff --git a/main.py b/main.py index 92f5eef..52e7179 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import pygame from constants import * +from player import Player def main(): print("Starting asteroids!") @@ -7,11 +8,18 @@ def main(): print(f"Screen height: {SCREEN_HEIGHT}") pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) + clock = pygame.time.Clock() + dt = 0 + p1 = Player(SCREEN_WIDTH/2,SCREEN_HEIGHT/2) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return - screen.fill((0, 0, 0)) # simplified fill call - pygame.display.flip() # fixed typo in flip + screen.fill((0, 0, 0)) + p1.draw(screen) + pygame.display.flip() + + tick = clock.tick(60) + dt = tick/1000 if __name__ == "__main__": main() diff --git a/player.py b/player.py new file mode 100644 index 0000000..1b5824a --- /dev/null +++ b/player.py @@ -0,0 +1,23 @@ +import circleshape +import constants +import pygame + +class Player(circleshape.CircleShape): + def __init__(self,x,y): + super().__init__(x,y,constants.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,(255,255,255),self.triangle(),2) + + + def rotate(self,dt): +