aiaiask.com©

开花即结果 提问即答案

请写出一个贪吃蛇小游戏的代码
原始问题:

请写出一个贪吃蛇小游戏的代码

以下是一个简单的Python贪吃蛇小游戏代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口大小
window_width = 500
window_height = 500
window = pygame.display.set_mode((window_width, window_height))

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

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

# 定义蛇的初始位置和大小
snake_block_size = 10
snake_speed = 15
x1 = window_width / 2
y1 = window_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1

# 定义食物的初始位置
foodx = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0

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

# 定义显示分数的函数
def Your_score(score):
    value = font_style.render("Your Score: " + str(score), True, white)
    window.blit(value, [0, 0])

# 定义游戏结束的函数
def message(msg, color):
    mesg = font_style.render(msg, True, color)
    window.blit(mesg, [window_width / 6, window_height / 3])

# 游戏循环
game_over = False
while not game_over:
    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_size
                y1_change = 0
            elif event.key == pygame.K_RIGHT:
                x1_change = snake_block_size
                y1_change = 0
            elif event.key == pygame.K_UP:
                y1_change = -snake_block_size
                x1_change = 0
            elif event.key == pygame.K_DOWN:
                y1_change = snake_block_size
                x1_change = 0

    # 判断蛇是否撞墙
    if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
        game_over = True

    # 更新蛇的位置
    x1 += x1_change
    y1 += y1_change

    # 绘制食物
    window.fill(black)
    pygame.draw.rect(window, red, [foodx, foody, snake_block_size, snake_block_size])

    # 绘制蛇
    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_over = True

    for x in snake_List:
        pygame.draw.rect(window, white, [x[0], x[1], snake_block_size, snake_block_size])

    # 更新分数
    Your_score(Length_of_snake - 1)

    # 判断蛇是否吃到食物
    if x1 == foodx and y1 == foody:
        foodx = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
        foody = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0
        Length_of_snake += 1

    # 更新屏幕
    pygame.display.update()

    # 控制游戏速度
    clock = pygame.time.Clock()
    clock.tick(snake_speed)

# 显示游戏结束信息
message("You lost", white)
pygame.display.update()

# 等待3秒后退出游戏
pygame.time.wait(3000)

# 退出pygame
pygame.quit()

# 退出程序
quit()

这个代码使用pygame库实现了一个简单的贪吃蛇小游戏。玩家通过控制蛇的方向,让蛇吃到食物,蛇的身体会变长,直到撞墙或者撞到自己的身体,游戏结束。在游戏结束后,会显示玩家的得分,并等待3秒后退出游戏。

请写出一个贪吃蛇小游戏的代码
https://www.aiaiask.com/ask/37193.html
AI / 模型DeepSeek
时间 / DATEJune 17, 2023, 1:47 PM
语言zh-CN
IP / 区域新疆维吾尔自治区巴音郭楞州