added asteroid splitting
This commit is contained in:
parent
82882bab74
commit
e36416de3a
2 changed files with 17 additions and 1 deletions
16
asteroid.py
16
asteroid.py
|
|
@ -1,6 +1,7 @@
|
||||||
import circleshape
|
import circleshape
|
||||||
import pygame
|
import pygame
|
||||||
import constants
|
import constants
|
||||||
|
import random
|
||||||
|
|
||||||
class Asteroid(circleshape.CircleShape):
|
class Asteroid(circleshape.CircleShape):
|
||||||
|
|
||||||
|
|
@ -14,3 +15,18 @@ class Asteroid(circleshape.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 <= constants.ASTEROID_MIN_RADIUS:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
rand_angle = random.uniform(20, 50)
|
||||||
|
new_velocity1 = self.velocity.rotate(rand_angle)
|
||||||
|
new_velocity2 = self.velocity.rotate(-rand_angle)
|
||||||
|
new_radius = self.radius - constants.ASTEROID_MIN_RADIUS
|
||||||
|
|
||||||
|
asteroid1 = Asteroid(self.position.x, self.position.y, new_radius)
|
||||||
|
asteroid1.velocity = new_velocity1 * 1.2
|
||||||
|
asteroid2 = Asteroid(self.position.x, self.position.y, new_radius)
|
||||||
|
asteroid2.velocity = new_velocity2 * 1.2
|
||||||
|
|
|
||||||
2
main.py
2
main.py
|
|
@ -58,7 +58,7 @@ def main():
|
||||||
for asteroid in asteroids:
|
for asteroid in asteroids:
|
||||||
for shot in shots:
|
for shot in shots:
|
||||||
if shot.is_colided(asteroid):
|
if shot.is_colided(asteroid):
|
||||||
asteroid.kill()
|
asteroid.split()
|
||||||
shot.kill()
|
shot.kill()
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
tick = clock.tick(60)
|
tick = clock.tick(60)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue