贪吃蛇游戏项目二次开发

1.程序来源

	https://www.cnblogs.com/lyy135146/p/11523670.html

2.运行环境

	PyCharm 2023.2.5 (Professional Edition)
	python 3.12.0

原代码

点击查看代码
# python 运行时的环境
import pygame, sys, random

# 这个模块包含所有pygame所使用的常量
from pygame.locals import *

# 定义颜色变量

# 目标方块儿红颜色
redColor = pygame.Color(255, 0, 0)
# 背景颜色为黑色
blackColor = pygame.Color(0, 0, 0)
# 蛇颜色为白色
whiteColor = pygame.Color(255, 255, 255)


# 定义游戏结束
def gameOver():
    pygame.quit()
    sys.exit()


# 定义main函数 定义入口函数
def main():
    # 初始化pygame
    pygame.init()
    # 定义一个变量 控制速度
    fpsClock = pygame.time.Clock()
    # 创建一个窗口界面
    playSurface = pygame.display.set_mode((640, 480))
    pygame.display.set_caption('贪吃蛇')

    # 初始化目标方块的位置
    targetPosition = [300, 300]
    # 目标方块标记 判断贪吃蛇是否吃掉目标方块 1为没吃掉 0为吃掉
    targetFlag = 1


    # 初始化贪吃蛇的位置 (100,100)为基准
    # 初始化贪吃蛇长度 列表中有几个元素 就有几个身体
    snake_head = [100, 100]
    snake_body = [[80, 100], [60, 100]]


    # 初始化方向 默认为右
    direction = 'right'
    # 定义一个认为控制的方向的变量
    changedirection = direction

    # pygame 所有事件全部放到一个实时循环中
    while True:
        # 从队列中获取事件
        for event in pygame.event.get():

            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    changedirection = 'right'
                if event.key == K_LEFT:
                    changedirection = 'left'
                if event.key == K_UP:
                    changedirection = 'up'
                if event.key == K_DOWN:
                    changedirection = 'down'
                if event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))
        # 确定方向
        if changedirection == 'left' and not direction == 'right':
            direction = changedirection
        if changedirection == 'right' and not direction == 'left':
            direction = changedirection
        if changedirection == 'up' and not direction == 'down':
            direction = changedirection
        if changedirection == 'down' and not direction == 'up':
            direction = changedirection

        # 根据方向移动蛇头
        if direction == 'right':
            snake_head[0] += 20
        if direction == 'left':
            snake_head[0] -= 20
        if direction == 'up':
            snake_head[1] -= 20
        if direction == 'down':
            snake_head[1] += 20

        # 增加蛇的长度
        snake_body.insert(0, list(snake_head))

        # 如果贪吃蛇位置和目标方块位置重合
        if snake_head[0] == targetPosition[0] and snake_head[1] == targetPosition[1]:
            targetFlag = 0

        else:
            snake_body.pop()

        if targetFlag == 0:
            x = random.randrange(1, 32)
            y = random.randrange(1, 24)
            targetPosition = [int(x * 20), int(y * 20)]
            targetFlag = 1

        # 填充背景颜色
        playSurface.fill(blackColor)

        for position in snake_body:
            # rect函数内
            # 第一个参数surface  指定一个surface编辑区
            # 第二个参数color    指定颜色
            # 第三个参数rect     返回一个矩形包含位置信息(x,y),(width,height)
            # 第四个参数width    表示线条的粗细 width=0 实心  width=1 空心
            # 画蛇
            pygame.draw.rect(playSurface, whiteColor, Rect(position[0], position[1], 20, 20))
            # 画目标方块儿
            pygame.draw.rect(playSurface, redColor, Rect(targetPosition[0], targetPosition[1], 20, 20))

        # 更新显示到屏幕
        pygame.display.flip()

        # 判断游戏结束
        if snake_head[0] > 620 or snake_head[0] < 0:
            gameOver()
        if snake_head[1] > 460 or snake_head[1] < 0:
            gameOver()
        # 控制游戏速度
        fpsClock.tick(3)


# 启动入口 main函数
if __name__ == '__main__':
    main()
### 运行截图

image


3.主要问题

当前的事件处理部分可能有些重复,可以封装成一个函数,或者用字典映射按键和方向,减少IF判断的数量。目前判断蛇头是否碰到自身需要遍历整个蛇身体,可能效率不高,但考虑到蛇的长度不大,可能影响不大。不过代码中目前并没有检测自身碰撞的逻辑,这是一个功能上的缺失,需要添加。:食物生成的位置可能在蛇的身体上,导致食物出现在不可到达的位置。需要确保食物生成的位置不在蛇的当前位置。目前游戏结束时直接调用GAMEOVER,没有显示游戏结束的信息,比如分数或“GAME OVER”文字。同时加快了一下帧率。


4.新代码

点击查看代码
import pygame
import sys
import random
from pygame.locals import *

# ================== 常量定义 ==================
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
CELL_SIZE = 20
FPS = 5

# 颜色定义
COLORS = {
    'background': (0, 0, 0),
    'snake': (255, 255, 255),
    'food': (255, 0, 0),
    'text': (255, 255, 0)
}

# 方向映射
DIRECTIONS = {
    K_RIGHT: 'right',
    K_LEFT: 'left',
    K_UP: 'up',
    K_DOWN: 'down'
}

OPPOSITE_DIRECTION = {
    'right': 'left',
    'left': 'right',
    'up': 'down',
    'down': 'up'
}


# ================== 游戏对象类 ==================
class Snake:
    def __init__(self):
        self.head = [WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2]
        self.body = [list(self.head)]
        self.direction = 'right'
        self.grow = False

    def change_direction(self, new_dir):
        if new_dir != OPPOSITE_DIRECTION[self.direction]:
            self.direction = new_dir

    def move(self):
        # 根据方向移动头部
        if self.direction == 'right':
            self.head[0] += CELL_SIZE
        elif self.direction == 'left':
            self.head[0] -= CELL_SIZE
        elif self.direction == 'up':
            self.head[1] -= CELL_SIZE
        elif self.direction == 'down':
            self.head[1] += CELL_SIZE

        # 插入新头部位置
        self.body.insert(0, list(self.head))

        # 如果没有吃到食物则移除尾部
        if not self.grow:
            self.body.pop()
        self.grow = False

    def check_collision(self):
        # 边界碰撞检测
        if (self.head[0] < 0 or self.head[0] >= WINDOW_WIDTH or
                self.head[1] < 0 or self.head[1] >= WINDOW_HEIGHT):
            return True
        # 自身碰撞检测
        if self.head in self.body[1:]:
            return True
        return False


class Food:
    def __init__(self, snake_body):
        self.position = self.generate_new_position(snake_body)

    def generate_new_position(self, snake_body):
        while True:
            x = random.randint(0, (WINDOW_WIDTH // CELL_SIZE) - 1) * CELL_SIZE
            y = random.randint(0, (WINDOW_HEIGHT // CELL_SIZE) - 1) * CELL_SIZE
            new_pos = [x, y]
            if new_pos not in snake_body:
                return new_pos


# ================== 游戏函数 ==================
def show_game_over(surface, score):
    font = pygame.font.Font(None, 72)
    text = font.render('Game Over!', True, COLORS['text'])
    text_rect = text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 - 30))

    font_small = pygame.font.Font(None, 36)
    score_text = font_small.render(f'Score: {score}', True, COLORS['text'])
    score_rect = score_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 20))

    surface.blit(text, text_rect)
    surface.blit(score_text, score_rect)
    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()


def main():
    # 初始化游戏
    pygame.init()
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('贪吃蛇优化版')

    snake = Snake()
    food = Food(snake.body)
    score = 0

    # 主循环
    while True:
        # 事件处理
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key in DIRECTIONS:
                    snake.change_direction(DIRECTIONS[event.key])
                elif event.key == K_ESCAPE:
                    pygame.event.post(pygame.event.Event(QUIT))

        # 移动蛇
        snake.move()

        # 碰撞检测
        if snake.check_collision():
            show_game_over(screen, score)

        # 吃到食物检测
        if snake.head == food.position:
            snake.grow = True
            food = Food(snake.body)
            score += 1
            # 每吃5个食物加速
            if score % 5 == 0:
                global FPS
                FPS += 1

        # 绘制游戏
        screen.fill(COLORS['background'])

        # 绘制蛇
        for segment in snake.body:
            pygame.draw.rect(screen, COLORS['snake'],
                             (segment[0], segment[1], CELL_SIZE, CELL_SIZE))

        # 绘制食物
        pygame.draw.rect(screen, COLORS['food'],
                         (food.position[0], food.position[1], CELL_SIZE, CELL_SIZE))

        # 显示分数
        font = pygame.font.Font(None, 36)
        text = font.render(f'Score: {score}', True, COLORS['text'])
        screen.blit(text, (10, 10))

        pygame.display.update()
        clock.tick(FPS)


if __name__ == '__main__':
    main()

5.重构后运行截图

image


6.总结

在这次二次开发中主要方向都在以下几点:对象化重构过程,碰撞系统调试,食物生成优化,输入响应优化。由于不是自己写的程序,并且队pygame并不熟悉,对于程序的理解可能还并不足够,优化后程序在输入方面仍然存在一些问题,但是相较于原版已经有所改善。第一次尝试二次开发,对于代码中注释的重要性有了更深刻的认识,注释的存在让协同的开发者可以更好的理解别人写的程序,便于后续的优化和开发。

posted @   Petrichor-  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示