Hardware Reference
In-Depth Information
not only have to return the ball, but you also have the opportunity of changing the light of the
ball to make it more diicult for your opponent to return. In a real game of Ping-Pong, this is done
by adding top spin to the ball. In your Ping game you can simulate the same sort of efect by hav-
ing more delta movement in the Y direction, the further from the center of the bat the collision
occurs. his involves a further calculation once the collision has been detected. he other thing
that needs changing is the method of serving. he serve goes to the player who has just lost the
point, and there needs to be a bit of an element of surprise for the opposing player. herefore, the
serve can be played early by pressing a key, but if the player waits too long, the serve will happen
automatically. All these changes can be seen in Listing 5-4, the two-player game.
Listing 5-4 The Two-Player Game of Ping
#!/usr/bin/env python
“””
Ping - Tennis game two player
with score
For the Raspberry Pi
“””
import time # for delays
import os, pygame, sys
import random
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”)
outSound = pygame.mixer.Sound(“sounds/out.ogg”)
p0hitSound = pygame.mixer.Sound(“sounds/hit0.ogg”)
p1hitSound = pygame.mixer.Sound(“sounds/hit1.ogg”)
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
pygame.display.set_caption(“Ping 2 players”)
screenWidth = 500
screenHeight =300
screen = pygame.display.set_mode([screenWidth,;
screenHeight],0,32)
background = pygame.Surface((screenWidth,screenHeight))
textSize = 36
scoreSurface = pygame.Surface((textSize,textSize))
font = pygame.font.Font(None, textSize)
pygame.event.set_allowed(None)
pygame.event.set_allowed([pygame.KEYDOWN,pygame.QUIT])
# define the colours to use for the user interface
Search WWH ::




Custom Search