From f0d7ef7cce1664ded11ac60abd9616d98eef24f8 Mon Sep 17 00:00:00 2001 From: specCon18 Date: Thu, 16 Jan 2025 11:37:57 -0500 Subject: [PATCH] added astroids and astroid field --- asteroid.py | 16 +++++++++++++++ asteroidfield.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 20 +++++++++++++++++++ out.txt | 2 ++ 4 files changed, 89 insertions(+) create mode 100644 asteroid.py create mode 100644 asteroidfield.py create mode 100644 out.txt diff --git a/asteroid.py b/asteroid.py new file mode 100644 index 0000000..f97b5e5 --- /dev/null +++ b/asteroid.py @@ -0,0 +1,16 @@ +import circleshape +import pygame +import constants + +class Asteroid(circleshape.CircleShape): + + containers = [] + + def __init__(self, x, y, radius): + super().__init__(x,y,radius) + + def draw(self,screen): + pygame.draw.circle(screen,(255,255,255),self.position,self.radius,2) + + def update(self, dt): + self.position += self.velocity * dt diff --git a/asteroidfield.py b/asteroidfield.py new file mode 100644 index 0000000..da82d41 --- /dev/null +++ b/asteroidfield.py @@ -0,0 +1,51 @@ +import pygame +import random +from asteroid import Asteroid +from constants import * + + +class AsteroidField(pygame.sprite.Sprite): + edges = [ + [ + pygame.Vector2(1, 0), + lambda y: pygame.Vector2(-ASTEROID_MAX_RADIUS, y * SCREEN_HEIGHT), + ], + [ + pygame.Vector2(-1, 0), + lambda y: pygame.Vector2( + SCREEN_WIDTH + ASTEROID_MAX_RADIUS, y * SCREEN_HEIGHT + ), + ], + [ + pygame.Vector2(0, 1), + lambda x: pygame.Vector2(x * SCREEN_WIDTH, -ASTEROID_MAX_RADIUS), + ], + [ + pygame.Vector2(0, -1), + lambda x: pygame.Vector2( + x * SCREEN_WIDTH, SCREEN_HEIGHT + ASTEROID_MAX_RADIUS + ), + ], + ] + + def __init__(self): + pygame.sprite.Sprite.__init__(self, self.containers) + self.spawn_timer = 0.0 + + def spawn(self, radius, position, velocity): + asteroid = Asteroid(position.x, position.y, radius) + asteroid.velocity = velocity + + def update(self, dt): + self.spawn_timer += dt + if self.spawn_timer > ASTEROID_SPAWN_RATE: + self.spawn_timer = 0 + + # spawn a new asteroid at a random edge + edge = random.choice(self.edges) + speed = random.randint(40, 100) + velocity = edge[0] * speed + velocity = velocity.rotate(random.randint(-30, 30)) + position = edge[1](random.uniform(0, 1)) + kind = random.randint(1, ASTEROID_KINDS) + self.spawn(ASTEROID_MIN_RADIUS * kind, position, velocity) diff --git a/main.py b/main.py index 1cbede8..f65a729 100644 --- a/main.py +++ b/main.py @@ -1,26 +1,46 @@ import pygame from constants import * from player import Player +from asteroidfield import AsteroidField +from asteroid import Asteroid def main(): + + # Print basic starting info print("Starting asteroids!") print(f"Screen width: {SCREEN_WIDTH}") print(f"Screen height: {SCREEN_HEIGHT}") + + # Initialize game, screen, game-clock and delta time counter pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) clock = pygame.time.Clock() dt = 0 + # Groups drawables = pygame.sprite.Group() updatables = pygame.sprite.Group() + asteroids = pygame.sprite.Group() + # Set the containers of the astroids + Asteroid.containers = (asteroids,drawables,updatables) + + # Set the containers of the astroid_field + AsteroidField.containers = (updatables) + af = AsteroidField() + + # Set the containers of player Player.containers = (drawables, updatables) + # Set the position of the player p1 = Player(SCREEN_WIDTH/2,SCREEN_HEIGHT/2) while True: + # if event is quit then kill the game for event in pygame.event.get(): if event.type == pygame.QUIT: return + + # fill the screen with the void of space screen.fill((0, 0, 0)) for drawable in drawables: diff --git a/out.txt b/out.txt new file mode 100644 index 0000000..ef7c89e --- /dev/null +++ b/out.txt @@ -0,0 +1,2 @@ +MESA: error: ZINK: failed to choose pdev +glx: failed to create drisw screen