This commit is contained in:
2025-07-13 19:00:57 +02:00
parent 01a298e864
commit b78a164429
4 changed files with 26 additions and 5 deletions

View File

@@ -7,3 +7,5 @@ ASTEROID_SPAWN_RATE = 0.8 # seconds
ASTEROID_MAX_RADIUS = ASTEROID_MIN_RADIUS * ASTEROID_KINDS ASTEROID_MAX_RADIUS = ASTEROID_MIN_RADIUS * ASTEROID_KINDS
PLAYER_TURN_SPEED = 300 PLAYER_TURN_SPEED = 300
PLAYER_SPEED = 200 PLAYER_SPEED = 200
PLAYER_SHOOT_SPEED = 500
SHOT_RADIUS = 5

View File

@@ -5,6 +5,7 @@ from asteroid import Asteroid
from asteroidfield import AsteroidField from asteroidfield import AsteroidField
from constants import * from constants import *
from player import Player from player import Player
from shot import Shot
def main(): def main():
print("Starting Asteroids!") print("Starting Asteroids!")
@@ -25,10 +26,12 @@ def main():
updatable = pygame.sprite.Group() updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group() drawable = pygame.sprite.Group()
asteroids = pygame.sprite.Group() asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
Asteroid.containers = (asteroids, updatable, drawable) Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = (updatable) AsteroidField.containers = (updatable)
Player.containers = (updatable, drawable) Player.containers = (updatable, drawable)
Shot.containers = (shots, updatable, drawable)
player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
asteroidField = AsteroidField() asteroidField = AsteroidField()

View File

@@ -1,11 +1,9 @@
import pygame import pygame
from circleshape import CircleShape from circleshape import CircleShape
from constants import PLAYER_RADIUS, PLAYER_SPEED, PLAYER_TURN_SPEED from constants import PLAYER_RADIUS, PLAYER_SHOOT_SPEED, PLAYER_SPEED, PLAYER_TURN_SPEED, SHOT_RADIUS
from shot import Shot
class Player(CircleShape): class Player(CircleShape):
#containers = []
def __init__(self, x, y): def __init__(self, x, y):
super().__init__(x, y, PLAYER_RADIUS) super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0 self.rotation = 0
@@ -22,7 +20,6 @@ class Player(CircleShape):
def draw(self, screen): def draw(self, screen):
pygame.draw.polygon(screen, (255,255,255), self.triangle(), 2) pygame.draw.polygon(screen, (255,255,255), self.triangle(), 2)
def rotate(self, dt): def rotate(self, dt):
self.rotation += PLAYER_TURN_SPEED * dt self.rotation += PLAYER_TURN_SPEED * dt
@@ -41,6 +38,13 @@ class Player(CircleShape):
if keys[pygame.K_s]: if keys[pygame.K_s]:
self.move(-dt) self.move(-dt)
if keys[pygame.K_SPACE]:
self.shoot(dt)
def move(self, dt): def move(self, dt):
forward = pygame.Vector2(0, 1).rotate(self.rotation) forward = pygame.Vector2(0, 1).rotate(self.rotation)
self.position += forward * PLAYER_SPEED * dt self.position += forward * PLAYER_SPEED * dt
def shoot(self, dt):
shot = Shot(self.position.x, self.position.y, SHOT_RADIUS)
shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOOT_SPEED

12
shot.py Normal file
View File

@@ -0,0 +1,12 @@
import pygame
from circleshape import CircleShape
class Shot(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