Skip to content

Instantly share code, notes, and snippets.

@hortonew
Last active December 11, 2015 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hortonew/4590375 to your computer and use it in GitHub Desktop.
Save hortonew/4590375 to your computer and use it in GitHub Desktop.
Pygame - Play Single Sound
import pygame, sys, os
from pygame.locals import *
running = True
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
#Create path to sound file
mypath = os.path.dirname( os.path.realpath( __file__) )
laser = os.path.join(mypath, 'resources/laser.wav')
#Create sound object for laser
pygame.mixer.init()
sound = pygame.mixer.Sound(laser)
def handleEvents():
global running
for event in pygame.event.get():
if (event.type == QUIT) or (event.type == KEYDOWN and event.key == K_ESCAPE):
running = False
if event.type == KEYDOWN:
if event.key == K_SPACE:
sound.play()
if __name__ == "__main__":
while running:
handleEvents()
pygame.display.flip()
clock.tick(30)
pygame.quit()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment