Hardware Reference
In-Depth Information
Listing 5-2 continued
screen.blit(background,[0,0])
while True :
checkForEvent()
time.sleep(0.05)
drawScreen(position)
position = moveBall(position)
def moveBall(p):
global delta
p[0] += delta[0]
p[1] += delta[1]
if p[0] <= limit[0] :
bounceSound.play()
delta[0] = -delta[0]
p[0] = limit[0]
if p[0] >= limit[1] :
bounceSound.play()
delta[0] = -delta[0]
p[0] = limit[1]
if p[1] <= limit[2] :
bounceSound.play()
delta[1] = - delta[1]
p[1] = limit[2]
if p[1] >= limit[3] :
bounceSound.play()
delta[1] = - delta[1]
p[1] = limit[3]
return p
def drawScreen(p) : # draw to the screen
screen.blit(background,[0,0]) # set background colour
pygame.draw.rect(screen,(255,0,0), (hw - ;
(box[0]/2),hh - (box[1]/2),box[0],box[1]), 2)
pygame.draw.circle(screen,cBlock, (p[0], p[1]),ballRad, 2)
pygame.display.update()
def updateBox(d,amount):
global box, limit
box[d] += amount
limit[0] = hw - (box[0]/2) +ballRad #leftLimit
limit[1] = hw + (box[0]/2) -ballRad #rightLimit
limit[2] = hh - (box[1]/2) + ballRad #topLimit
Search WWH ::




Custom Search