B站视频: https://www.bilibili.com/video/BV1Cx4y1P7iY/

西瓜视频: https://www.ixigua.com/7211710650026820111

油管视频: https://www.youtube.com/watch?v=7kYqWGxXS28

工具下载地址: https://www.cursor.so/

提示词: 请帮助我写一个能在python3.11下 正常运行的贪吃蛇小游戏

第一次写不一定能够成功 如果不能成功 运行 则全选代码进行改错

GPT4 输出的结果如下:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口大小
screen_width = 480
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置游戏标题
pygame.display.set_caption("贪吃蛇小游戏")

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)

# 定义蛇的初始位置和大小
snake_size = 10
snake_x = screen_width / 2
snake_y = screen_height / 2

# 定义蛇的移动速度
snake_speed = 10

# 定义蛇的移动方向
snake_direction = "right"

# 定义食物的初始位置和大小
food_size = 10
food_x = round(random.randrange(0, screen_width - food_size) / 10.0) * 10.0
food_y = round(random.randrange(0, screen_height - food_size) / 10.0) * 10.0

# 定义分数
score = 0

# 定义字体
font = pygame.font.SysFont(None, 25)

# 定义游戏结束函数
def game_over():
    # 显示游戏结束信息
    message("游戏结束", red)
    # 停止游戏
    pygame.quit()
    # 退出程序
    quit()

# 定义显示文字函数
def message(msg, color):
    # 渲染文字
    text = font.render(msg, True, color)
    # 显示文字
    screen.blit(text, [screen_width / 6, screen_height / 3])

# 游戏循环
while True:
    try:
        # 处理事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # 停止游戏
                pygame.quit()
                # 退出程序
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    snake_direction = "left"
                elif event.key == pygame.K_RIGHT:
                    snake_direction = "right"
                elif event.key == pygame.K_UP:
                    snake_direction = "up"
                elif event.key == pygame.K_DOWN:
                    snake_direction = "down"

        # 移动蛇的位置
        if snake_direction == "right":
            snake_x += snake_speed
        elif snake_direction == "left":
            snake_x -= snake_speed
        elif snake_direction == "up":
            snake_y -= snake_speed
        elif snake_direction == "down":
            snake_y += snake_speed

        # 判断蛇是否吃到食物
        if snake_x == food_x and snake_y == food_y:
            # 重新生成食物
            food_x = round(random.randrange(0, screen_width - food_size) / 10.0) * 10.0
            food_y = round(random.randrange(0, screen_height - food_size) / 10.0) * 10.0
            # 增加分数
            score += 10

        # 绘制游戏界面
        screen.fill(white)
        pygame.draw.rect(screen, black, [snake_x, snake_y, snake_size, snake_size])
        pygame.draw.rect(screen, green, [food_x, food_y, food_size, food_size])
        # 显示分数
        message("分数: " + str(score), black)
        # 更新屏幕
        pygame.display.update()

        # 判断蛇是否撞墙
        if snake_x >= screen_width or snake_x < 0 or snake_y >= screen_height or snake_y < 0:
            game_over()
    except Exception as e:
        print(e)
        game_over()

    # 控制游戏速度
    pygame.time.Clock().tick(30)