The text and pictures of this article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time
This article is from Tencent cloud by Python sophomore
As a classic little game, snake came out as early as 1976. I first came into contact with it in my parents’ Nokia mobile phone.
Although the history of the snake is relatively long, but it has a very tenacious vitality, keep enduring, one of the important reasons is that the game manufacturers continue to update and iterate. Now, this game has become very rich in game scenes and rules.
Next, let’s take a look at how to simply implement this small game through python.
- There should be the main interface of the game, snake and food;
- It can control the snake to move and get food;
- After eating the food, the snake increases its length and score, and the food disappears and randomly generates new food;
- The game ends when the snake touches the surrounding boundary or its own body.
- Operating system: Windows
- Python version: 3.6
- Modules involved: sys, random, pyGame
First, install the third-party library pyGame and use pip to install pyGame.
The main interface of the game
SCREEN_X = 500 SCREEN_Y = 500 screen_size = (SCREEN_X, SCREEN_Y) screen = pygame.display.set_mode(screen_size) pygame.display.set_ Capture ('snake eating ')
A snake eater
def __init__(self): self.dirction = pygame.K_RIGHT self.body = 
def addNode(self): left, top = (0, 0) if self.body: left, top = (self.body.left, self.body.top) node = pygame.Rect(left, top, 20, 20) if self.dirction == pygame.K_LEFT: node.left -= 20 elif self.dirction == pygame.K_RIGHT: node.left += 20 elif self.dirction == pygame.K_UP: node.top -= 20 elif self.dirction == pygame.K_DOWN: node.top += 20 self.body.insert(0, node) def delNode(self): self.body.pop()
def changeDirection(self, curkey): LR = [pygame.K_LEFT, pygame.K_RIGHT] UD = [pygame.K_UP, pygame.K_DOWN] if curkey in LR + UD: if (curkey in LR) and (self.dirction in LR): return if (curkey in UD) and (self.dirction in UD): return self.dirction = curkey
def isDead(self): #Hit the wall if self.body.x not in range(SCREEN_X): return True if self.body.y not in range(SCREEN_Y): return True #Hit yourself if self.body in self.body[1:]: return True return False
Put food in
def set(self): if self.rect.x == -20: allpos =  for pos in range(20, SCREEN_X - 20, 20): allpos.append(pos) self.rect.left = random.choice(allpos) self.rect.top = random.choice(allpos) print(self.rect)
Eat the food
def remove(self): self.rect.x = -20
≠ text display
def show_text(screen, pos, text, color, font_bold=False, font_size=30, font_italic=False): #Set text size cur_ font = pygame.font.SysFont (song style, font)_ size) #Bold cur_font.set_bold(font_bold) #Italics cur_font.set_italic(font_italic) #Set content text_fmt = cur_font.render(text, 1, color) #Draw text screen.blit(text_fmt, pos)
show_text(screen, (50, 400), 'scores: ' + str(scores), (103, 213, 213))
Display death alert
show_text(screen, (150, 50), 'GAME OVER', (227, 29, 18), False, 50) show_text(screen, (140, 100), "Press space to try again", (0, 0, 22), False, 30)
When the greedy snake eats the food and increases the length and score of the snake, the food disappears and is put in again.
if food.rect == snake.body: scores += 1 food.remove() snake.addNode() #Put food in food.set()
The final effect
Package with pyinstall,