腾讯云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

相关推荐
workflower14 分钟前
AI制造-推荐初始步骤
java·开发语言·人工智能·软件工程·制造·需求分析·软件需求
2501_9206276116 分钟前
Flutter 框架跨平台鸿蒙开发 - 古文学习应用
学习·flutter·harmonyos
GIS兵墩墩32 分钟前
postgis--PostgreSQL16及其plpython3u扩展
python·postgis
new Object ~32 分钟前
LangChain的短期记忆存储实现
python·langchain
魔都吴所谓35 分钟前
【Python】从零构建:IP地理位置查询实战指南
开发语言·python·tcp/ip
测试19981 小时前
使用Python自动化生成接口测试用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
智算菩萨2 小时前
【Pygame】第10章 游戏状态管理与场景切换机制
python·游戏·pygame
songcream12 小时前
TensorFlow的一些基本概念
人工智能·python·tensorflow
ACGkaka_2 小时前
SimpleDateFormat 线程安全问题及修复方案
java·jvm·安全