diff options
author | Luiz Irber <luiz.irber@gmail.com> | 2008-04-06 16:55:57 (GMT) |
---|---|---|
committer | Luiz Irber <luiz.irber@gmail.com> | 2008-04-06 16:55:57 (GMT) |
commit | 1fd480d53e99b8f8849e5ebc39134e1dc8cea698 (patch) | |
tree | b392dd4dffb52caf594a5ca6e45b566036ba3b37 | |
parent | 76863407698542c73afb5840e658b615e2dfa8d1 (diff) |
Fixes on sound and levels.
- Now the game can be started without a sound device on the computer.
But it still needs more testing.
- Multiple targets can be set on a level, and any object can be a target.
But there isn't any level that uses this feature yet.
- Minor cleanup on code, and some comments added.
-rw-r--r-- | Gambiarra/gambiarra.py | 65 | ||||
-rw-r--r-- | Gambiarra/levels.py | 68 | ||||
-rw-r--r-- | Gambiarra/objects/animals.py | 6 | ||||
-rw-r--r-- | Gambiarra/objects/balls.py | 22 | ||||
-rw-r--r-- | Gambiarra/objects/elastica.py | 6 | ||||
-rw-r--r-- | Gambiarra/objects/esteira.py | 4 | ||||
-rw-r--r-- | Gambiarra/objects/target.py | 4 | ||||
-rw-r--r-- | Gambiarra/objects/things.py | 6 | ||||
-rw-r--r-- | Gambiarra/objects/wall.py | 10 |
9 files changed, 119 insertions, 72 deletions
diff --git a/Gambiarra/gambiarra.py b/Gambiarra/gambiarra.py index 234ce30..1919e86 100644 --- a/Gambiarra/gambiarra.py +++ b/Gambiarra/gambiarra.py @@ -20,19 +20,22 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -import pygame -from pygame.locals import * import os import sys from os.path import abspath + +import pygame +from pygame.locals import * import levels as Levels + +from command import Play, Help, Quit +from gamemenu import GameMenu + from objects.animals import * from objects.elastica import Elastica from objects.esteira import Esteira from objects.target import Target from objects.wall import * -from command import Play, Help, Quit -from gamemenu import GameMenu def check_collision(sprite_list, wall_list): new_objects = pygame.sprite.RenderPlain() @@ -136,37 +139,44 @@ def check_collision(sprite_list, wall_list): class Game(object): + # controle do jogo fps = 30 - screen = None playing = None - run = None - background = None + running = None clock = None level = 0 levels = [] selected_element = None + _showed_help = None + count = None + play_sounds = None + + # elementos do jogo + screen = None menu = None congrats = None congratsSnd = None - _showed_help = None - count = None - def __init__(self): + def __init__(self, play_sounds=True): pygame.init() - pygame.mixer.init() + self.play_sounds = play_sounds + if self.play_sounds: + pygame.mixer.init() self.screen = pygame.display.set_mode((1200,900)) #omitindo flags pygame.display.flip() - self.run = True + self.running = True self.playing = False pygame.display.set_caption("Gambiarra") self.clock = pygame.time.Clock() self.levels = Levels.init_levels() self.menu = GameMenu() self.congrats = pygame.image.load("../data/images/fim_fase.png") - self.congratsSnd = pygame.mixer.Sound(abspath("../data/snd/Congrats.wav")) + if self.play_sounds: + self.congratsSnd = pygame.mixer.Sound(abspath("../data/snd/Congrats.wav")) self._showed_help = False self.count = 0 + def run(self): #inicia o loop self.main_loop() @@ -198,15 +208,6 @@ class Game(object): if self.selected_element.editable: self.selected_element.rect.center = pygame.mouse.get_pos() - - def goal_reached(self): - reached = False - if self.level < len(self.levels): - if self.levels[self.level].goal.rect.collidepoint( - self.levels[self.level].toGoal.rect.center): - reached = True - return reached - def mouse_event(self, mousePos): if not self.selected_element: collided = False @@ -269,11 +270,14 @@ class Game(object): self.selected_element = None def show_congratulations(self): - pygame.mixer.stop() - self.congratsSnd.play() + if self.play_sounds: + pygame.mixer.stop() + self.congratsSnd.play() + self.screen.blit(self.congrats, (600 - self.congrats.get_width()/2, 450 - self.congrats.get_height()/2) ) pygame.display.flip() + while True: for event in pygame.event.get(): if event.type == MOUSEBUTTONDOWN: @@ -281,10 +285,8 @@ class Game(object): def main_loop(self): self.menu.run() - while self.run: - goal = self.goal_reached() - while (not goal and - self.level < len(self.levels)): + while self.running and self.level < len(self.levels): + while not self.levels[self.level].goal_reached(): self.event_handler() self.clock.tick(self.fps) self.update_screen(self.fps) @@ -296,17 +298,16 @@ class Game(object): pygame.display.flip() - goal = self.goal_reached() self.playing = False self._showed_help = False - goal = False self.level += 1 self.show_congratulations() if (len(self.levels) == self.level) : - self.run = False + self.running = False def main(): - game = Game() + game = Game(play_sounds=False) + game.run() if __name__ == "__main__": main() diff --git a/Gambiarra/levels.py b/Gambiarra/levels.py index c0328e5..2e4efe2 100644 --- a/Gambiarra/levels.py +++ b/Gambiarra/levels.py @@ -30,6 +30,8 @@ from objects.wall import * from command import * def _is_static(obj): + # FIXME: we need to define a way of saying that an object is static, this + # "if" is repulsive! =D if (isinstance(obj, Target) or isinstance(obj, Esteira) or isinstance(obj, Elastica)): return True @@ -131,14 +133,19 @@ class Level(object): on the screen""" objects = None - def __init__(self, objInPlace, objToAdd, goal, toGoal, helpImage): + def __init__(self, objInPlace, objToAdd, goals, helpImage): self.simulator = SimulationView(objInPlace) self.objbar = ObjectBar(objToAdd) self.cmdbar = CommandBar() - self.goal = goal - self.toGoal = toGoal + self.goals = goals self.helpImage = helpImage + def goal_reached(self): + for obj, goal in self.goals: + if not obj.rect.collidepoint(goal.rect.center): + return False + return True + def draw(self): self.simulator.draw() self.objbar.draw() @@ -180,11 +187,14 @@ def init_levels(): level3HelpImage = pygame.image.load("../data/images/obj-level3.png") level4ObjInPlace = [ BowlingBall((20,20), editable=False), - SoccerBall((800, 300),editable=False), - Target((900, 90), editable=False)] - level4ObjToAdd = [ Esteira(), Esteira()] - level4Goal = level4ObjInPlace[-1] - level4ToGoal = level4ObjInPlace[0] + SoccerBall((800, 300), editable=False), + Target((900, 90), editable=False), + Target((100, 550), editable=False)] + level4ObjToAdd = [ Esteira(), Esteira(), Esteira()] + level4Goal1 = level4ObjInPlace[-2] + level4ToGoal1 = level4ObjInPlace[0] + level4Goal2 = level4ObjInPlace[-1] + level4ToGoal2 = level4ObjInPlace[1] level4HelpImage = pygame.image.load("../data/images/obj-level4.png") level5ObjInPlace = [ BowlingBall((1000,300), editable=False), @@ -204,17 +214,35 @@ def init_levels(): level6ToGoal = level6ObjInPlace[0] level6HelpImage = pygame.image.load("../data/images/obj-level6.png") - level1 = Level( level1ObjInPlace, level1ObjToAdd, level1Goal, - level1ToGoal, level1HelpImage) - level2 = Level( level2ObjInPlace, level2ObjToAdd, level2Goal, - level2ToGoal, level2HelpImage) - level3 = Level( level3ObjInPlace, level3ObjToAdd, level3Goal, - level3ToGoal, level3HelpImage) - level4 = Level( level4ObjInPlace, level4ObjToAdd, level4Goal, - level4ToGoal, level4HelpImage) - level5 = Level( level5ObjInPlace, level5ObjToAdd, level5Goal, - level5ToGoal, level5HelpImage) - level6 = Level( level6ObjInPlace, level6ObjToAdd, level6Goal, - level6ToGoal, level6HelpImage) + level1 = Level( level1ObjInPlace, + level1ObjToAdd, + [(level1Goal, level1ToGoal)], + level1HelpImage) + + level2 = Level( level2ObjInPlace, + level2ObjToAdd, + [(level2Goal, level2ToGoal)], + level2HelpImage) + + level3 = Level( level3ObjInPlace, + level3ObjToAdd, + [(level3Goal,level3ToGoal)], + level3HelpImage) + + level4 = Level( level4ObjInPlace, + level4ObjToAdd, + [(level4Goal1, level4ToGoal1), + (level4Goal2, level4ToGoal2)], + level4HelpImage) + + level5 = Level( level5ObjInPlace, + level5ObjToAdd, + [(level5Goal, level5ToGoal)], + level5HelpImage) + + level6 = Level( level6ObjInPlace, + level6ObjToAdd, + [(level6Goal, level6ToGoal)], + level6HelpImage) return [level1, level2, level3, level4, level5, level6] diff --git a/Gambiarra/objects/animals.py b/Gambiarra/objects/animals.py index ab65dfb..de64fa9 100644 --- a/Gambiarra/objects/animals.py +++ b/Gambiarra/objects/animals.py @@ -27,9 +27,13 @@ from os.path import abspath class Penguin(Thing): def __init__(self, initialPosition=None, editable=True): + if pygame.mixer.get_init(): + snd = pygame.mixer.Sound(abspath("../data/snd/penguin.wav")) + else: + snd = None super(Penguin, self).__init__( pygame.image.load(abspath("../data/images/penguin.png")), - editable, pygame.mixer.Sound(abspath("../data/snd/penguin.wav")), + editable, snd, initialPosition, elasticity = 100, mobility = True, gravity = 5) self.speed=[5,0] diff --git a/Gambiarra/objects/balls.py b/Gambiarra/objects/balls.py index cff55c6..f409d38 100644 --- a/Gambiarra/objects/balls.py +++ b/Gambiarra/objects/balls.py @@ -27,22 +27,34 @@ from things import Thing class BowlingBall(Thing): def __init__(self, initialPosition=None, editable=True): + if pygame.mixer.get_init(): + snd = pygame.mixer.Sound(abspath("../data/snd/BowlingBall.wav")) + else: + snd = None super(BowlingBall, self).__init__( - pygame.image.load(abspath("../data/images/bolaBoliche.png")), - editable, pygame.mixer.Sound(abspath("../data/snd/BowlingBall.wav")), - initialPosition, elasticity = 60, mobility = True, gravity = 5) + pygame.image.load(abspath("../data/images/bolaBoliche.png")), + editable, snd, + initialPosition, elasticity = 60, mobility = True, gravity = 5) class BeachBall(Thing): def __init__(self, initialPosition=None, editable=True): + if pygame.mixer.get_init(): + snd = pygame.mixer.Sound(abspath("../data/snd/BowlingBall.wav")) + else: + snd = None super(BeachBall, self).__init__( pygame.image.load(abspath("../data/images/bola.png")), - editable, pygame.mixer.Sound(abspath("../data/snd/BowlingBall.wav")), + editable, snd, initialPosition, elasticity = 90, mobility = True, gravity = 5) class SoccerBall(Thing): def __init__(self, initialPosition=None, editable=True): + if pygame.mixer.get_init(): + snd = pygame.mixer.Sound(abspath("../data/snd/BowlingBall.wav")) + else: + snd = None super(SoccerBall, self).__init__( pygame.image.load(abspath("../data/images/futebol.png")), - editable, pygame.mixer.Sound(abspath("../data/snd/BowlingBall.wav")), + editable, snd, initialPosition, elasticity = 70, mobility = True, gravity = 5) diff --git a/Gambiarra/objects/elastica.py b/Gambiarra/objects/elastica.py index 67527da..350edab 100644 --- a/Gambiarra/objects/elastica.py +++ b/Gambiarra/objects/elastica.py @@ -28,8 +28,12 @@ from things import Thing class Elastica(Thing): def __init__(self, initialPosition = [0,0], editable=True): + if pygame.mixer.get_init(): + snd = pygame.mixer.Sound(abspath("../data/snd/cama_elastica.wav")) + else: + snd = None super(Elastica, self).__init__( pygame.image.load(abspath("../data/images/cama_elastica.png")), - editable, pygame.mixer.Sound(abspath("../data/snd/cama_elastica.wav")), + editable, snd, initialPosition, elasticity = 100, mobility = False, gravity = 10) diff --git a/Gambiarra/objects/esteira.py b/Gambiarra/objects/esteira.py index 0bcbb3c..88bff1f 100644 --- a/Gambiarra/objects/esteira.py +++ b/Gambiarra/objects/esteira.py @@ -23,7 +23,7 @@ from os.path import abspath import pygame -from things import Thing, NoSound +from things import Thing class Esteira(Thing): @@ -32,7 +32,7 @@ class Esteira(Thing): def __init__(self, initialPosition = [0,0], editable=True): super(Esteira, self).__init__( pygame.image.load(abspath("../data/images/esteira_dir.png")), - editable, NoSound(), + editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) self.sentido = 1 diff --git a/Gambiarra/objects/target.py b/Gambiarra/objects/target.py index 62a481c..8df61b8 100644 --- a/Gambiarra/objects/target.py +++ b/Gambiarra/objects/target.py @@ -23,12 +23,12 @@ from os.path import abspath import pygame -from things import Thing, NoSound +from things import Thing class Target(Thing): def __init__(self, initialPosition = [0,0], editable=True): super(Target, self).__init__( pygame.image.load(abspath("../data/images/target.png")), - editable, NoSound(), + editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) diff --git a/Gambiarra/objects/things.py b/Gambiarra/objects/things.py index f1ebf04..3fe6572 100644 --- a/Gambiarra/objects/things.py +++ b/Gambiarra/objects/things.py @@ -41,9 +41,6 @@ import pygame -class NoSound(): - def play(self): return - class Thing(pygame.sprite.Sprite): img = None @@ -77,4 +74,5 @@ class Thing(pygame.sprite.Sprite): screen.blit(self.image, (pos[0],pos[1])) def play(self): - self.snd.play() + if self.snd and pygame.mixer.get_init(): + self.snd.play() diff --git a/Gambiarra/objects/wall.py b/Gambiarra/objects/wall.py index 0bb3d44..866801c 100644 --- a/Gambiarra/objects/wall.py +++ b/Gambiarra/objects/wall.py @@ -23,27 +23,27 @@ from os.path import abspath import pygame -from things import Thing, NoSound +from things import Thing class LeftWall(Thing): def __init__(self, initialPosition = [0,0], editable=False): super(LeftWall, self).__init__( pygame.image.load(abspath("../data/images/leftwall.png")), - editable, NoSound(), + editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) class RightWall(Thing): def __init__(self, initialPosition = [1185,0], editable=False): super(RightWall, self).__init__( pygame.image.load(abspath("../data/images/rightwall.png")), - editable, NoSound(), + editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) class UpWall(Thing): def __init__(self, initialPosition = [15,0], editable=False): super(UpWall, self).__init__( pygame.image.load(abspath("../data/images/upwall.png")), - editable, NoSound(), + editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) @@ -51,6 +51,6 @@ class DownWall(Thing): def __init__(self, initialPosition = [15,755], editable=False): super(DownWall, self).__init__( pygame.image.load(abspath("../data/images/downwall.png")), - editable, NoSound(), + editable, None, initialPosition, elasticity = 100, mobility = False, gravity = 10) |