Banana Blowout
Last week we worked on a game called Banana Bash. Today, we put the polish on it by adding animation and some sounds.
The game is played as before, but with a few additions. 1) The monkey should do a little 8 frame hop. 2) If a coconut hits a monkey, it should split open across 3 frames. 3) When the monkey touches a coconut or a banana, it should play the appropriate sound.
Assume the following components are available: If you want any others in your
implementation, you will need to create them.
•monkey1.gif-monkey8.gif – these are the 8 frames of the monkey animation
•coconut1.gif-coconut3-gif - these are the 3 frames of the coconut breaking open
•bite.wav –the sound that should play when the monkey eats a banana
•cry.wav–the sound that should play when the monkey gets hit by a coconut
Modify the code from the banana bash game (reproduced below) to make the monkey dance!
# catch the bananas and avoid the coconuts
from livewires import games, color
import random
games.init(screen_width=640, screen_height=480, fps=50)
#global score
playerscore = 0
class Monkey(games.Sprite):
def __init__(self):
monkey_image = games.load_image("monkey.gif")
super(Monkey, self).__init__(image=monkey_image, x=games.mouse.x, bottom=games.screen.height)
self.score = games.Text(value=0, size=25, color=color.white, top=5, right=games.screen.width-10)
games.screen.add(self.score)
def update(self):
self.x = games.mouse.x
ifself.left < 0:
self.left = 0
ifself.rightgames.screen.width:
self.right = games.screen.width
self.check_catch()
defcheck_catch(self):
for s in self.overlapping_sprites:
s.handle_caught()
globalplayerscore
self.score.value = playerscore
class Banana(games.Sprite):
image = games.load_image("banana.gif")
speed = 1
def __init__(self, x, y=90):
super(Banana, self).__init__(image=Banana.image, x=x, y=y, dy=Banana.speed)
def update(self):
ifself.bottomgames.screen.height:
end_message = games.Message(value="Game Over!!", size=90, color=color.white,
x=games.screen.width/2, y=games.screen.height/2,
lifetime=5*games.screen.fps, after_death=games.screen.quit)
games.screen.add(end_message)
defhandle_caught(self):
globalplayerscore
playerscore += 10
self.destroy()
class Coconut(games.Sprite):
image = games.load_image("coconut.gif")
speed = 1
def __init__(self, x, y=90):
super(Coconut, self).__init__(image=Coconut.image, x=x, y=y, dy=Coconut.speed)
def update(self):
ifself.bottomgames.screen.height:
self.destroy()
defhandle_caught(self):
self.end_game()
self.destroy()
defend_game(self):
end_message = games.Message(value="You Died!!", size=90, color=color.white,
x=games.screen.width/2, y=games.screen.height/2,
lifetime=5*games.screen.fps, after_death=games.screen.quit)
games.screen.add(end_message)
class Tree(games.Sprite):
"""
An invisible tree that moves around dropping bananas and coconuts.
"""
image = games.load_image("invisible.gif")
def __init__(self, y=55, speed=2, odds_change=200, banana_weight=2):
super(Tree, self).__init__(image=Tree.image, x=games.screen.width/2, y=y, dx=speed)
self.odds_change = odds_change
self.banana_weight = banana_weight
self.time_til_drop = 0
def update(self):
ifself.left < 0 or self.rightgames.screen.width:
self.dx = -self.dx
elifrandom.randrange(self.odds_change) == 0:
self.dx = -self.dx
self.check_drop()
defcheck_drop(self):
ifself.time_til_drop > 0:
self.time_til_drop -= 1
else:
b_or_c = random.randrange(self.banana_weight+1)
ifb_or_c > 0: # drop a banana
new_banana = Banana(x = self.x)
games.screen.add(new_banana)
self.time_til_drop = int(new_banana.height*1.3/Banana.speed)+1
else: # drop a coconut
new_coconut = Coconut(x=self.x)
games.screen.add(new_coconut)
self.time_til_drop = int(new_coconut.height*1.3/Coconut.speed)+1
def main():
bg_image = games.load_image("background.gif", transparent=False)
games.screen.background = bg_image
the_tree = Tree()
games.screen.add(the_tree)
the_monkey = Monkey()
games.screen.add(the_monkey)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
# play the game!
main()