Skip to content

Commit

Permalink
add Asteroid.add_if_below_threshold()
Browse files Browse the repository at this point in the history
Add a method to create a new random asteroid, headed toward
player, if the supplied threshold is not met.
  • Loading branch information
kawa-kokosowa committed Aug 13, 2016
1 parent df8f72f commit f7721b6
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ def __init__(self, center, size, x_speed, y_speed):
self.y_speed = y_speed
super(Asteroid, self).__init__(self)

@classmethod
def add_if_below_threshold(cls, player, asteroid_list, at_least_x_existing):

if len(asteroid_list) < at_least_x_existing:
plus_or_minus_x = random.choice([1, -1])
new_asteroid_x = player.collider.rect.top - (10 * plus_or_minus_x)

if new_asteroid_x > player.collider.rect.left:
x_speed = -1
else:
x_speed = 1

plus_or_minus_y = random.choice([1, -1])
new_asteroid_y = player.collider.rect.left - (10 * plus_or_minus_y)

if new_asteroid_y > player.collider.rect.top:
y_speed = -1
else:
y_speed = 1

another_asteroid = Asteroid((new_asteroid_x, new_asteroid_y), 10, x_speed, y_speed)
asteroid_list.add(another_asteroid)

def new_smaller_asteroid(self):
"""New x and y speed based on player coords
Expand Down Expand Up @@ -300,25 +323,7 @@ def wrap_logic(rect, layer_size):

# create some asteroids, hurdled t the player
# we should make these chase the player, actually...
if len(asteroid_list) < 20:
plus_or_minus_x = random.choice([1, -1])
new_asteroid_x = player.collider.rect.top - (10 * plus_or_minus_x)

if new_asteroid_x > player.collider.rect.left:
x_speed = -1
else:
x_speed = 1

plus_or_minus_y = random.choice([1, -1])
new_asteroid_y = player.collider.rect.left - (10 * plus_or_minus_y)

if new_asteroid_y > player.collider.rect.top:
y_speed = -1
else:
y_speed = 1

another_asteroid = Asteroid((new_asteroid_x, new_asteroid_y), 10, x_speed, y_speed)
asteroid_list.add(another_asteroid)
Asteroid.add_if_below_threshold(player, asteroid_list, 20)

# DRAWING/RENDER CODE

Expand Down

0 comments on commit f7721b6

Please sign in to comment.