From cf21f5ff0a462ab574b2f34e7a96b32563f514e2 Mon Sep 17 00:00:00 2001 From: specCon18 Date: Tue, 21 Jan 2025 02:54:02 -0500 Subject: [PATCH] added shooting --- constants.py | 3 +++ main.py | 3 +++ player.py | 17 ++++++++++++++++- shot.py | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 shot.py diff --git a/constants.py b/constants.py index 41441c9..445461a 100644 --- a/constants.py +++ b/constants.py @@ -4,6 +4,9 @@ SCREEN_HEIGHT = 720 PLAYER_RADIUS = 20 PLAYER_ROTATION_SPEED = 300 PLAYER_SPEED = 200 +PLAYER_SHOOT_SPEED = 500 + +SHOT_RADIUS = 5 ASTEROID_MIN_RADIUS = 20 ASTEROID_KINDS = 3 diff --git a/main.py b/main.py index 6418f7c..cd5197e 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from constants import * from player import Player from asteroidfield import AsteroidField from asteroid import Asteroid +from shot import Shot def main(): @@ -22,6 +23,7 @@ def main(): drawables = pygame.sprite.Group() updatables = pygame.sprite.Group() asteroids = pygame.sprite.Group() + shots = pygame.sprite.Group() # Set the containers of the astroids Asteroid.containers = (asteroids,drawables,updatables) @@ -35,6 +37,7 @@ def main(): # Set the position of the player p1 = Player(SCREEN_WIDTH/2,SCREEN_HEIGHT/2) + Shot.containers = (shots,drawables,updatables) while True: # if event is quit then kill the game for event in pygame.event.get(): diff --git a/player.py b/player.py index 512b0ee..a15efa0 100644 --- a/player.py +++ b/player.py @@ -1,6 +1,7 @@ import circleshape import constants import pygame +from shot import Shot class Player(circleshape.CircleShape): @@ -39,6 +40,20 @@ class Player(circleshape.CircleShape): self.move(dt) if keys[pygame.K_s]: self.move(-dt) + if keys[pygame.K_SPACE]: + self.shoot() + def move(self,dt): forward = pygame.Vector2(0, 1).rotate(self.rotation) - self.position += forward * constants.PLAYER_SPEED * dt + self.position += forward * constants.PLAYER_SPEED * dt + + def shoot(self): + # Create new shot at player position + shot = Shot(self.position.x, self.position.y) + + # Calculate velocity (similar to your move method) + forward = pygame.Vector2(0, 1).rotate(self.rotation) + shot.velocity = forward * constants.PLAYER_SHOOT_SPEED + + # Add to container + Shot.containers[0].add(shot) diff --git a/shot.py b/shot.py new file mode 100644 index 0000000..8573045 --- /dev/null +++ b/shot.py @@ -0,0 +1,16 @@ +import circleshape +import pygame +import constants + +class Shot(circleshape.CircleShape): + + containers = [] + + def __init__(self, x, y): + super().__init__(x,y,constants.SHOT_RADIUS) + + def draw(self,screen): + pygame.draw.circle(screen,(255,255,255),self.position,self.radius,2) + + def update(self, dt): + self.position += self.velocity * dt