From a00f4cf1eb4217483408dde11d9d0c7e763ab0a2 Mon Sep 17 00:00:00 2001 From: yuzu-eva Date: Fri, 20 Sep 2024 18:25:40 +0200 Subject: added ability to shoot --- asteroid.py | 2 +- circleshape.py | 3 +++ constants.py | 3 +++ main.py | 8 ++++++++ player.py | 6 ++++++ shot.py | 13 +++++++++++++ 6 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 shot.py diff --git a/asteroid.py b/asteroid.py index c77618c..3ab16d9 100644 --- a/asteroid.py +++ b/asteroid.py @@ -5,7 +5,7 @@ class Asteroid(CircleShape): super().__init__(x, y, radius) def draw(self, screen): - pygame.draw.circle(screen, "white", self.position, 2) + pygame.draw.circle(screen, "white", self.position, self.radius, 2) def update(self, dt): self.position += self.velocity * dt diff --git a/circleshape.py b/circleshape.py index eb08882..0093b29 100644 --- a/circleshape.py +++ b/circleshape.py @@ -20,3 +20,6 @@ class CircleShape(pygame.sprite.Sprite): def update(self, dt): # sub-classes must override pass + + def collision_check(self, other): + return self.position.distance_to(other.position) <= self.radius + other.radius diff --git a/constants.py b/constants.py index 020eeab..87070cd 100644 --- a/constants.py +++ b/constants.py @@ -9,3 +9,6 @@ ASTEROID_MAX_RADIUS = ASTEROID_MIN_RADIUS * ASTEROID_KINDS PLAYER_RADIUS = 20 PLAYER_TURN_SPEED = 300 PLAYER_SPEED = 200 +PLAYER_SHOOT_SPEED = 500 + +SHOT_RADIUS = 5 diff --git a/main.py b/main.py index 43645dc..6081f2a 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from circleshape import * from player import * from asteroid import * from asteroidfield import * +from shot import * def main(): pygame.init() @@ -13,6 +14,7 @@ def main(): updatable = pygame.sprite.Group() drawable = pygame.sprite.Group() asteroids = pygame.sprite.Group() + shots = pygame.sprite.Group() Asteroid.containers = (asteroids, updatable, drawable) AsteroidField.containers = (updatable) @@ -22,6 +24,8 @@ def main(): Player.containers = (updatable, drawable) player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) + Shot.containers = (shots, updatable, drawable) + dt = 0 while True: @@ -31,6 +35,10 @@ def main(): screen.fill("black") for obj in updatable: obj.update(dt) + for asteroid in asteroids: + if asteroid.collision_check(player): + print("Game over!") + return for obj in drawable: obj.draw(screen) pygame.display.flip() diff --git a/player.py b/player.py index 339b8d1..9b7f4bc 100644 --- a/player.py +++ b/player.py @@ -1,6 +1,7 @@ import pygame from constants import * from circleshape import * +from shot import * class Player(CircleShape): def __init__(self, x, y): @@ -32,8 +33,13 @@ class Player(CircleShape): self.move(dt) if keys[pygame.K_DOWN]: self.move(-dt) + if keys[pygame.K_SPACE]: + self.shoot(self.position) def move(self, dt): forward = pygame.Vector2(0, 1).rotate(self.rotation) self.position += forward * PLAYER_SPEED * dt + def shoot(self, position): + shot = Shot(position.x, position.y, SHOT_RADIUS) + shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOOT_SPEED diff --git a/shot.py b/shot.py new file mode 100644 index 0000000..198b48e --- /dev/null +++ b/shot.py @@ -0,0 +1,13 @@ +from constants import * +from circleshape import * + +class Shot(CircleShape): + def __init__(self, x, y, radius): + super().__init__(x, y, radius) + + def draw(self, screen): + pygame.draw.circle(screen, "white", self.position, self.radius) + + def update(self, dt): + self.position += self.velocity * dt + -- cgit v1.2.3