Hardware Reference
In-Depth Information
Listing 5-3 continued
# now test to any interaction
if p[1] <= limit[2] : # test top
bounceSound.play()
delta[1] = - delta[1]
p[1] = limit[2]
elif p[1] >= limit[3] : # test bottom
bounceSound.play()
delta[1] = - delta[1]
p[1] = limit[3]
elif p[0] <= limit[0] : # test missed ball player 1
p[0] = limit[0]
rally = False
print “ missed ball”
elif p[0] >= limit[1] : # test missed ball player 2
p[0] = limit[1]
rally = False
print “ missed ball”
# now test left bat limit
elif p[0] <= limit[4] and p[1] >= batY[0] - ballRad ;
and p[1] <= batY[0] + ballRad + batHeight:
bounceSound.play()
p[0] = limit[4]
delta[0] = random.randint(5,15)
if random.randint(1,4) > 2 : ;
# random change in y direction
delta[1] = 16 - delta[0]
else :
delta[1] = -(16 - delta[0])
# Test right bat collision
elif p[0] >= limit[5] and p[1] >= batY[1] - ballRad ;
and p[1] <= batY[1] + ballRad + batHeight:
bounceSound.play()
delta[0] = - delta[0]
p[0] = limit[5]
score+= 1
updateScore()
batY[0] = p[1] - ballRad # make auto opponent follow bat
#batY[1] = p[1]- ballRad # temporary test for auto player
return p
def updateScore():
global score, best, scoreRect, scoreSurface
scoreSurface = font.render(str(best)+” : “+str(score),;
Search WWH ::




Custom Search