Writing Python Game project , The most important time python Medium pygame library . install pygame Library and usage in me CSDN Another blog post . I won't go into details here . Run the game interface at the bottom .

Below is the detailed code and comments
import pygame,sys,random,time from pygame.locals import * # from pygame Modules import common functions and constants
# Define color variables black_colour = pygame.Color(0,0,0) white_colour =
pygame.Color(255,255,255) red_colour = pygame.Color(255,0,0) 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) # Sets the color of the prompt font
GameOver_colour = GameOver_font.render('Game Over',True,grey_colour) # Set prompt position
GameOver_location = GameOver_colour.get_rect() GameOver_location.midtop =
(320,10) # 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()
# Defining 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] # Initializes the length of the greedy snake snakelength = [[100,100],[80,100],[60,100]] # Initializes the position of the target block
square_purpose = [300,300] # Initializes 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 game loop while
True: # Detection keys, 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:
# Judging keyboard events , use w,s,a,d To show up, down, left and 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 block if snakeposition[0]==square_purpose[0] and
snakeposition[1]==square_purpose[1]: square_position = 0 else:
snakelength.pop() # Regenerate target box if square_position ==0: # Random generation x,y, 20 times larger , Within window range 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:
# Refresh pygame Display layer pygame.display.flip() # To judge whether or not to die 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(5) if __name__ == "__main__":



©2019-2020 Toolsou All rights reserved,
C Review of basic language knowledge Go Language learning notes (GUI programming )Java Misunderstanding —— Method overloading is a manifestation of polymorphism ? How to achieve low cost and high stability for cloud native applications ?elementui Shuttle box el-transfer Display list content text too long C/C++ Memory model Element-Ui assembly Message Message prompt , alert Popup C# Making a simplified version of calculator Python In pycharm editor Interface style modification Tiktok refresh progress bar ( Two little balls turn ), The code is simple