This article mainly introduces a most simple animation – the cat is running.

Import PyGame,sys,time from PyGame.locals Import * # Initialize the PyGame library PyGame.init () # set up a 400*400 pixel window D = Pygame.display.set_mode ((400,400)) # set the top title of the window, Display. Set_caption (" Cat is running") # Initialize the PyGame.time.clock object to help ensure that the program runs at a certain maximum FPS. This Clock object ensures that our game program does not run too fast by pausing in each while loop below. Without these pauses, the animation of the while loop here will run as fast as the computer can run, and the user won't be able to keep up. C = pygame.time.clock () # initialize the background COLOR COLOR = (255,255,255) # initialize the cat's x coordinate catx = 0 # initialize the cat's y coordinate caty = 0 # Initialize the cat's x direction movement speed Speedy = 5 # initializes the cat's movement in the y direction direction = 'r' # loads the cat's picture cat = Pygame. Image. The load ("/Users/wys/Desktop/the PNG ") # initialization FPS, namely the following per second while loop will execute 30 FPS = 30 # assurance program has been running not quit while True: If direction == 'r': catx+=speedx if catx==275: If direction == 'd': caty+=speedy if caty==320: If direction == 'l': catx-=speedx: =0: direction == 'u': caty-=speedy if caty==0: D.blit (cat,(catx,caty)) # for the event in pygame.event.get(): If event.type == QUIT: Quit () # End sys.exit() # refresh window effect pygame.display.update() # set FPS for Clock object C, C. twick (FPS)Copy the code

The kitten image used here can be obtained from here at 125 * 79 pixels. The renderings are as follows: