Hardware Reference
In-Depth Information
Now when you consider bat-and-ball collisions you must look at both the X and Y elements of the
coordinates of the bat and ball. Figure 5-7 summarises this by showing both sets of conditions
that have to be met. his is complicated by the fact that whereas the X coordinate of the bat is
ixed, the Y coordinate is going to change with input from the player. Note here that unlike con-
ventional coordinates, Pygame has the Y-axis numbers increase as you move down the screen.
Figure 5-7:
he collision
geometry of the
bat and ball.
he overall structure of the game also needs to be deined. Previously there was just a ball
bouncing around the screen. Now you have to have more code to deine the various stages of
the game. Each game consists of three balls, and a rally is the time one ball spends in play.
he score advances each time you hit the ball, and a ball is lost when it collides against the
far-right bounding box. After each ball is lost, the automatic left-hand player serves a new
ball at a randomly chosen speed and direction. his structure needs to be imposed on the
simple game mechanics. Let's see how all this comes together in Listing 5-3.
Listing 5-3 The Single-Player Ping Game
#!/usr/bin/env python
“””
Ping - Tennis game one 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”)
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
continued
Search WWH ::




Custom Search