Adding in bullets

This commit is contained in:
franky212
2024-10-28 21:10:08 -06:00
parent ae27a3c7a0
commit 48acf7759a
4 changed files with 32 additions and 1 deletions

View File

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

View File

@@ -6,6 +6,7 @@ from constants import SCREEN_HEIGHT, SCREEN_WIDTH
from player import Player from player import Player
from asteroid import Asteroid from asteroid import Asteroid
from asteroidfield import AsteroidField from asteroidfield import AsteroidField
from shot import Shot
def main(): def main():
pygame.init() pygame.init()
@@ -16,9 +17,11 @@ def main():
asteroids = pygame.sprite.Group() asteroids = pygame.sprite.Group()
updatable = pygame.sprite.Group() updatable = pygame.sprite.Group()
drawable = pygame.sprite.Group() drawable = pygame.sprite.Group()
shots = pygame.sprite.Group()
Asteroid.containers = (asteroids, updatable, drawable) Asteroid.containers = (asteroids, updatable, drawable)
AsteroidField.containers = updatable AsteroidField.containers = updatable
Shot.containers = (shots, updatable, drawable)
asteroid_field = AsteroidField() asteroid_field = AsteroidField()
Player.containers = (updatable, drawable) Player.containers = (updatable, drawable)

View File

@@ -1,11 +1,13 @@
import pygame import pygame
from circleshape import CircleShape 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): class Player(CircleShape):
def __init__(self, x, y): def __init__(self, x, y):
super().__init__(x, y, PLAYER_RADIUS) super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0 self.rotation = 0
self.timer = 0
def rotate(self, dt): def rotate(self, dt):
self.rotation += PLAYER_TURN_SPEED * dt self.rotation += PLAYER_TURN_SPEED * dt
@@ -25,6 +27,16 @@ class Player(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()
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 # in the player class
def triangle(self): def triangle(self):

13
shot.py Normal file
View File

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