Hardware Reference
In-Depth Information
screen.blit(background,[0,0]) # set background colour
pygame.draw.rect(screen,cBlock, (px, py, 10 ,10), 0)
pygame.display.update()
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 and ;
event.key == pygame.K_ESCAPE :
terminate()
if __name__ == '__main__':
main()
You will see that the direction of the block is deined by the two variables dx and dy (for
delta X and delta Y) and is ixed at ive pixels in the X direction and ten in the Y direction per
frame. he square is drawn in solid black on a white background.
he main function irst of all deines the square in the center of the screen, and goes into an
endless loop in which it checks the keyboard for a quit and draws the screen. hen it updates
the square's position and checks for any collision between the square and the sides of the
window. Note as the sides of the window are not a drawn line, there is no need to account for
the line thickness here.
In the main loop there is a sleep command, but this is commented out with a # , which
means that it is ignored. Try removing the # and see how much slower the block will move
around. Also experiment with changing the values of dx and dy and see how this changes
both the speed and the angle of trajectory of the block. Finally, have a play with adjusting the
screenWidth and screenHeight variables.
Improving the Ping Game
Well, Listing 5-1 is a good start, but a number of things are not quite right in it. First of all,
the object bouncing about is a square, and although that is true to the original game, you can
do much better these days. So let's make the object a circle instead of a square. his will
Search WWH ::




Custom Search