Use python to make a simple snake game

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


Snake game is a classic puzzle game, simple and playable.

Recalling childhood classics, today we will learn how to make a snake game.

development tools

  • python version: 3.6.8
  • Editor: pycharm

Related modules

import copy
import random
import pygame

Module installation

pip  install  -i  https://pypi.doubanio.com/simple/  --trusted-host pypi.doubanio.com  pygame

Achieve effect

Insert picture description here

Complete code

import copy
import random
# 游戏模块
import pygame

# 蛇的模型
snake_list = [[10, 10]]

# 500x500 背景大小
# 食物的模型 随机生成
x = random.randint(10, 490)
y = random.randint(10, 490)
food_point = [x, y]

# 上下左右的方位 初始小蛇方向
move_up = False
move_down = False
move_left = False
move_right = True

# 画布
# 初始化游戏组件
pygame.init()
# 设置画布大小
screen = pygame.display.set_mode((500, 500))
# 设置名字
title = pygame.display.set_caption('贪吃蛇游戏')
# 设置游戏时钟
clock = pygame.time.Clock()
while True:
    # 电影 是一帧一帧 30fps
    clock.tick(20)
    # 游戏循环
    # 把背景填充为白色
    screen.fill([255, 255, 255])

    """贪吃蛇移动 获取键盘事件"""
    # 获取电脑的时间
    # 确定移动方位
    for event in pygame.event.get():
        # 获取键盘事件
        # print(event)
        if event.type == pygame.KEYDOWN:
            # 向下移动
            if event.key == pygame.K_DOWN:
                move_up = False
                move_down = True
                move_left = False
                move_right = False
            if event.key == pygame.K_UP:
                move_up = True
                move_down = False
                move_left = False
                move_right = False
            if event.key == pygame.K_LEFT:
                move_up = False
                move_down = False
                move_left = True
                move_right = False
            if event.key == pygame.K_RIGHT:
                move_up = False
                move_down = False
                move_left = False
                move_right = True

    # 身子的移动
    snake_len = len(snake_list) - 1
    while snake_len > 0:
        snake_list[snake_len] = copy.deepcopy(snake_list[snake_len - 1])
        snake_len -= 1
    # 蛇头的移动
    if move_up:
        snake_list[snake_len][1] -= 10
        if snake_list[snake_len][1] < 0:
            snake_list[snake_len][1] = 500

    if move_down:
        snake_list[snake_len][1] += 10
        if snake_list[snake_len][1] > 500:
            snake_list[snake_len][1] = 0

    if move_left:
        snake_list[snake_len][0] -= 10
        if snake_list[snake_len][0] < 0:
            snake_list[snake_len][0] = 500

    if move_right:
        snake_list[snake_len][0] += 10
        if snake_list[snake_len][0] > 500:
            snake_list[snake_len][0] = 0
    # 绘制食物圆点
    food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15)
    # 循环
    snake_rect = []
    for snake_pos in snake_list:
        snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], snake_pos, 5))
        # 如果食物与蛇发送了碰撞 碰撞检测方法
        if food_rect.collidepoint(snake_pos):
            snake_list.append(food_point)
            # 重新生成食物
            food_point = [random.randint(10, 490), random.randint(10, 490)]
            break
    # 贪吃蛇吃到了自己 应该结束游戏
    # 取到蛇头
    snake_head_rect = snake_rect[0]
    count = len(snake_rect)
    while count > 1:
        # 蛇头与身子的任何一个点都有可能发生碰撞
        if snake_head_rect.colliderect(snake_rect[count - 1]):
            print('贪吃蛇吃到了自己,结束游戏')
            pygame.quit()
        count -= 1
    # 把绘制的东西显示出来
    pygame.display.update()

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112786600