Hardware Reference
In-Depth Information
limit[3] = (hh + (box[1]/2))-ballRad #bottomLimit
def terminate(): # close down the program
print (“Closing down please wait”)
pygame.quit() # close pygame
sys.exit()
def checkForEvent(): # see if you need to quit
event = pygame.event.poll()
if event.type == pygame.QUIT :
terminate()
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_ESCAPE :
terminate()
if event.key == pygame.K_DOWN : ;
# expand / contract the box
updateBox(1,-2)
if event.key == pygame.K_UP :
updateBox(1,2)
if event.key == pygame.K_LEFT :
updateBox(0,-2)
if event.key == pygame.K_RIGHT :
updateBox(0,2)
if __name__ == '__main__':
main()
You can see here that not only have the functions' names changed but the variables deining the
movement and some other parameters also have changed from being separate named variables
to being items in a list. his makes it easer to pass them into and out of functions. A function is
restricted to returning only one item, but by packing lots of variables into a list you get to return
many under the one name. Note that in this code checking for a collision involves only checking
the position of the ball against predeined limits. his eliminates the need to do calculations such
as adjusting for the ball radius every time you want to do a collision check. Precalculating these
limits helps to speed up the overall program and gets the ball moving faster and smoother.
he sound is handled by loading in a bounce sound. Note here though that the initialisation
of the sound does not use the default values but uses
pygame.mixer.init(frequency=22050, size=-16, ;
channels=2, buffer=512)
Search WWH ::




Custom Search