From b78a164429abf8840409674f427873af4e91f4a7 Mon Sep 17 00:00:00 2001 From: htamas1210 Date: Sun, 13 Jul 2025 19:00:57 +0200 Subject: [PATCH] shooting --- constants.py | 2 ++ main.py | 3 +++ player.py | 14 +++++++++----- shot.py | 12 ++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 shot.py diff --git a/constants.py b/constants.py index a8a6be5..772ad8c 100644 --- a/constants.py +++ b/constants.py @@ -7,3 +7,5 @@ ASTEROID_SPAWN_RATE = 0.8 # seconds ASTEROID_MAX_RADIUS = ASTEROID_MIN_RADIUS * ASTEROID_KINDS PLAYER_TURN_SPEED = 300 PLAYER_SPEED = 200 +PLAYER_SHOOT_SPEED = 500 +SHOT_RADIUS = 5 diff --git a/main.py b/main.py index fa4651b..a405988 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ from asteroid import Asteroid from asteroidfield import AsteroidField from constants import * from player import Player +from shot import Shot def main(): print("Starting Asteroids!") @@ -25,10 +26,12 @@ 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) Player.containers = (updatable, drawable) + Shot.containers = (shots, updatable, drawable) player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) asteroidField = AsteroidField() diff --git a/player.py b/player.py index b114b97..edea4bf 100644 --- a/player.py +++ b/player.py @@ -1,11 +1,9 @@ import pygame 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): - #containers = [] - def __init__(self, x, y): super().__init__(x, y, PLAYER_RADIUS) self.rotation = 0 @@ -22,7 +20,6 @@ class Player(CircleShape): def draw(self, screen): pygame.draw.polygon(screen, (255,255,255), self.triangle(), 2) - def rotate(self, dt): self.rotation += PLAYER_TURN_SPEED * dt @@ -41,6 +38,13 @@ class Player(CircleShape): if keys[pygame.K_s]: self.move(-dt) + if keys[pygame.K_SPACE]: + self.shoot(dt) + def move(self, dt): forward = pygame.Vector2(0, 1).rotate(self.rotation) 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 diff --git a/shot.py b/shot.py new file mode 100644 index 0000000..56312f5 --- /dev/null +++ b/shot.py @@ -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