Hardware Reference
In-Depth Information
To make the bouncing a bit more interesting, a sound should be produced at the moment of
impact, something that did not occur in the irst incarnations of the game. Finally, the code
makes the bounding rectangle (the rectangle containing the ball) adjustable from the cursor
keys of the keyboard. All this is added to produce the code in Listing 5-2.
Listing 5-2 The Improved Bounce Code
#!/usr/bin/env python
“””
Bounce with sound
A Raspberry Pi test
“””
import time # for delays
import os, pygame, sys
pygame.init() # initialise graphics interface
pygame.mixer.quit()
pygame.mixer.init(frequency=22050, size=-16, ;
channels=2, buffer=512)
bounceSound = pygame.mixer.Sound(“sounds/bounce.ogg”)
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
pygame.display.set_caption(“Bounce2”)
screenWidth = 400
screenHeight =400
screen = pygame.display.set_mode([screenWidth,;
screenHeight],0,32)
background = pygame.Surface((screenWidth,screenHeight))
# define the colours to use for the user interface
cBackground =(255,255,255)
cBlock = (0,0,0)
background.fill(cBackground) # make background colour
box = [screenWidth-80,screenHeight-80]
delta = [5,10]
hw = screenWidth / 2
hh = screenHeight /2
position = [hw,hh] # position of the ball
limit = [0, 0, 0, 0] #wall limits
ballRad = 8 # size of the ball
def main():
global position
updateBox(0,0) # set up wall limits
continued
Search WWH ::




Custom Search