200 lines of Python code to achieve snake

200 Line Python code to achieve greedy snake#

Not much to say, all the code will be given at the end, or you can start from hereFork, the text;

List of functions currently implemented:

  1. To control the snake, use the up, down, left, and right arrow keys;
  2. If you touch the edge, wall, or yourself, the game ends;
  3. The food disappears when it comes into contact with food, and the body grows according to the type of food;
  4. Current length display;
  5. Pause and death interface;

Running animation##

Code snippet analysis##

Code for drawing each part###

# The background of the game and the background used to display the text at the bottom
def draw_background():
 # white background
 screen.fill(COLORS['white'])
 pygame.draw.rect(screen,COLORS['black'],(-100,GAME_SIZE[1],3000,200),0)

# Draw walls
def draw_wall():for xy in wall_list:
  pygame.draw.rect(screen,COLORS['darkgray'],(xy[0]-WALL_WIDTH/2,xy[1]-WALL_WIDTH/2,WALL_WIDTH,WALL_HEIGHT),0)

# Draw snake, including head and body
def draw_snake():
 head = snake_list[0]
 pygame.draw.circle(screen,COLORS['darkred'],(head[0],head[1]),int(SNAKE_WIDTH/2),0)for xy in snake_list[1:]:
  pygame.draw.rect(screen,COLORS['darkred'],(xy[0]-SNAKE_WIDTH/2,xy[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT),2)

# Draw food
def draw_food():for xyz in food_list:
  pygame.draw.rect(screen,FOOD_COLORS[xyz[2]-1],(xyz[0]-FOOD_WIDTH/2,xyz[1]-FOOD_WIDTH/2,FOOD_WIDTH,FOOD_HEIGHT),0)

# Draw the body length record below
def draw_context():
 txt = FONT_M.render('Snake length: '+str(len(snake_list)-1),True,COLORS['lightblue'])
 x,y =10,GAME_SIZE[1]+(int((SIZE[1]-GAME_SIZE[1])/2))
 y =int(y-FONT_M.size('Count')[1]/2)
 screen.blit(txt,(x,y))

# Draw pause interface
def draw_pause():
 s = pygame.Surface(SIZE, pygame.SRCALPHA)
 s.fill((255,255,255,220))
 screen.blit(s,(0,0))
 txt = FONT_M.render('PAUSE',True,COLORS['darkgray'])
 x,y = SIZE[0]/2,SIZE[1]/2
 x,y =int(x-FONT_M.size('PAUSE')[0]/2),int(y-FONT_M.size('PAUSE')[1]/2)
 screen.blit(txt,(x,y))

# Draw death interface
def draw_dead():
 s = pygame.Surface(SIZE, pygame.SRCALPHA)
 s.fill((255,255,255,240))
 screen.blit(s,(0,0))
 txt = FONT_M.render('YOU DEAD',True,COLORS['black'])
 x,y = SIZE[0]/2,SIZE[1]/2
 x,y =int(x-FONT_M.size('YOU DEAD')[0]/2),int(y-FONT_M.size('YOU DEAD')[1]/2)
 screen.blit(txt,(x,y))

Death and food collision inspection###

# Rectangular coverage check is used as collision detection. The idea is to take the reverse, that is, take the reverse of all the situations that will not be covered.
def rect_cover(rect1,rect2):
 left1 =int(rect1[0])
 right1 =int(rect1[0]+rect1[2])
 up1 =int(rect1[1])
 down1 =int(rect1[1]+rect1[3])
 left2 =int(rect2[0])
 right2 =int(rect2[0]+rect2[2])
 up2 =int(rect2[1])
 down2 =int(rect2[1]+rect2[3])ifnot(right2<=left1 or left2>=right1 or down2<=up1 or up2>=down1):return True
 return False

# Check if you touch food
def check_food():
 # Head and food
 first = snake_list[0]
 snake_head_rect =(first[0]-SNAKE_WIDTH/2,first[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT)for i inrange(len(food_list)):
  xyz = food_list[i]
  food_rect =(xyz[0]-FOOD_WIDTH/2,xyz[1]-FOOD_WIDTH/2,FOOD_WIDTH,FOOD_HEIGHT)ifrect_cover(snake_head_rect,food_rect):add_body(xyz[2])
   del food_list[i]return True
 return False

# Check if you touch the edge, wall or your body
def check_dead():
 first = snake_list[0]
 snake_head_rect =(first[0]-SNAKE_WIDTH/2,first[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT)
 # Head and edge
 if first[0]<0 or first[0]> GAME_SIZE[0] or first[1]<0 or first[1]> GAME_SIZE[1]:return True
 # Head and wall
 for xy in wall_list:
  wall_rect =(xy[0]-WALL_WIDTH/2,xy[1]-WALL_WIDTH/2,WALL_WIDTH,WALL_HEIGHT)ifrect_cover(snake_head_rect,wall_rect):return True
 # Head and self
 for xy in snake_list[1:]:
  body_rect =(xy[0]-SNAKE_WIDTH/2,xy[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT)ifrect_cover(snake_head_rect,body_rect):return True
 return False

Renew food and increase the body length of snakes###

def add_food():while(True):
  xyz =[random.choice(X_LIST),random.choice(Y_LIST),random.choice([1,2,3,4])]if xyz not in wall_list:
   food_list.append(xyz)break
def add_body(length=1):for c inrange(length):
  # Tail plus one section
  last2,last1 = snake_list[-2],snake_list[-1]if last2[0]==last1[0]: #Two vertical paragraphs
   if last2[1]>last1[1]: #Face down
    snake_list.append([last1[0],last1[1]-SNAKE_WIDTH])else:
    snake_list.append([last1[0],last1[1]+SNAKE_WIDTH])else: #Two horizontal sections
   if last2[0]>last1[0]: #To the right
    snake_list.append([last1[0]-SNAKE_WIDTH,last1[1]])else:
    snake_list.append([last1[0]+SNAKE_WIDTH,last1[1]])

Snake's automatic movement###

# Determine the current direction of the snake by pressing the button
if event.key == K_LEFT:if head in['up','down']:
  head ='left'
elif event.key == K_RIGHT:if head in['up','down']:
  head ='right'
elif event.key == K_UP:if head in['left','right']:
  head ='up'
elif event.key == K_DOWN:if head in['left','right']:
  head ='down'

# Judge the snake’s next position by heading
first = snake_list[0]
snake_list[1:]= snake_list[:-1]if head =='up':
 snake_list[0]=[first[0],first[1]-SNAKE_WIDTH]
elif head =='down':
 snake_list[0]=[first[0],first[1]+SNAKE_WIDTH]
elif head =='left':
 snake_list[0]=[first[0]-SNAKE_WIDTH,first[1]]
elif head =='right':
 snake_list[0]=[first[0]+SNAKE_WIDTH,first[1]]

All codes##

import sys,random

import pygame
from pygame.color import THECOLORS as COLORS
from pygame.locals import*

def draw_background():
 # white background
 screen.fill(COLORS['white'])
 pygame.draw.rect(screen,COLORS['black'],(-100,GAME_SIZE[1],3000,200),0)

def draw_wall():for xy in wall_list:
  pygame.draw.rect(screen,COLORS['darkgray'],(xy[0]-WALL_WIDTH/2,xy[1]-WALL_WIDTH/2,WALL_WIDTH,WALL_HEIGHT),0)

def draw_snake():
 head = snake_list[0]
 pygame.draw.circle(screen,COLORS['darkred'],(head[0],head[1]),int(SNAKE_WIDTH/2),0)for xy in snake_list[1:]:
  pygame.draw.rect(screen,COLORS['darkred'],(xy[0]-SNAKE_WIDTH/2,xy[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT),2)

def draw_food():for xyz in food_list:
  pygame.draw.rect(screen,FOOD_COLORS[xyz[2]-1],(xyz[0]-FOOD_WIDTH/2,xyz[1]-FOOD_WIDTH/2,FOOD_WIDTH,FOOD_HEIGHT),0)

def draw_context():
 txt = FONT_M.render('Snake length: '+str(len(snake_list)-1),True,COLORS['lightblue'])
 x,y =10,GAME_SIZE[1]+(int((SIZE[1]-GAME_SIZE[1])/2))
 y =int(y-FONT_M.size('Count')[1]/2)
 screen.blit(txt,(x,y))

def draw_pause():
 s = pygame.Surface(SIZE, pygame.SRCALPHA)
 s.fill((255,255,255,220))
 screen.blit(s,(0,0))
 txt = FONT_M.render('PAUSE',True,COLORS['darkgray'])
 x,y = SIZE[0]/2,SIZE[1]/2
 x,y =int(x-FONT_M.size('PAUSE')[0]/2),int(y-FONT_M.size('PAUSE')[1]/2)
 screen.blit(txt,(x,y))

def draw_dead():
 s = pygame.Surface(SIZE, pygame.SRCALPHA)
 s.fill((255,255,255,240))
 screen.blit(s,(0,0))
 txt = FONT_M.render('YOU DEAD',True,COLORS['black'])
 x,y = SIZE[0]/2,SIZE[1]/2
 x,y =int(x-FONT_M.size('YOU DEAD')[0]/2),int(y-FONT_M.size('YOU DEAD')[1]/2)
 screen.blit(txt,(x,y))

def rect_cover(rect1,rect2):
 left1 =int(rect1[0])
 right1 =int(rect1[0]+rect1[2])
 up1 =int(rect1[1])
 down1 =int(rect1[1]+rect1[3])
 left2 =int(rect2[0])
 right2 =int(rect2[0]+rect2[2])
 up2 =int(rect2[1])
 down2 =int(rect2[1]+rect2[3])ifnot(right2<=left1 or left2>=right1 or down2<=up1 or up2>=down1):return True
 return False

def add_food():while(True):
  xyz =[random.choice(X_LIST),random.choice(Y_LIST),random.choice([1,2,3,4])]if xyz not in wall_list:
   food_list.append(xyz)break
def add_body(length=1):for c inrange(length):
  # Tail plus one section
  last2,last1 = snake_list[-2],snake_list[-1]if last2[0]==last1[0]: #Two vertical paragraphs
   if last2[1]>last1[1]: #Face down
    snake_list.append([last1[0],last1[1]-SNAKE_WIDTH])else:
    snake_list.append([last1[0],last1[1]+SNAKE_WIDTH])else: #Two horizontal sections
   if last2[0]>last1[0]: #To the right
    snake_list.append([last1[0]-SNAKE_WIDTH,last1[1]])else:
    snake_list.append([last1[0]+SNAKE_WIDTH,last1[1]])

def check_food():
 # Head and food
 first = snake_list[0]
 snake_head_rect =(first[0]-SNAKE_WIDTH/2,first[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT)for i inrange(len(food_list)):
  xyz = food_list[i]
  food_rect =(xyz[0]-FOOD_WIDTH/2,xyz[1]-FOOD_WIDTH/2,FOOD_WIDTH,FOOD_HEIGHT)ifrect_cover(snake_head_rect,food_rect):add_body(xyz[2])
   del food_list[i]return True
 return False

def check_dead():
 first = snake_list[0]
 snake_head_rect =(first[0]-SNAKE_WIDTH/2,first[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT)
 # Head and edge
 if first[0]<0 or first[0]> GAME_SIZE[0] or first[1]<0 or first[1]> GAME_SIZE[1]:return True
 # Head and wall
 for xy in wall_list:
  wall_rect =(xy[0]-WALL_WIDTH/2,xy[1]-WALL_WIDTH/2,WALL_WIDTH,WALL_HEIGHT)ifrect_cover(snake_head_rect,wall_rect):return True
 # Head and self
 for xy in snake_list[1:]:
  body_rect =(xy[0]-SNAKE_WIDTH/2,xy[1]-SNAKE_WIDTH/2,SNAKE_WIDTH,SNAKE_HEIGHT)ifrect_cover(snake_head_rect,body_rect):return True
 return False

if __name__ =="__main__":
 # init pygame
 pygame.init()
    
 # contant
 GAME_SIZE =[900,900]
 SIZE =[GAME_SIZE[0],GAME_SIZE[1]+100]
 FONT_S = pygame.font.SysFont('Times',50)
 FONT_M = pygame.font.SysFont('Times',90)
 DIRECTION =['up','right','down','left']
 X_LIST =[x for x inrange(GAME_SIZE[0])]
 Y_LIST =[y for y inrange(GAME_SIZE[1])]
 FOOD_COLORS =((46,139,87),(199,21,133),(25,25,112),(255,215,0))

 # wall
 wall_list =[[100,200],[600,500],[350,200],[500,800]]
 WALL_WIDTH,WALL_HEIGHT =30,30

 # food
 food_list =[(150,200,1),(300,500,1),(740,542,1),(300,600,1),(700,600,1)]
 FOOD_WIDTH,FOOD_HEIGHT =14,14
    
 # create screen 500*500
 screen = pygame.display.set_mode(SIZE)
    
 # variable parameter
 snake_list =[[100+12*4,100],[100+12*3,100],[100+12*2,100],[100+12*1,100],[100,100]]
 SNAKE_WIDTH,SNAKE_HEIGHT =12,12
 snake_v =0
 count_time =0

 # level
 frame =0.05
 level =1
    
 # main loop
 running = True
 pause = False
 dead = False
 head ='right'while running:for event in pygame.event.get():if event.type == pygame.QUIT:
    running = False
    break
   elif event.type == pygame.MOUSEBUTTONDOWN:
    pause = not pause
   elif event.type == pygame.KEYUP:if event.key == K_LEFT:if head in['up','down']:
      head ='left'
    elif event.key == K_RIGHT:if head in['up','down']:
      head ='right'
    elif event.key == K_UP:if head in['left','right']:
      head ='up'
    elif event.key == K_DOWN:if head in['left','right']:
      head ='down'

  # update data
  if not pause and not dead:
   count_time += frame*level
   first = snake_list[0]
   snake_list[1:]= snake_list[:-1]if head =='up':
    snake_list[0]=[first[0],first[1]-SNAKE_WIDTH]
   elif head =='down':
    snake_list[0]=[first[0],first[1]+SNAKE_WIDTH]
   elif head =='left':
    snake_list[0]=[first[0]-SNAKE_WIDTH,first[1]]
   elif head =='right':
    snake_list[0]=[first[0]+SNAKE_WIDTH,first[1]]

  # background
  draw_background()
  # tunnel
  draw_wall()
  # choose item
  draw_snake()
  # food
  draw_food()
  # point
  draw_context()
  # pause
  if not dead and pause:draw_pause()
  # dead
  if dead:draw_dead()
  # flip
  pygame.display.flip()

  # pause 20ms
  pygame.time.delay(int(frame/level*1000))

  # check win or not
  dead =check_dead()ifcheck_food():add_food()
    
 pygame.quit()

Recommended Posts

200 lines of Python code to achieve snake
500 lines of python code to achieve aircraft war
Python implements the source code of the snake game
Python code to find bugs (2)
Python code to find bugs(7)
How to comment python code
Python code to find bugs (3)
Python code to find bugs(9)
Python code to find bugs(6)
Python code to find bugs (1)
Python code to find bugs(8)
Python code to find bugs(5)
Minimalism is the soul of Python | Python code to find bugs (10)
python how to view webpage code
Use python to achieve stepwise regression
How to wrap in python code
Python handles operation code to execl
How to verify successful installation of python
Does Python code need to be indented
7 features of Python3.9
Actually very simple | Python code to find bugs (12)
How to use code running assistant in python
How to set code auto prompt in python
Implementation of Python headless crawler to download files
Python how to move the code collectively right
Detailed examples of using Python to calculate KS
Introduction to Python
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
Python simulation to realize the distribution of playing cards
Centos 6.4 python 2.6 upgrade to 2.7
Basics of Python syntax
Python SMS bombing code
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
09. Common modules of Python3
Use jenkins to automatically pull code cloud code to achieve automated deployment
How to find the area of a circle in python