腾讯云AI代码助手编程挑战赛——贪吃蛇小游戏

作品介绍

贪吃蛇小游戏需要控制蛇的移动方向,使其吃掉地图上随机出现的食物,每吃掉一个食物,蛇的身体就会增长一格,是一款老少皆宜的小游戏,我们可以用腾讯ai助手生成全部代码,简单方便快捷。

技术架构

python语言的tk库来完成的GUI页面设计,通过代码来完成具体的业务逻辑。

实现过程

我们通过对腾讯ai助手进行提问,并不断改进,最后得到完整代码。

项目源码

python 复制代码
import pygame
import time
import random

# 初始化 Pygame
pygame.init()

# 设置窗口大小
width = 600
height = 400
dis = pygame.display.set_mode((width, height))

# 设置游戏标题
pygame.display.set_caption('美化版贪吃蛇游戏')

# 定义颜色
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# 加载背景图片
background = pygame.image.load('background.jpg')  # 确保你有背景图片

# 设置时钟以控制游戏速度
clock = pygame.time.Clock()

snake_block = 20  # 蛇的尺寸
snake_speed = 10  # 降低蛇的速度

# 设置字体样式
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)

def our_snake(snake_block, snake_list):
    """绘制蛇"""
    for x in snake_list:
        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])

def message(msg, color):
    """显示消息"""
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, [width / 6, height / 3])

def gameLoop():
    """游戏主循环"""
    game_over = False
    game_close = False

    # 初始位置
    x1 = width / 2
    y1 = height / 2

    # 变化量
    x1_change = 0
    y1_change = 0

    snake_List = []
    Length_of_snake = 1

    # 食物位置
    foodx = round(random.randrange(0, width - snake_block) / 20.0) * 20.0
    foody = round(random.randrange(0, height - snake_block) / 20.0) * 20.0

    while not game_over:

        while game_close == True:
            dis.blit(background, (0, 0))  # 显示背景图片
            message("游戏结束! 按 Q 退出或按 C 重新开始", red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0

        # 检查边界
        if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        dis.blit(background, (0, 0))  # 显示背景图片
        pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
        if len(snake_List) > Length_of_snake:
            del snake_List[0]

        # 检查是否吃到自己
        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True

        our_snake(snake_block, snake_List)

        pygame.display.update()

        # 检查是否吃到食物
        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, width - snake_block) / 20.0) * 20.0
            foody = round(random.randrange(0, height - snake_block) / 20.0) * 20.0
            Length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()

if __name__ == "__main__":
    gameLoop()

使用说明

上下左右键控制蛇蛇移动方向,使其吃掉地图上随机出现的食物,每吃掉一个食物,蛇的身体就会增长一格,蛇撞墙或咬身体则游戏结束,按 Q 退出或按 C 重新开始。

开发环境

系统:win11系统

工具:VS Code

插件:安装腾讯云AI代码助手插件

腾讯云AI代码助手在上述过程中全程助力

完整的助力于开发的整个生命周期,包括初始页面到数据展示以及操作,最后进行打包exe文件。

效果展示

腾讯云AI代码助手编程挑战赛------贪吃蛇小游戏_哔哩哔哩_bilibili

相关推荐
再见晴天*_*34 分钟前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
Hello_Embed2 小时前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中2 小时前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
酷飞飞3 小时前
Python网络与多任务编程:TCP/UDP实战指南
网络·python·tcp/ip
Magnetic_h4 小时前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
hdsoft_huge4 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
研梦非凡4 小时前
ICCV 2025|从粗到细:用于高效3D高斯溅射的可学习离散小波变换
人工智能·深度学习·学习·3d
数字化顾问4 小时前
Python:OpenCV 教程——从传统视觉到深度学习:YOLOv8 与 OpenCV DNN 模块协同实现工业缺陷检测
python
雨白5 小时前
Java 多线程指南:从基础用法到线程安全
android·java
Hungry_Shark5 小时前
IDEA版本控制管理之使用Gitee
java·gitee·intellij-idea