[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Symple Key Input and Sounds



Hi!

    For blind users and others, I am giving you a basic program to play sounds, including infinite background for games if your interested. I changed some names so they do not get confused as built in functions, variables, or constants. 

    I even have an exit sound to play, and of course you must delay the exit so it plays.

    I wish to thank Marcus for helping me and I am sure there are many questions to ask. For the next thing is to get arrays setup, tables, and see where it goes from here for us sightless programmer/gamers. 

        Bruce


# Import the necessary ocempgui parts.
from ocempgui.object import ActionListener
from ocempgui.widgets import *
from ocempgui.widgets.Constants import *
from pygame.locals import *
import os.path
import sys
import pygame.mixer, pygame.time
q_mixer = pygame.mixer
q_time = pygame.time

# choose a desired audio format
# (freq, size, stereo, buffersize)
# 1=mono, 2=stereo and 1024 buf size, 2050 for 16 bit
q_mixer.init(21024) #raises exception on fail

# playsound function
def play_sound (File4Sound, Repeats, Max4Time):
# Function play sound on one channel at present path!
    File4Sound = os.path.join('', File4Sound)
    sound = q_mixer.Sound(File4Sound)
    channel = sound.play(Repeats, Max4Time) #(loops+1, max ms-time)


# Pressing a key will print the key its unicode representation to 
#the Pygame screen title bar.
def keydown_handler (keyevent):
# Say voice of key pressed!
#    if keyevent.unicode == K_ESCAPE:
    if keyevent.unicode == chr(K_q):
        re.title = "Pressing Escape Key!"
    else:
        re.title = "Key %s pressed" % keyevent.unicode
#    print "Key %s pressed" % chr(K_q) 

# Exit game command!
    if keyevent.unicode == chr(K_ESCAPE):
        play_sound ("0Fail.Wav", 0, -1)
        q_time.wait(2000)
#        pygame.quit() #Must be global defined.
        sys.exit()
    if keyevent.unicode == chr(K_q):
#        pygame.quit() #Must be global defined.
        sys.exit()

# Say voice of key pressed!
# LRS Sensors!
    if keyevent.unicode == chr(K_l):
        play_sound ("0LRS.wav", 0, -1)

# SRS Sensors!
    if keyevent.unicode == chr(K_s):
        play_sound ("0SRS.wav", 0, -1)

# Location Of Enterprise!
    if keyevent.unicode == chr(K_a):
        play_sound ("0LOC.wav", 0, -1)

# Star Base Control!
    if keyevent.unicode == chr(K_b):
        play_sound ("0BAS.wav", 0, -1)

# Computer Com!
    if keyevent.unicode == chr(K_c):
        play_sound ("0Com.wav", 0, -1)

# Enemy Location!
    if keyevent.unicode == chr(K_e):
        play_sound ("0ENM.wav", 0, -1)

# Probe Control!
    if keyevent.unicode == chr(K_f):
        play_sound ("0PRB.wav", 0, -1)

# Galaxy Map!
    if keyevent.unicode == chr(K_g):
        play_sound ("0GAL.wav", 0, -1)

# Klingon Status!
    if keyevent.unicode == chr(K_k):
        play_sound ("0KLI.wav", 0, -1)

# Navigation Control!
    if keyevent.unicode == chr(K_n):
        play_sound ("0NAV.wav", 0, -1)

# Direction Control!
    if keyevent.unicode == chr(K_d):
        play_sound ("0DIR.wav", 0, -1)

# Warp Control!
    if keyevent.unicode == chr(K_w):
        play_sound ("0Warp.wav", 0, -1)

# Calculate Range Control!
    if keyevent.unicode == chr(K_r):
        play_sound ("0CAL.wav", 0, -1)

# Phaser Control!
    if keyevent.unicode == chr(K_p):
        play_sound ("0PHA.wav", 0, -1)

# Torpedo Control!
    if keyevent.unicode == chr(K_t):
        play_sound ("0TOR.wav", 0, -1)


# object_focused will print a widget's text to the Pygame screen.
def object_focused (gui_widget, SoundFile):
# Function Print object name to title bar and play description!
    re.title = " %s focused" % gui_widget.text
    play_sound (SoundFile, 0, -1)
# Message to console commented out!
#    print "Element %s is focused" % gui_widget.text

# Create the first button with no text and place it at screen offset 3,3
button_one = Button ()
button_one.topleft = 3, 3

# Create a red background color for the first button and assign it a red
# label with the text "Button number one".
label_one = Label ("Button one")
label_one.create_style()
label_one.style["bgcolor"][STATE_NORMAL] = (255, 0, 0)
button_one.create_style()
button_one.style["bgcolor"][STATE_NORMAL] = (255, 0, 0)
button_one.child = label_one

# Connect the focus event (for tab).
# Play name of object focused!
button_one.connect_signal (SIG_FOCUSED, object_focused, button_one, 

"0Command-P.wav")

# Connect the mouse enter event to the button to have some "hover" effect.
# Play name of button hovered!
button_one.connect_signal (SIG_ENTER, object_focused, button_one, 

"0Command-P.wav")

#  Now when clicked play name of action of button.
button_one.connect_signal (SIG_CLICKED, object_focused, button_one, 

"0Command.wav")

# Create the second button with no text and place it 14 pixels to the right
# of the first one.
button_two = Button ()
button_two.topleft = button_one.left + 14, button_one.bottom

# Create a green background color for the second button and assign it a
# green label with the text "Button number two".
label_two = Label ("Button two")
label_two.create_style()
label_two.style["bgcolor"][STATE_NORMAL] = (0, 255, 0)
button_two.create_style()
button_two.style["bgcolor"][STATE_NORMAL] = (0, 255, 0)
button_two.child = label_two

# Connect the focus event (for tab or mouse "hover") of the second button.
# Play name of button focused!
button_two.connect_signal (SIG_FOCUSED, object_focused, button_two, 

"0LRS.wav")

# Connect the mouse movement event to the button to have some "hover" effect.
# Play name of button hovered!
button_two.connect_signal (SIG_ENTER, object_focused, button_two, "0LRS.wav")

#  Now when clicked play name of action of button.
button_two.connect_signal (SIG_CLICKED, object_focused, button_two, 

"0SRS.wav")

# Create the renderer object.
re = Renderer ()

# Create the action listener and register key presses.
listener = ActionListener ()
listener.connect_signal (SIG_KEYDOWN, keydown_handler)

# Assign the listener to the first event manager of the Renderer, so it
# recognizes key presses.
listener.manager = re.managers[0]

# Let the renderer create a pygame screen with 200 pixels width and 200
# pixels height.
re.create_screen (200, 200)

# Set the window title to "Two button demo".
re.title = "Two button demo"

# Set the screen background color to a grey RGB value.
re.color = (200, 200, 200)

# Add the buttons to the screen.
re.add_widget (button_one, button_two)

play_sound ("0Bridge.Wav", -1, -1)
# Start the main loop for drawing and event processing.
re.start ()