shooting
This commit is contained in:
@@ -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
|
||||
|
||||
3
main.py
3
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()
|
||||
|
||||
14
player.py
14
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
|
||||
|
||||
12
shot.py
Normal file
12
shot.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user