added shooting

This commit is contained in:
specCon18 2025-01-21 02:54:02 -05:00
parent 7b27ac29e8
commit cf21f5ff0a
4 changed files with 38 additions and 1 deletions

View file

@ -4,6 +4,9 @@ SCREEN_HEIGHT = 720
PLAYER_RADIUS = 20 PLAYER_RADIUS = 20
PLAYER_ROTATION_SPEED = 300 PLAYER_ROTATION_SPEED = 300
PLAYER_SPEED = 200 PLAYER_SPEED = 200
PLAYER_SHOOT_SPEED = 500
SHOT_RADIUS = 5
ASTEROID_MIN_RADIUS = 20 ASTEROID_MIN_RADIUS = 20
ASTEROID_KINDS = 3 ASTEROID_KINDS = 3

View file

@ -4,6 +4,7 @@ from constants import *
from player import Player from player import Player
from asteroidfield import AsteroidField from asteroidfield import AsteroidField
from asteroid import Asteroid from asteroid import Asteroid
from shot import Shot
def main(): def main():
@ -22,6 +23,7 @@ def main():
drawables = pygame.sprite.Group() drawables = pygame.sprite.Group()
updatables = pygame.sprite.Group() updatables = pygame.sprite.Group()
asteroids = pygame.sprite.Group() asteroids = pygame.sprite.Group()
shots = pygame.sprite.Group()
# Set the containers of the astroids # Set the containers of the astroids
Asteroid.containers = (asteroids,drawables,updatables) Asteroid.containers = (asteroids,drawables,updatables)
@ -35,6 +37,7 @@ def main():
# Set the position of the player # Set the position of the player
p1 = Player(SCREEN_WIDTH/2,SCREEN_HEIGHT/2) p1 = Player(SCREEN_WIDTH/2,SCREEN_HEIGHT/2)
Shot.containers = (shots,drawables,updatables)
while True: while True:
# if event is quit then kill the game # if event is quit then kill the game
for event in pygame.event.get(): for event in pygame.event.get():

View file

@ -1,6 +1,7 @@
import circleshape import circleshape
import constants import constants
import pygame import pygame
from shot import Shot
class Player(circleshape.CircleShape): class Player(circleshape.CircleShape):
@ -39,6 +40,20 @@ class Player(circleshape.CircleShape):
self.move(dt) self.move(dt)
if keys[pygame.K_s]: if keys[pygame.K_s]:
self.move(-dt) self.move(-dt)
if keys[pygame.K_SPACE]:
self.shoot()
def move(self,dt): def move(self,dt):
forward = pygame.Vector2(0, 1).rotate(self.rotation) 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)

16
shot.py Normal file
View file

@ -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