Skip to content

Commit

Permalink
#!/usr/bin/python
Browse files Browse the repository at this point in the history
  • Loading branch information
asweigart committed Aug 8, 2012
1 parent 113c91d commit f30ae54
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions square-shooter/square-shooter_makeover.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def copy(self, vector):
self.y = vector.y


class ObjectOnMap:
class ObjectOnMap(object):
"""Represents a circular object on the game map with position, radius, and velocity."""

def __init__(self, radius):
Expand Down Expand Up @@ -110,31 +110,60 @@ def collides_with(self, other):
return distance < (self.radius + other.radius)


def random_position():
"""Returns a random float value that will be near the edge of the map"""
if random.randint(0, 1) == 0:
return random.uniform(0.0, 0.25)
else:
return random.uniform(0.75, 1.0)


def bubble_factory(kind):
class Bubble(ObjectOnMap):
# (size, speed)
kinds = {'big': (0.1, 0.1),
'medium': (0.075, 0.15),
'small': (0.05, 0.25)}

size, speed = kinds[kind]
colors = [pygame.Color('#ffffcc'),
pygame.Color('#ffccff'),
pygame.Color('#ccffff'),
pygame.Color('#ffdddd'),
pygame.Color('#ddffdd'),
pygame.Color('#ddddff')]

def __init__(self, kind):
size, speed = Bubble.kinds[kind]
super(Bubble ,self).__init__(size)

self.pos = Vector2D(
random.random(),
random.random())
self.speed = Vector2D(
random.uniform(-speed, speed),
random.uniform(-speed, speed))
self.kind = kind
self.color = random.choice(Bubble.colors)

def spawn(self):
spawned_bubbles = []
spawned_powerups = []

if self.kind == "small":
if random.random() < 0.25:
spawned_powerups.append(Powerup(self.pos))
else:
print(self.kind)
if self.kind == "medium":
new_kind = "small"
elif self.kind == "big":
new_kind = "medium"

for i in range(2):
spawned_bubbles.append(Bubble(new_kind))
spawned_bubbles[-1].pos.copy(self.pos)

return (spawned_bubbles, spawned_powerups)


class Powerup(ObjectOnMap):
def __init__(self, pos):
super(Powerup, self).__init__(0.03)
self.pos.copy(pos)
self.kind = random.choice(("shield", "bullet", "freeze"))
self.age = 0

new_bubble = ObjectOnMap(size)
new_bubble.pos = Vector2D(
random_position(),
random_position())
new_bubble.speed = Vector2D(
random.uniform(-speed, speed),
random.uniform(-speed, speed))
new_bubble.kind = kind
return new_bubble

class GameWorld:
bubbles = []
Expand Down Expand Up @@ -179,7 +208,7 @@ def init_level(self, level):
del self.explosions[:]
del self.powerups[:]
for i in range(level):
self.bubbles.append(bubble_factory("big"))
self.bubbles.append(Bubble("big"))

def update(self, delta_t):
self.handle_collisions(delta_t)
Expand Down Expand Up @@ -246,7 +275,9 @@ def handle_collisions(self, delta_t):
# Push it along or it will just
# destroy the newly formed bubbles.
self.bullet.update(delta_t * 5)
self.spawn_bubbles(b)
spawned_bubbles, spawned_powerups = b.spawn()
self.bubbles.extend(spawned_bubbles)
self.powerups.extend(spawned_powerups)
self.spawn_explosion(b)
self.mark_score(b)
if len(self.bubbles) == 0:
Expand All @@ -270,34 +301,11 @@ def handle_collisions(self, delta_t):
self.apply_powerup(p)
self.powerups.remove(p)

def spawn_bubbles(self, parent):
if parent.kind == "small":
if random.random() < 0.25:
self.spawn_powerup(parent)
else:
if parent.kind == "big":
new_type = "medium"
elif parent.kind == "medium":
new_type = "small"
b = bubble_factory(new_type)
b.pos.copy(parent.pos)
self.bubbles.append(b)
b = bubble_factory(new_type)
b.pos.copy(parent.pos)
self.bubbles.append(b)

def spawn_explosion(self, bubble):
explosion = ObjectOnMap(0)
explosion.pos.copy(bubble.pos)
self.explosions.append(explosion)

def spawn_powerup(self, bubble):
powerup = ObjectOnMap(0.03)
powerup.pos.copy(bubble.pos)
powerup.kind = random.choice(("shield", "bullet", "freeze"))
powerup.age = 0
self.powerups.append(powerup)

def mark_score(self, bubble):
if bubble.kind == "small":
self.score += 5
Expand Down Expand Up @@ -374,11 +382,7 @@ def __init__(self, model, screen):
self.msg_font = pygame.font.SysFont(
font_name, self.height / 20)

self.bubble_colors = ["#ffffcc", "#ffccff", "#ccffff",
"#ffdddd", "#ddffdd", "#ddddff"]
for i in range(6):
self.bubble_colors[i] = pygame.Color(
self.bubble_colors[i])


self.game_paused = False

Expand Down Expand Up @@ -453,9 +457,6 @@ def render_game_world(self):
if m.bullet != None: self.render_bullet()

for bubble in m.bubbles:
if not hasattr(bubble, "color"):
bubble.color = random.choice(
self.bubble_colors)
pos = bubble.pos
pygame.draw.circle(
self.screen,
Expand Down

0 comments on commit f30ae54

Please sign in to comment.