Instant Pygame for Python Game Development How-to by Idris Ivan

Instant Pygame for Python Game Development How-to by Idris Ivan

Author:Idris, Ivan [Idris, Ivan]
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 0101-01-01T00:00:00+00:00


Pausing the game: Sometimes we need to pause the execution of a game, as in our case in order to be able to hear a sound. We can do this with the following code snippet:pygame.time.delay(TIMEOUT * 1000)

The delay is specified in milliseconds, that's why we are multiplying by 1000.

Stopping the sound: After a while we need to stop the sound with the corresponding stop method:audio.stop()

The audio demo code is listed as follows:

import pygame, sys from pygame.locals import * import numpy import urllib2 import time WAV_FILE = 'smashingbaby.wav' def play(): audio = pygame.mixer.Sound(WAV_FILE) audio.play(-1) TIMEOUT = 1 pygame.time.delay(TIMEOUT * 1000) audio.stop() time.sleep(TIMEOUT) pygame.init() pygame.display.set_caption('Sound Demo') response = urllib2.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav') filehandle = open(WAV_FILE, 'w') filehandle.write(response.read()) filehandle.close() screen = pygame.display.set_mode((400, 400)) while True: sys_font = pygame.font.SysFont("None", 19) rendered = sys_font.render('Smashing Baby', 0, (255, 100, 100)) screen.blit(rendered, (100, 100)) for event in pygame.event.get(): if event.type == QUIT: play() pygame.quit() sys.exit() pygame.display.update()



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.