splitting asteroids, game done
This commit is contained in:
23
asteroid.py
23
asteroid.py
@@ -1,5 +1,8 @@
|
|||||||
from circleshape import CircleShape
|
from circleshape import CircleShape
|
||||||
import pygame
|
import pygame
|
||||||
|
import random
|
||||||
|
|
||||||
|
from constants import ASTEROID_MIN_RADIUS
|
||||||
|
|
||||||
class Asteroid(CircleShape):
|
class Asteroid(CircleShape):
|
||||||
def __init__(self, x, y, radius):
|
def __init__(self, x, y, radius):
|
||||||
@@ -11,3 +14,23 @@ class Asteroid(CircleShape):
|
|||||||
|
|
||||||
def update(self, dt):
|
def update(self, dt):
|
||||||
self.position += self.velocity * dt
|
self.position += self.velocity * dt
|
||||||
|
|
||||||
|
def split(self):
|
||||||
|
self.kill()
|
||||||
|
|
||||||
|
if self.radius <= ASTEROID_MIN_RADIUS:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
angle = random.uniform(20, 50)
|
||||||
|
rand_angle1 = self.position.rotate(angle)
|
||||||
|
rand_angle2 = self.position.rotate(-angle)
|
||||||
|
|
||||||
|
new_radius = self.radius - ASTEROID_MIN_RADIUS
|
||||||
|
|
||||||
|
asteroid1 = Asteroid(self.position.x, self.position.y, new_radius)
|
||||||
|
asteroid1.velocity = rand_angle1 * 1.2
|
||||||
|
asteroid2 = Asteroid(self.position.x, self.position.y, new_radius)
|
||||||
|
asteroid2.velocity = rand_angle2 * 1.2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ PLAYER_TURN_SPEED = 300
|
|||||||
PLAYER_SPEED = 200
|
PLAYER_SPEED = 200
|
||||||
PLAYER_SHOOT_SPEED = 500
|
PLAYER_SHOOT_SPEED = 500
|
||||||
SHOT_RADIUS = 5
|
SHOT_RADIUS = 5
|
||||||
|
PLAYER_SHOOT_COOLDOWN = 0.3
|
||||||
|
|||||||
5
main.py
5
main.py
@@ -49,6 +49,11 @@ def main():
|
|||||||
if asteroid.is_colliding(player):
|
if asteroid.is_colliding(player):
|
||||||
print("Game Over")
|
print("Game Over")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
for shot in shots:
|
||||||
|
if asteroid.is_colliding(shot):
|
||||||
|
asteroid.split()
|
||||||
|
shot.kill()
|
||||||
|
break
|
||||||
|
|
||||||
for drawing in drawable:
|
for drawing in drawable:
|
||||||
drawing.draw(screen)
|
drawing.draw(screen)
|
||||||
|
|||||||
10
player.py
10
player.py
@@ -1,12 +1,13 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from circleshape import CircleShape
|
from circleshape import CircleShape
|
||||||
from constants import PLAYER_RADIUS, PLAYER_SHOOT_SPEED, PLAYER_SPEED, PLAYER_TURN_SPEED, SHOT_RADIUS
|
from constants import PLAYER_RADIUS, PLAYER_SHOOT_COOLDOWN, PLAYER_SHOOT_SPEED, PLAYER_SPEED, PLAYER_TURN_SPEED, SHOT_RADIUS
|
||||||
from shot import Shot
|
from shot import Shot
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
# in the player class
|
# in the player class
|
||||||
def triangle(self):
|
def triangle(self):
|
||||||
@@ -24,6 +25,8 @@ class Player(CircleShape):
|
|||||||
self.rotation += PLAYER_TURN_SPEED * dt
|
self.rotation += PLAYER_TURN_SPEED * dt
|
||||||
|
|
||||||
def update(self, dt):
|
def update(self, dt):
|
||||||
|
self.timer -= dt
|
||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
if keys[pygame.K_a]:
|
if keys[pygame.K_a]:
|
||||||
@@ -46,5 +49,10 @@ class Player(CircleShape):
|
|||||||
self.position += forward * PLAYER_SPEED * dt
|
self.position += forward * PLAYER_SPEED * dt
|
||||||
|
|
||||||
def shoot(self, dt):
|
def shoot(self, dt):
|
||||||
|
if self.timer > 0:
|
||||||
|
return
|
||||||
|
|
||||||
shot = Shot(self.position.x, self.position.y, SHOT_RADIUS)
|
shot = Shot(self.position.x, self.position.y, SHOT_RADIUS)
|
||||||
shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOOT_SPEED
|
shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * PLAYER_SHOOT_SPEED
|
||||||
|
|
||||||
|
self.timer = PLAYER_SHOOT_COOLDOWN
|
||||||
|
|||||||
Reference in New Issue
Block a user