From 48acf7759aa6588464d0fbe1fb4b8e6a36c694d0 Mon Sep 17 00:00:00 2001 From: franky212 Date: Mon, 28 Oct 2024 21:10:08 -0600 Subject: [PATCH] Adding in bullets --- constants.py | 3 +++ main.py | 3 +++ player.py | 14 +++++++++++++- shot.py | 13 +++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 shot.py diff --git a/constants.py b/constants.py index 39f2bb0..4222896 100644 --- a/constants.py +++ b/constants.py @@ -4,6 +4,9 @@ SCREEN_HEIGHT = 720 PLAYER_RADIUS = 20 PLAYER_TURN_SPEED = 300 PLAYER_SPEED = 200 +PLAYER_SHOOT_SPEED = 500 +PLAYER_SHOOT_COOLDOWN = 0.3 +SHOT_RADIUS = 5 ASTEROID_MIN_RADIUS = 20 ASTEROID_KINDS = 3 diff --git a/main.py b/main.py index f72b6ea..a28db09 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from constants import SCREEN_HEIGHT, SCREEN_WIDTH from player import Player from asteroid import Asteroid from asteroidfield import AsteroidField +from shot import Shot def main(): pygame.init() @@ -16,9 +17,11 @@ def main(): asteroids = pygame.sprite.Group() updatable = pygame.sprite.Group() drawable = pygame.sprite.Group() + shots = pygame.sprite.Group() Asteroid.containers = (asteroids, updatable, drawable) AsteroidField.containers = updatable + Shot.containers = (shots, updatable, drawable) asteroid_field = AsteroidField() Player.containers = (updatable, drawable) diff --git a/player.py b/player.py index b785cd5..78ecf7c 100644 --- a/player.py +++ b/player.py @@ -1,11 +1,13 @@ import pygame from circleshape import CircleShape -from constants import PLAYER_RADIUS, PLAYER_TURN_SPEED, PLAYER_SPEED +from shot import Shot +from constants import PLAYER_RADIUS, PLAYER_TURN_SPEED, PLAYER_SPEED, PLAYER_SHOOT_SPEED, PLAYER_SHOOT_COOLDOWN class Player(CircleShape): def __init__(self, x, y): super().__init__(x, y, PLAYER_RADIUS) self.rotation = 0 + self.timer = 0 def rotate(self, dt): self.rotation += PLAYER_TURN_SPEED * dt @@ -25,6 +27,16 @@ class Player(CircleShape): self.move(dt) if keys[pygame.K_s]: self.move(-dt) + if keys[pygame.K_SPACE]: + self.shoot() + + self.timer -= dt + + def shoot(self): + if self.timer > 0: + return + self.timer = PLAYER_SHOOT_COOLDOWN + Shot(self.position.x, self.position.y, (pygame.Vector2(0, 1).rotate(self.rotation)) * PLAYER_SHOOT_SPEED) # in the player class def triangle(self): diff --git a/shot.py b/shot.py new file mode 100644 index 0000000..996896a --- /dev/null +++ b/shot.py @@ -0,0 +1,13 @@ +import pygame +from circleshape import CircleShape +from constants import SHOT_RADIUS +class Shot(CircleShape): + def __init__(self, x, y, velocity): + super().__init__(x, y, SHOT_RADIUS) + self.velocity = velocity + + def draw(self, screen): + pygame.draw.circle(screen, "white", self.position, self.radius, 2) + + def update(self, dt): + self.position += (self.velocity * dt) \ No newline at end of file