Python 100 lines to implement a simple snake game (with code)

1. Game introduction

Snake is a very simple game, suitable for practice. Let's take a look at my screenshots of the game:

game screenshot

How to play:
Enter key: Start the game
Space bar: Pause/Continue
↑↓←→ Arrow keys or WSAD keys: Control the direction of movement.

There are three types of food: red, green, and blue, which correspond to 10 points, 20 points, and 30 points respectively. Every time you eat a food, you will increase the corresponding points. Every time you increase 100 points, the speed will increase by one level. There is no checkpoint set. Soon, and then GAME OVER.

2. Game analysis

The game of greedy snake is very simple, a dot appears randomly on the screen, indicating "food", control the movement of the "snake" up, down, left, and right, after eating the "food", the body of the "snake" lengthens, and the "snake" touches the frame or itself body, the game ends.

Let's first analyze what points need to be paid attention to when writing this game.

1. How do snakes represent?
We can divide the entire game area into small grids. A group of small grids connected together form a "snake". We can use different colors to represent it. As shown in the above picture, I use dark colors Indicates the background, and the light color indicates "snake".

We can use coordinates to represent each small square, and the range of X-axis and Y-axis can be set. Use a list to store the coordinates of the "snake body", then a "snake" will come out, and finally it only needs to be displayed in different colors.

2. How does the snake move?
The first reaction is to move each small square forward one space like an earthworm, but it is very troublesome to realize. It was stuck here from the beginning.

Imagine the greedy snake we have played. Every time the "snake" moves, it feels like it moves forward as a whole. Exclude the "action" of the "snake" in your mind, and think carefully about the "snake" before and after moving. In fact, except for the head and tail, the other parts have not changed at all. That's easy, adding the coordinates of the next space to the beginning of the list and removing the last element of the list is equivalent to moving the snake forward one space.

3. How to determine the end of the game?
If the "snake" moves beyond the scope of the game area or touches itself, it will be considered a loss. The range of the axis coordinates is predetermined, and it is easy to judge if it exceeds the range. So how do you judge when you meet yourself?

If the picture of the "snake" moving in your mind is really difficult, but in the code, our "snake" is a list, then we only need to judge whether the coordinates of the next grid are included in the "snake" Wouldn't it be on the list?

With these questions sorted out, we can start coding.

3. Code implementation
Since the program needs to frequently add and delete the head and tail of the "snake", for better performance, we use deque instead of list.

First, the "snake" needs to be initialized. The initial length of the "snake" is 3, and the position is located in the upper left corner.

# 游戏区域的坐标范围
SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1)
SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1)
# 初始化蛇
snake = deque()
def _init_snake():
    snake.clear()
    snake.append((2, scope_y[0]))
    snake.append((1, scope_y[0]))
    snake.append((0, scope_y[0]))
创建“食物”,在屏幕内随机选取一个点作为“食物”,但是要保证“食物”不在“蛇”身上。
#食物
def create_food(snake):
    food_x = random.randint(SCOPE_X[0], SCOPE_X[1])
    food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])
    while (food_x, food_y) in snake:
        # 如果食物出现在蛇身上,则重来
        food_x = random.randint(SCOPE_X[0], SCOPE_X[1])
        food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])
    return food_x, food_y

The movement of the "snake" can have 4 directions, and a tuple is used to represent the direction of movement. Each time the direction key is pressed, the corresponding value is assigned to

# 方向
pos = (1, 0)

for event in pygame.event.get():
    if event.type == QUIT:
        sys.exit()
    elif event.type == KEYDOWN:
        if event.key in (K_w, K_UP):
            # 这个判断是为了防止蛇向上移时按了向下键,导致直接 GAME OVER
            if pos[1]:
                pos = (0, -1)
        elif event.key in (K_s, K_DOWN):
            if pos[1]:
                pos = (0, 1)
        elif event.key in (K_a, K_LEFT):
            if pos[0]:
                pos = (-1, 0)
        elif event.key in (K_d, K_RIGHT):
            if pos[0]:
                pos = (1, 0)

The movement of the "snake" can be expressed as:

next_s = (snake[0][0] + pos[0], snake[0][1] + pos[1])
if next_s == food:
    # 吃到了食物
    snake.appendleft(next_s)
    food = create_food(snake)
else:
    if SCOPE_X[0] <= next_s[0] <= SCOPE_X[1] and SCOPE_Y[0] <= next_s[1] <= SCOPE_Y[1] and next_s not in snake:
        snake.appendleft(next_s)
        snake.pop()
    else:
        game_over = True

full code

"""
贪吃蛇小游戏
"""
import random
import sys
import time
import pygame
from pygame.locals import *
from collections import deque
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 480
SIZE = 20
def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
    imgText = font.render(text, True, fcolor)
    screen.blit(imgText, (x, y))
def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('贪吃蛇')
    light = (100, 100, 100)  # 蛇的颜色
    dark = (200, 200, 200)  # 食物颜色

    font1 = pygame.font.SysFont('SimHei', 24)  # 得分的字体
    font2 = pygame.font.Font(None, 72)  # GAME OVER 的字体
    red = (200, 30, 30)  # GAME OVER 的字体颜色
    fwidth, fheight = font2.size('GAME OVER')
    line_width = 1  # 网格线宽度
    black = (0, 0, 0)  # 网格线颜色
    bgcolor = (40, 40, 60)  # 背景色
    # 方向,起始向右
    pos_x = 1
    pos_y = 0
    # 如果蛇正在向右移动,那么快速点击向下向左,由于程序刷新没那么快,向下事件会被向左覆盖掉,导致蛇后退,直接GAME OVER
    # b 变量就是用于防止这种情况的发生
    b = True
    # 范围
    scope_x = (0, SCREEN_WIDTH // SIZE - 1)
    scope_y = (2, SCREEN_HEIGHT // SIZE - 1)
    # 蛇
    snake = deque()
    # 食物
    food_x = 0
    food_y = 0
    # 初始化蛇
    def _init_snake():
        nonlocal snake
        snake.clear()
        snake.append((2, scope_y[0]))
        snake.append((1, scope_y[0]))
        snake.append((0, scope_y[0]))
    # 食物
    def _create_food():
        nonlocal food_x, food_y
        food_x = random.randint(scope_x[0], scope_x[1])
        food_y = random.randint(scope_y[0], scope_y[1])
        while (food_x, food_y) in snake:
            # 为了防止食物出到蛇身上
            food_x = random.randint(scope_x[0], scope_x[1])
            food_y = random.randint(scope_y[0], scope_y[1])
    _init_snake()
    _create_food()
    game_over = True
    start = False  # 是否开始,当start = True,game_over = True 时,才显示 GAME OVER
    score = 0  # 得分
    orispeed = 0.5  # 原始速度
    speed = orispeed
    last_move_time = None
    pause = False  # 暂停

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_RETURN:
                    if game_over:
                        start = True
                        game_over = False
                        b = True
                        _init_snake()
                        _create_food()
                        pos_x = 1
                        pos_y = 0
                        # 得分
                        score = 0
                        last_move_time = time.time()
                elif event.key == K_SPACE:
                    if not game_over:
                        pause = not pause
                elif event.key in (K_w, K_UP):
                    # 这个判断是为了防止蛇向上移时按了向下键,导致直接 GAME OVER
                    if b and not pos_y:
                        pos_x = 0
                        pos_y = -1
                        b = False
                elif event.key in (K_s, K_DOWN):
                    if b and not pos_y:
                        pos_x = 0
                        pos_y = 1
                        b = False
                elif event.key in (K_a, K_LEFT):
                    if b and not pos_x:
                        pos_x = -1
                        pos_y = 0
                        b = False
                elif event.key in (K_d, K_RIGHT):
                    if b and not pos_x:
                        pos_x = 1
                        pos_y = 0
                        b = False

        # 填充背景色
        screen.fill(bgcolor)
        # 画网格线 竖线
        for x in range(SIZE, SCREEN_WIDTH, SIZE):
            pygame.draw.line(screen, black, (x, scope_y[0] * SIZE), (x, SCREEN_HEIGHT), line_width)
        # 画网格线 横线
        for y in range(scope_y[0] * SIZE, SCREEN_HEIGHT, SIZE):
            pygame.draw.line(screen, black, (0, y), (SCREEN_WIDTH, y), line_width)
        if game_over:
            if start:
                print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, 'GAME OVER',
                           red)
        else:
            curTime = time.time()
            if curTime - last_move_time > speed:
                if not pause:
                    b = True
                    last_move_time = curTime
                    next_s = (snake[0][0] + pos_x, snake[0][1] + pos_y)
                    if next_s[0] == food_x and next_s[1] == food_y:
                        # 吃到了食物
                        _create_food()
                        snake.appendleft(next_s)
                        score += 10
                        speed = orispeed - 0.03 * (score // 100)
                    else:
                        if scope_x[0] <= next_s[0] <= scope_x[1] and scope_y[0] <= next_s[1] <= scope_y[1] \
                                and next_s not in snake:
                            snake.appendleft(next_s)
                            snake.pop()
                        else:
                            game_over = True

        # 画食物
        if not game_over:
            # 避免 GAME OVER 的时候把 GAME OVER 的字给遮住了
            pygame.draw.rect(screen, light, (food_x * SIZE, food_y * SIZE, SIZE, SIZE), 0)

        # 画蛇
        for s in snake:
            pygame.draw.rect(screen, dark, (s[0] * SIZE + line_width, s[1] * SIZE + line_width,
                                            SIZE - line_width * 2, SIZE - line_width * 2), 0)
        print_text(screen, font1, 30, 7, f'速度: {
      
      score // 100}')
        print_text(screen, font1, 450, 7, f'得分: {
      
      score}')
        pygame.display.update()
if __name__ == '__main__':
    main()

at last

In order to thank the readers, I would like to share with you some of my recent favorite programming dry goods, to give back to every reader, and hope to help you.
There is a full set of information suitable for beginners~
Come and grow up with Xiaoyu!
① More than 100+ Python e-books (mainstream and classic books should be available)
② Python standard library information (the most complete Chinese version)
③ Source code of reptile projects (forty or fifty interesting and classic hand-practice projects and source code)
④ Python Basic introduction, reptiles, web development, and big data analysis videos (suitable for beginners)
⑤ Python learning roadmap (say goodbye to unrefined learning)

material

Guess you like

Origin blog.csdn.net/Modeler_xiaoyu/article/details/128202301