In self-study Python In the process of searching information on the Internet, I found some interesting things ,python Game library module of , It can play a little game by itself , Then I found some game codes on the Internet ,, I changed some of them myself , It's a simple Snake , The code was also typed as it was, just a little bit changed
import pygame, sys, random, time from pygame.locals import * #
from pygame Modules import commonly used functions and constants # Define color variables black_colour = pygame.Color(28, 56, 20)
white_colour = pygame.Color(255, 144, 20) red_colour = pygame.Color(255,34 ,
20) grey_colour = pygame.Color(150, 150, 150) # Define the end of game function def
GameOver(gamesurface): # Format prompt font GameOver_font =
pygame.font.SysFont("MicrosoftYaHei", 16) # Set the color of the prompt font GameOver_colour =
GameOver_font.render('GameOver', True, grey_colour)# English only # Set prompt location
GameOver_location = GameOver_colour.get_rect() GameOver_location.midtop = (310,
200) # Bind the above settings to the handle gamesurface.blit(GameOver_colour, GameOver_location) # Prompt operation information
pygame.display.flip() # dormancy 5 second time.sleep(5) # Quit the game pygame.quit() # Exit the program
sys.exit() # Define the main function def main(): # initialization pygame, Prepare for hardware pygame.init()
pygame.time.Clock() ftpsClock = pygame.time.Clock() # Create a window gamesurface =
pygame.display.set_mode((640, 480)) # Set the title of the window
pygame.display.set_caption('tanchishe snake') # initialize variable # Initializes the starting position of the snake
snakeposition = [100, 100] # Initialize the length of the snake snakelength = [[100, 100], [80, 100],
[60, 100]] # Initializes the location of the target block square_purpose = [300, 300] # Initialize a number to determine whether the target block exists
square_position = 1 # Initialization direction , Used to make the snake move derection = "right" change_derection =
derection # Play the main loop of the game while True: # Test key, etc pygame event for event in
pygame.event.get(): if event.type == QUIT: # After receiving the exit event , Exit the program pygame.quit()
sys.exit() elif event.type == KEYDOWN: # Judge keyboard events , use w,s,a,d Up, down, left, right if event.key ==
K_RIGHT or event.key == ord('d'): change_derection = "right" if event.key ==
K_LEFT or event.key == ord('a'): change_derection = "left" if event.key == K_UP
or event.key == ord('w'): change_derection = "up" if event.key == K_DOWN or
event.key == ord('s'): change_derection = "down" if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT)) # Judge whether the direction of movement is opposite if change_derection
== 'left' and not derection == 'right': derection = change_derection if
change_derection == 'right' and not derection == 'left': derection =
change_derection if change_derection == 'up' and not derection == 'down':
derection = change_derection if change_derection == 'down' and not derection ==
'up': derection = change_derection # According to the direction , Change the coordinates if derection == 'left':
snakeposition[0] -= 20 if derection == 'right': snakeposition[0] += 20 if
derection == 'up': snakeposition[1] -= 20 if derection == 'down':
snakeposition[1] += 20 # Increase the length of the snake snakelength.insert(0, list(snakeposition)) #
Determine whether to eat the target box if snakeposition[0] == square_purpose[0] and snakeposition[1] ==
square_purpose[1]: square_position = 0 else: snakelength.pop() # Regenerate target block if
square_position == 0: # Random generation x,y, Twenty fold increase , Within the window x = random.randrange(1, 32) y =
random.randrange(1, 24) square_purpose = [int(x * 20), int(y * 20)]
square_position = 1 # draw pygame Display layer gamesurface.fill(black_colour) for position
in snakelength: pygame.draw.rect(gamesurface, white_colour, Rect(position[0],
position[1], 20, 20)) pygame.draw.rect(gamesurface, red_colour,
Rect(square_purpose[0], square_purpose[1], 20, 20)) # Refresh pygame Display layer
pygame.display.flip() # Judge whether death or not if snakeposition[0] < 0 or snakeposition[0] >
620: GameOver(gamesurface) if snakeposition[1] < 0 or snakeposition[1] > 460:
GameOver(gamesurface) for snakebody in snakelength[1:]: if snakeposition[0] ==
snakebody[0] and snakeposition[1] == snakebody[1]: GameOver(gamesurface) #
Control game speed ftpsClock.tick(8) if __name__ == "__main__": main()

Technology