From f08ce544560afa32b884f05590081b4009dc8689 Mon Sep 17 00:00:00 2001 From: htamas1210 Date: Mon, 14 Jul 2025 16:19:26 +0200 Subject: [PATCH] splitting asteroids, game done --- asteroid.py | 23 +++++++++++++++++++++++ constants.py | 1 + main.py | 5 +++++ player.py | 10 +++++++++- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/asteroid.py b/asteroid.py index 0fccc00..2731f8c 100644 --- a/asteroid.py +++ b/asteroid.py @@ -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 + + + diff --git a/constants.py b/constants.py index 772ad8c..d261f18 100644 --- a/constants.py +++ b/constants.py @@ -9,3 +9,4 @@ PLAYER_TURN_SPEED = 300 PLAYER_SPEED = 200 PLAYER_SHOOT_SPEED = 500 SHOT_RADIUS = 5 +PLAYER_SHOOT_COOLDOWN = 0.3 diff --git a/main.py b/main.py index a405988..f2dc310 100644 --- a/main.py +++ b/main.py @@ -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) diff --git a/player.py b/player.py index edea4bf..51e3578 100644 --- a/player.py +++ b/player.py @@ -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