From 298e9c9df41261526e5419d1d722430cbae5caed Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Wed, 23 Jan 2013 22:30:40 -0430 Subject: [PATCH] Added sound and music. --- audio.py | 21 +++++++++++ ingame.py | 18 +++++++++- intro.py | 21 ++++++++--- main.py | 6 ++++ menu.py | 33 +++++++++++++----- .../01-DANJYON KIMURA-TELEPORTER.mp3} | Bin .../{8-Bit Easter => 8-Bit_Easter}/Cover.jpg | Bin .../DANJYON_KIMURA.jpg | Bin score.py | 11 +++++- 9 files changed, 95 insertions(+), 15 deletions(-) rename music/{8-Bit Easter/01 - DANJYON KIMURA - TELEPORTER.mp3 => 8-Bit_Easter/01-DANJYON KIMURA-TELEPORTER.mp3} (100%) rename music/{8-Bit Easter => 8-Bit_Easter}/Cover.jpg (100%) rename music/{8-Bit Easter => 8-Bit_Easter}/DANJYON_KIMURA.jpg (100%) diff --git a/audio.py b/audio.py index 3d10ec1..0a068ae 100644 --- a/audio.py +++ b/audio.py @@ -1,3 +1,24 @@ ############################################ # Created on 1-23-2013. Miguel Angel Astor # ############################################ +import pygame + +try: + import pygame.mixer as mixer +except ImportError: + import android_mixer as mixer + +class CachedAudioManager: + def __init__(self): + self.cache = {} + + def load_sound(self, path): + if path not in self.cache: + self.cache[path] = mixer.Sound(path) + + def play_sound(self, path): + if path not in self.cache: + self.load_sound(path) + self.cache[path].play() + +cached_audio_manager = CachedAudioManager() diff --git a/ingame.py b/ingame.py index 0fc6482..647e1e7 100644 --- a/ingame.py +++ b/ingame.py @@ -11,10 +11,16 @@ try: except ImportError: android = None +try: + import pygame.mixer as mixer +except ImportError: + import android_mixer as mixer + import math_utils import player import background import imloader +import audio import actor import database from state import BaseState, VALID_STATES @@ -328,8 +334,9 @@ class InGameState(BaseState): huggable.add_moving_frame(image) huggable.toggle_scared() - + self.npcs.add(huggable) + audio.cached_audio_manager.play_sound('sfx/Explo_1.wav') def create_new_enemy(self, position): play_img = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Idle_front.png') @@ -377,6 +384,7 @@ class InGameState(BaseState): enemy.set_angle(math_utils.angle_vectors_2D(self.vec_1, vec_2)) self.enemies.add(enemy) + audio.cached_audio_manager.play_sound('sfx/amiguito.wav') def input(self): for event in pygame.event.get(): @@ -423,6 +431,9 @@ class InGameState(BaseState): # Start the enemy creation timer. pygame.time.set_timer(pygame.USEREVENT + 3, 3000) + mixer.music.load('music/8-Bit_Easter/01-DANJYON KIMURA-TELEPORTER.mp3') + mixer.music.play(-1) + if self.cancel and self.time_left > 0: # If the player pressed escape, force a timeout. self.time_left = 0 @@ -437,6 +448,7 @@ class InGameState(BaseState): player.PLAYERS[1].kill() self.done = True self.create_explosion(self.player.get_position()) + audio.cached_audio_manager.play_sound('sfx/Explo_4.wav') if not self.done: if self.cursor_x != self.screen_center[0] or self.cursor_y != self.screen_center[1]: @@ -507,11 +519,13 @@ class InGameState(BaseState): if self.player.is_moving() and npc.test_collision_with_actor(self.player): npc.make_invisible() self.create_explosion(npc.get_position()) + audio.cached_audio_manager.play_sound('sfx/Explo_4.wav') player.PLAYERS[1].inc_score_by_one() self.time_left += 1 if player.PLAYERS[1].get_score() % 25 == 0: self.wave += 1 + audio.cached_audio_manager.play_sound('sfx/new_stage_1.wav') # If the npc exploded this turn, remove it. if not npc.is_visible(): @@ -536,10 +550,12 @@ class InGameState(BaseState): enemy.make_invisible() self.create_explosion(enemy.get_position()) removal.add(enemy) + audio.cached_audio_manager.play_sound('sfx/Explo_2.wav') elif enemy.get_position()[1] <= self.constraints[2] or enemy.get_position()[1] >= self.constraints[3]: enemy.make_invisible() self.create_explosion(enemy.get_position()) removal.add(enemy) + audio.cached_audio_manager.play_sound('sfx/Explo_2.wav') if len(removal) > 0 and len(self.enemies) >= self.max_npc: # If npcs dissapeared this cycle restart the timer. diff --git a/intro.py b/intro.py index 2cb6e90..385d760 100644 --- a/intro.py +++ b/intro.py @@ -10,6 +10,11 @@ try: except ImportError: android = None +try: + import pygame.mixer as mixer +except ImportError: + import android_mixer as mixer + from state import BaseState, VALID_STATES import actor import game @@ -21,6 +26,7 @@ class IntroState(BaseState): def __init__(self): BaseState.__init__(self) + self.play_music = True self.count = 0 self.next_transition = VALID_STATES['STAY'] @@ -89,6 +95,16 @@ class IntroState(BaseState): self.next_transition = VALID_STATES['MENU'] def update(self): + if self.play_music: + mixer.music.load('music/YellowSubmarine.mp3') + mixer.music.play(0) + self.play_music = False + + if self.next_transition != VALID_STATES['QUIT']: + sm_position = self.sine_movement.get_position() + if sm_position[0] > pygame.display.Info().current_w + self.w_extra: + self.next_transition = VALID_STATES['MENU'] + self.sine_movement.update() sm_position = self.sine_movement.get_position() self.submarine.set_position([sm_position[0], @@ -103,11 +119,6 @@ class IntroState(BaseState): print "Position: " + str(self.sine_movement.get_position()) print self.sine_movement.is_moving() - if self.next_transition != VALID_STATES['QUIT']: - sm_position = self.sine_movement.get_position() - if sm_position[0] > pygame.display.Info().current_w + self.w_extra: - self.next_transition = VALID_STATES['MENU'] - return self.next_transition def render(self, canvas): diff --git a/main.py b/main.py index ca0aa40..1845bb7 100755 --- a/main.py +++ b/main.py @@ -9,6 +9,11 @@ try: except ImportError: android = None +try: + import pygame.mixer as mixer +except ImportError: + import android_mixer as mixer + import database from game import Game @@ -20,6 +25,7 @@ def main(): # Init PyGame. pygame.init() pygame.font.init() + mixer.init() if android: # Init pgs4a and map Android's back button to PyGame's escape key. diff --git a/menu.py b/menu.py index 83e88b3..2bb38ed 100644 --- a/menu.py +++ b/menu.py @@ -8,8 +8,14 @@ try: except ImportError: android = None +try: + import pygame.mixer as mixer +except ImportError: + import android_mixer as mixer + import database import player +import audio from constants import DEBUG from imloader import cached_image_loader from actor import BaseActor @@ -21,7 +27,7 @@ class MenuState(BaseState): def __init__(self): BaseState.__init__(self) - self.next_transition = VALID_STATES['STAY'] + self.next_transition = VALID_STATES['INTRO'] self.current_menu = MENUS['MAIN'] self.story_time = 0 self.story_timeout = 7000 # 7 seconds. @@ -185,14 +191,8 @@ class MenuState(BaseState): self.user_click = True def update(self): - if android is None: - pygame.mouse.set_visible(True) - - player.PLAYERS[1].reset_score() - if self.next_transition != VALID_STATES['QUIT']: if self.next_transition != VALID_STATES['STAY']: - # Set next_transition to STAY if the game gets to this state from ScoreState. self.next_transition = VALID_STATES['STAY'] # Reset the scores label to force a database reload. self.score_1 = None @@ -200,6 +200,17 @@ class MenuState(BaseState): self.score_3 = None self.score_4 = None self.score_5 = None + if android is None: + pygame.mouse.set_visible(True) + player.PLAYERS[1].reset_score() + + if self.current_menu == MENUS['MAIN'] or self.current_menu == MENUS['INTRO']: + mixer.music.load('music/Press_Start_Infinitoooooooo.mp3') + mixer.music.play(-1) + else: + mixer.music.load('music/Wind_Waker_Main (Remix).mp3') + mixer.music.play(-1) + if self.current_menu == MENUS['MAIN']: # Check for mouse (tap) collisions with the main menu buttons. @@ -210,17 +221,23 @@ class MenuState(BaseState): # Collision with the high scores button. self.current_menu = MENUS['SCORE'] self.reload_scores() + mixer.music.load('music/Wind_Waker_Main (Remix).mp3') + mixer.music.play(-1) elif self.new_button.rect.collidepoint(self.cursor_x, self.cursor_y): # Collision with the new game button. self.current_menu = MENUS['INTRO'] self.then = pygame.time.get_ticks() + mixer.music.stop() + audio.cached_audio_manager.play_sound('sfx/press_start_2.wav') elif self.current_menu == MENUS['SCORE']: # Check for mouse (tap) collisions with the high scores menu buttons. if self.back_button.rect.collidepoint(self.cursor_x, self.cursor_y): # Collision with the go back button. self.current_menu = MENUS['MAIN'] - + mixer.music.load('music/Press_Start_Infinitoooooooo.mp3') + mixer.music.play(-1) + elif self.current_menu == MENUS['INTRO']: # Wait for a timeout before going to the game. now = pygame.time.get_ticks() diff --git a/music/8-Bit Easter/01 - DANJYON KIMURA - TELEPORTER.mp3 b/music/8-Bit_Easter/01-DANJYON KIMURA-TELEPORTER.mp3 similarity index 100% rename from music/8-Bit Easter/01 - DANJYON KIMURA - TELEPORTER.mp3 rename to music/8-Bit_Easter/01-DANJYON KIMURA-TELEPORTER.mp3 diff --git a/music/8-Bit Easter/Cover.jpg b/music/8-Bit_Easter/Cover.jpg similarity index 100% rename from music/8-Bit Easter/Cover.jpg rename to music/8-Bit_Easter/Cover.jpg diff --git a/music/8-Bit Easter/DANJYON_KIMURA.jpg b/music/8-Bit_Easter/DANJYON_KIMURA.jpg similarity index 100% rename from music/8-Bit Easter/DANJYON_KIMURA.jpg rename to music/8-Bit_Easter/DANJYON_KIMURA.jpg diff --git a/score.py b/score.py index b1e41fb..6e34d64 100644 --- a/score.py +++ b/score.py @@ -9,8 +9,14 @@ try: except ImportError: android = None +try: + import pygame.mixer as mixer +except ImportError: + import android_mixer as mixer + import player import database +import audio from constants import DEBUG from imloader import cached_image_loader from actor import BaseActor @@ -21,7 +27,7 @@ class ScoreState(BaseState): BaseState.__init__(self) self.background_color = (125, 158, 192) - self.next_transition = VALID_STATES['STAY'] + self.next_transition = VALID_STATES['MENU'] self.cursor_x = 0 self.cursor_y = 0 @@ -97,6 +103,9 @@ class ScoreState(BaseState): if self.next_transition != VALID_STATES['STAY']: # Set next_transition to STAY if the game gets to this state from GameState a second or third time, etc. self.next_transition = VALID_STATES['STAY'] + mixer.music.stop() + audio.cached_audio_manager.play_sound('sfx/Game_Over_2.wav') + if self.letter_index < 3: # If not all initials are set, check taps on every letter.