Python implements the source code of the snake game

The examples in this article share the specific code of python to achieve snake-eating for your reference. The specific content is as follows

import pygame
import sys
import random
SCREEN_X=600
SCREEN_Y=600classSnake(object):
def __init__(self):
self.direction=pygame.K_RIGHT
self.body =[]for i inrange(5):
self.addnode()
def addnode(self):
left, top =(0,0)if self.body:
left, top =(self.body[0].left,self.body[0].top)
node = pygame.Rect(left,top,25,25)if self.direction == pygame.K_RIGHT:
node.left +=25
elif self.direction == pygame.K_LEFT:
node.left -=25
elif self.direction == pygame.K_UP:
node.top -=25
elif self.direction == pygame.K_DOWN:
node.top +=25
self.body.insert(0,node)
def delnode(self):
self.body.pop()
def isdead(self):if self.body[0].x not inrange(SCREEN_X):return True
if self.body[0].y not inrange(SCREEN_Y):return True
if self.body[0]in self.body[1:]:print('in body')return True
return False
def move(self):
self.addnode()
self.delnode()
def changeddirection(self,curkey):
LR =[pygame.K_LEFT,pygame.K_RIGHT]
UD =[pygame.K_DOWN,pygame.K_UP]if curkey in LR+UD:if(curkey in LR)and(self.direction in LR):returnif(curkey in UD)and(self.direction in UD):return
self.direction = curkey
classFood(object):
def __init__(self):
self.rect = pygame.Rect(-25,0,25,25)
def remove(self):
self.rect.x =-25
def set(self):if self.rect.x ==-25:
allpos =[]for pos inrange(25,SCREEN_X-25,25):
allpos.append(pos)
self.rect.left = random.choice(allpos)
self.rect.top = random.choice(allpos)print(self.rect)
def show_text(screen,pos,text,color,font_bold = False,font_size =60,font_italic = False):
cur_font=pygame.font.SysFont('Song Ti',font_size)
cur_font.set_bold(font_bold)
cur_font.set_italic(font_italic)
text_fmt = cur_font.render(text,1,color)
screen.blit(text_fmt,pos)
def main():
pygame.init()
screen_size =(SCREEN_X,SCREEN_Y)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Snake')
clock=pygame.time.Clock()
scores=0
isdead=False
snake=Snake()
food=Food()while True:for event in pygame.event.get():if event.type == pygame.QUIT:
sys.exit()if event.type == pygame.KEYDOWN:
# print(event)
# pressed_keys = pygame.key.get_pressed()
# if pressed_keys.count(1)1:
# continueprint(event.key)
snake.changeddirection(event.key)if event.key == pygame.K_SPACE and isdead:returnmain()
screen.fill((255,255,255))if not isdead:
scores +=1
snake.move()for rect in snake.body:
pygame.draw.rect(screen,(20,220,39),rect,0)
isdead=snake.isdead()if isdead:show_text(screen,(100,200),'you dead!',(227,29,18),False,100)show_text(screen,(150,260),'press space to try again...',(0,0,22),False,30)if food.rect == snake.body[0]:
scores +=50
food.remove()
snake.addnode()
food.set()
pygame.draw.rect(screen,(136,0,21),food.rect,0)show_text(screen,(50,500),'Scores:'+str(scores),(223,223,223))
pygame.display.update()
clock.tick(5)if __name__ =="__main__":main()

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements the source code of the snake game
Python implements the brick-and-mortar game
Python solves the Tower of Hanoi game
Python implements the sum of fractional sequences
200 lines of Python code to achieve snake
Python implements tic-tac-toe game
Python implements the shuffling of the cards in Doudizhu
Python implements Tetris game
Python implements minesweeper game
Python writes the game implementation of fishing master
Python implements guessing game
Consolidate the foundation of Python (4)
python guess the word game
Python implements WeChat airplane game
Python implements word guessing game
Consolidate the foundation of Python(6)
Python implements a guessing game
Python realizes the guessing game
Python implements simple tic-tac-toe game
Consolidate the foundation of Python(5)
Python implements code block folding
Python implements verification code recognition
Consolidate the foundation of Python (3)
Minimalism is the soul of Python | Python code to find bugs (10)
The usage of wheel in python
How Python implements the mail function
Python handles the 4 wheels of Chinese
Python3 implements the singleton design pattern
Python simulation of the landlord deal
What is the use of Python
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
The premise of Python string pooling
Secrets of the new features of Python 3.8
How Python implements the timer function
Python implements the aircraft war project
The father of Python joins Microsoft
Python implements horizontal stitching of pictures
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Python implements alternate execution of two threads
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Solve the conflict of multiple versions of python
What is the scope of python variables
Two days of learning the basics of Python
What is the id function of python
Python basic actual combat-guess the age game
Where is the pip path of python3
The essence of Python language: Itertools library
python implements the gradient method python the fastest descent method
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development