asteroids render
This commit is contained in:
13
asteroid.py
Normal file
13
asteroid.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from circleshape import CircleShape
|
||||
import pygame
|
||||
|
||||
class Asteroid(CircleShape):
|
||||
def __init__(self, x, y, radius):
|
||||
super().__init__(x,y, radius)
|
||||
|
||||
def draw(self, screen):
|
||||
pygame.draw.circle(screen, (255,255,255), (self.position.x, self.position.y), self.radius)
|
||||
|
||||
|
||||
def update(self, dt):
|
||||
self.position += self.velocity * dt
|
||||
51
asteroidfield.py
Normal file
51
asteroidfield.py
Normal 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)
|
||||
6
main.py
6
main.py
@@ -1,6 +1,8 @@
|
||||
import pygame
|
||||
import sys
|
||||
|
||||
from asteroid import Asteroid
|
||||
from asteroidfield import AsteroidField
|
||||
from constants import *
|
||||
from player import Player
|
||||
|
||||
@@ -22,10 +24,14 @@ def main():
|
||||
|
||||
updatable = pygame.sprite.Group()
|
||||
drawable = pygame.sprite.Group()
|
||||
asteroids = pygame.sprite.Group()
|
||||
|
||||
Asteroid.containers = (asteroids, updatable, drawable)
|
||||
AsteroidField.containers = (updatable)
|
||||
Player.containers = (updatable, drawable)
|
||||
|
||||
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
|
||||
asteroidField = AsteroidField()
|
||||
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
|
||||
Reference in New Issue
Block a user