Added sound and music.

This commit is contained in:
2013-01-23 22:30:40 -04:30
parent b800e46d29
commit 298e9c9df4
9 changed files with 95 additions and 15 deletions

View File

@@ -1,3 +1,24 @@
############################################ ############################################
# Created on 1-23-2013. Miguel Angel Astor # # 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()

View File

@@ -11,10 +11,16 @@ try:
except ImportError: except ImportError:
android = None android = None
try:
import pygame.mixer as mixer
except ImportError:
import android_mixer as mixer
import math_utils import math_utils
import player import player
import background import background
import imloader import imloader
import audio
import actor import actor
import database import database
from state import BaseState, VALID_STATES from state import BaseState, VALID_STATES
@@ -328,8 +334,9 @@ class InGameState(BaseState):
huggable.add_moving_frame(image) huggable.add_moving_frame(image)
huggable.toggle_scared() huggable.toggle_scared()
self.npcs.add(huggable) self.npcs.add(huggable)
audio.cached_audio_manager.play_sound('sfx/Explo_1.wav')
def create_new_enemy(self, position): def create_new_enemy(self, position):
play_img = imloader.cached_image_loader.get_image_to_screen_percent('gfx/ForeverAlone/Idle_front.png') 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)) enemy.set_angle(math_utils.angle_vectors_2D(self.vec_1, vec_2))
self.enemies.add(enemy) self.enemies.add(enemy)
audio.cached_audio_manager.play_sound('sfx/amiguito.wav')
def input(self): def input(self):
for event in pygame.event.get(): for event in pygame.event.get():
@@ -423,6 +431,9 @@ class InGameState(BaseState):
# Start the enemy creation timer. # Start the enemy creation timer.
pygame.time.set_timer(pygame.USEREVENT + 3, 3000) 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 self.cancel and self.time_left > 0:
# If the player pressed escape, force a timeout. # If the player pressed escape, force a timeout.
self.time_left = 0 self.time_left = 0
@@ -437,6 +448,7 @@ class InGameState(BaseState):
player.PLAYERS[1].kill() player.PLAYERS[1].kill()
self.done = True self.done = True
self.create_explosion(self.player.get_position()) self.create_explosion(self.player.get_position())
audio.cached_audio_manager.play_sound('sfx/Explo_4.wav')
if not self.done: if not self.done:
if self.cursor_x != self.screen_center[0] or self.cursor_y != self.screen_center[1]: 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): if self.player.is_moving() and npc.test_collision_with_actor(self.player):
npc.make_invisible() npc.make_invisible()
self.create_explosion(npc.get_position()) self.create_explosion(npc.get_position())
audio.cached_audio_manager.play_sound('sfx/Explo_4.wav')
player.PLAYERS[1].inc_score_by_one() player.PLAYERS[1].inc_score_by_one()
self.time_left += 1 self.time_left += 1
if player.PLAYERS[1].get_score() % 25 == 0: if player.PLAYERS[1].get_score() % 25 == 0:
self.wave += 1 self.wave += 1
audio.cached_audio_manager.play_sound('sfx/new_stage_1.wav')
# If the npc exploded this turn, remove it. # If the npc exploded this turn, remove it.
if not npc.is_visible(): if not npc.is_visible():
@@ -536,10 +550,12 @@ class InGameState(BaseState):
enemy.make_invisible() enemy.make_invisible()
self.create_explosion(enemy.get_position()) self.create_explosion(enemy.get_position())
removal.add(enemy) 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]: elif enemy.get_position()[1] <= self.constraints[2] or enemy.get_position()[1] >= self.constraints[3]:
enemy.make_invisible() enemy.make_invisible()
self.create_explosion(enemy.get_position()) self.create_explosion(enemy.get_position())
removal.add(enemy) 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 len(removal) > 0 and len(self.enemies) >= self.max_npc:
# If npcs dissapeared this cycle restart the timer. # If npcs dissapeared this cycle restart the timer.

View File

@@ -10,6 +10,11 @@ try:
except ImportError: except ImportError:
android = None android = None
try:
import pygame.mixer as mixer
except ImportError:
import android_mixer as mixer
from state import BaseState, VALID_STATES from state import BaseState, VALID_STATES
import actor import actor
import game import game
@@ -21,6 +26,7 @@ class IntroState(BaseState):
def __init__(self): def __init__(self):
BaseState.__init__(self) BaseState.__init__(self)
self.play_music = True
self.count = 0 self.count = 0
self.next_transition = VALID_STATES['STAY'] self.next_transition = VALID_STATES['STAY']
@@ -89,6 +95,16 @@ class IntroState(BaseState):
self.next_transition = VALID_STATES['MENU'] self.next_transition = VALID_STATES['MENU']
def update(self): 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() self.sine_movement.update()
sm_position = self.sine_movement.get_position() sm_position = self.sine_movement.get_position()
self.submarine.set_position([sm_position[0], self.submarine.set_position([sm_position[0],
@@ -103,11 +119,6 @@ class IntroState(BaseState):
print "Position: " + str(self.sine_movement.get_position()) print "Position: " + str(self.sine_movement.get_position())
print self.sine_movement.is_moving() 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 return self.next_transition
def render(self, canvas): def render(self, canvas):

View File

@@ -9,6 +9,11 @@ try:
except ImportError: except ImportError:
android = None android = None
try:
import pygame.mixer as mixer
except ImportError:
import android_mixer as mixer
import database import database
from game import Game from game import Game
@@ -20,6 +25,7 @@ def main():
# Init PyGame. # Init PyGame.
pygame.init() pygame.init()
pygame.font.init() pygame.font.init()
mixer.init()
if android: if android:
# Init pgs4a and map Android's back button to PyGame's escape key. # Init pgs4a and map Android's back button to PyGame's escape key.

33
menu.py
View File

@@ -8,8 +8,14 @@ try:
except ImportError: except ImportError:
android = None android = None
try:
import pygame.mixer as mixer
except ImportError:
import android_mixer as mixer
import database import database
import player import player
import audio
from constants import DEBUG from constants import DEBUG
from imloader import cached_image_loader from imloader import cached_image_loader
from actor import BaseActor from actor import BaseActor
@@ -21,7 +27,7 @@ class MenuState(BaseState):
def __init__(self): def __init__(self):
BaseState.__init__(self) BaseState.__init__(self)
self.next_transition = VALID_STATES['STAY'] self.next_transition = VALID_STATES['INTRO']
self.current_menu = MENUS['MAIN'] self.current_menu = MENUS['MAIN']
self.story_time = 0 self.story_time = 0
self.story_timeout = 7000 # 7 seconds. self.story_timeout = 7000 # 7 seconds.
@@ -185,14 +191,8 @@ class MenuState(BaseState):
self.user_click = True self.user_click = True
def update(self): 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['QUIT']:
if self.next_transition != VALID_STATES['STAY']: 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'] self.next_transition = VALID_STATES['STAY']
# Reset the scores label to force a database reload. # Reset the scores label to force a database reload.
self.score_1 = None self.score_1 = None
@@ -200,6 +200,17 @@ class MenuState(BaseState):
self.score_3 = None self.score_3 = None
self.score_4 = None self.score_4 = None
self.score_5 = 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']: if self.current_menu == MENUS['MAIN']:
# Check for mouse (tap) collisions with the main menu buttons. # Check for mouse (tap) collisions with the main menu buttons.
@@ -210,17 +221,23 @@ class MenuState(BaseState):
# Collision with the high scores button. # Collision with the high scores button.
self.current_menu = MENUS['SCORE'] self.current_menu = MENUS['SCORE']
self.reload_scores() 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): elif self.new_button.rect.collidepoint(self.cursor_x, self.cursor_y):
# Collision with the new game button. # Collision with the new game button.
self.current_menu = MENUS['INTRO'] self.current_menu = MENUS['INTRO']
self.then = pygame.time.get_ticks() 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']: elif self.current_menu == MENUS['SCORE']:
# Check for mouse (tap) collisions with the high scores menu buttons. # Check for mouse (tap) collisions with the high scores menu buttons.
if self.back_button.rect.collidepoint(self.cursor_x, self.cursor_y): if self.back_button.rect.collidepoint(self.cursor_x, self.cursor_y):
# Collision with the go back button. # Collision with the go back button.
self.current_menu = MENUS['MAIN'] self.current_menu = MENUS['MAIN']
mixer.music.load('music/Press_Start_Infinitoooooooo.mp3')
mixer.music.play(-1)
elif self.current_menu == MENUS['INTRO']: elif self.current_menu == MENUS['INTRO']:
# Wait for a timeout before going to the game. # Wait for a timeout before going to the game.
now = pygame.time.get_ticks() now = pygame.time.get_ticks()

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

@@ -9,8 +9,14 @@ try:
except ImportError: except ImportError:
android = None android = None
try:
import pygame.mixer as mixer
except ImportError:
import android_mixer as mixer
import player import player
import database import database
import audio
from constants import DEBUG from constants import DEBUG
from imloader import cached_image_loader from imloader import cached_image_loader
from actor import BaseActor from actor import BaseActor
@@ -21,7 +27,7 @@ class ScoreState(BaseState):
BaseState.__init__(self) BaseState.__init__(self)
self.background_color = (125, 158, 192) self.background_color = (125, 158, 192)
self.next_transition = VALID_STATES['STAY'] self.next_transition = VALID_STATES['MENU']
self.cursor_x = 0 self.cursor_x = 0
self.cursor_y = 0 self.cursor_y = 0
@@ -97,6 +103,9 @@ class ScoreState(BaseState):
if self.next_transition != VALID_STATES['STAY']: 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. # 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'] 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 self.letter_index < 3:
# If not all initials are set, check taps on every letter. # If not all initials are set, check taps on every letter.