splitting asteroids, game done

This commit is contained in:
2025-07-14 16:19:26 +02:00
parent b78a164429
commit f08ce54456
4 changed files with 38 additions and 1 deletions

View File

@@ -1,5 +1,8 @@
from circleshape import CircleShape
import pygame
import random
from constants import ASTEROID_MIN_RADIUS
class Asteroid(CircleShape):
def __init__(self, x, y, radius):
@@ -11,3 +14,23 @@ class Asteroid(CircleShape):
def update(self, dt):
self.position += self.velocity * dt
def split(self):
self.kill()
if self.radius <= ASTEROID_MIN_RADIUS:
return
else:
angle = random.uniform(20, 50)
rand_angle1 = self.position.rotate(angle)
rand_angle2 = self.position.rotate(-angle)
new_radius = self.radius - ASTEROID_MIN_RADIUS
asteroid1 = Asteroid(self.position.x, self.position.y, new_radius)
asteroid1.velocity = rand_angle1 * 1.2
asteroid2 = Asteroid(self.position.x, self.position.y, new_radius)
asteroid2.velocity = rand_angle2 * 1.2

View File

@@ -9,3 +9,4 @@ PLAYER_TURN_SPEED = 300
PLAYER_SPEED = 200
PLAYER_SHOOT_SPEED = 500
SHOT_RADIUS = 5
PLAYER_SHOOT_COOLDOWN = 0.3

View File

@@ -49,6 +49,11 @@ def main():
if asteroid.is_colliding(player):
print("Game Over")
sys.exit(0)
for shot in shots:
if asteroid.is_colliding(shot):
asteroid.split()
shot.kill()
break
for drawing in drawable:
drawing.draw(screen)

View File

@@ -1,12 +1,13 @@
import pygame
from circleshape import CircleShape
from constants import PLAYER_RADIUS, PLAYER_SHOOT_SPEED, PLAYER_SPEED, PLAYER_TURN_SPEED, SHOT_RADIUS
from constants import PLAYER_RADIUS, PLAYER_SHOOT_COOLDOWN, PLAYER_SHOOT_SPEED, PLAYER_SPEED, PLAYER_TURN_SPEED, SHOT_RADIUS
from shot import Shot
class Player(CircleShape):
def __init__(self, x, y):
super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0
self.timer = 0
# in the player class
def triangle(self):
@@ -24,6 +25,8 @@ class Player(CircleShape):
self.rotation += PLAYER_TURN_SPEED * dt
def update(self, dt):
self.timer -= dt
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
@@ -46,5 +49,10 @@ class Player(CircleShape):
self.position += forward * PLAYER_SPEED * dt
def shoot(self, dt):
if self.timer > 0:
return
shot = Shot(self.position.x, self.position.y, SHOT_RADIUS)
shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOOT_SPEED
self.timer = PLAYER_SHOOT_COOLDOWN