added astroids and astroid field

This commit is contained in:
specCon18 2025-01-16 11:37:57 -05:00
parent 9365e50c64
commit f0d7ef7cce
4 changed files with 89 additions and 0 deletions

16
asteroid.py Normal file
View file

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

51
asteroidfield.py Normal file
View file

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

20
main.py
View file

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

2
out.txt Normal file
View file

@ -0,0 +1,2 @@
MESA: error: ZINK: failed to choose pdev
glx: failed to create drisw screen