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

import pygame
import time
import random

# 初始化pygame
pygame.init()

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

# 设置游戏窗口的尺寸和标题
window_width = 800
window_height = 600
game_window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('贪吃蛇')

# 定义蛇的初始位置和大小
snake_block_size = 20
snake_speed = 15
font_style = pygame.font.SysFont(None, 50)
score_font = pygame.font.SysFont(None, 35)


def our_snake(snake_block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(game_window, black, [x[0], x[1], snake_block_size, snake_block_size])


def message(msg, color):
    mesg = font_style.render(msg, True, color)
    game_window.blit(mesg, [window_width / 6, window_height / 3])


clock = pygame.time.Clock()

def game_loop():
    game_over = False
    game_close = False

    # 蛇的初始位置和移动方向
    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) / 20.0) * 20.0
    foody = round(random.randrange(0, window_height - snake_block_size) / 20.0) * 20.0

    while not game_over:

        while game_close == True:
            game_window.fill(white)
            message('Game Over! Press Q-Quit or C-Play Again', black)
            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:
                        game_loop()

        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_close = True
        x1 += x1_change
        y1 += y1_change
        game_window.fill(white)
        pygame.draw.rect(game_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_close = True

        our_snake(snake_block_size, snake_list)
        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, window_width - snake_block_size) / 20.0) * 20.0
            foody = round(random.randrange(0, window_height - snake_block_size) / 20.0) * 20.0
            length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()


# 运行游戏循环
game_loop()

这个代码使用了 pygame 库来实现游戏窗口和游戏逻辑。游戏窗口的大小为 800x600 像素,蛇的大小为 20x20 像素,蛇的速度为 15。

游戏的主循环中,首先检测到游戏窗口关闭事件或按下键盘的 Q 键时,游戏结束。当游戏窗口关闭时,显示游戏结束消息,并根据按下的键来决定是退出游戏还是重新开始游戏。当按下键盘的方向键时,改变蛇的移动方向。

在每个游戏循环中,判断蛇是否撞到了墙壁或自己的身体,如果是,则游戏结束。如果蛇吃到了食物,增加蛇的长度并生成新的食物。

最后,调用 pygame.quit() 来退出游戏。你可以在命令行中运行这个脚本来开始游戏。


原文地址: https://www.cveoy.top/t/topic/pRlB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录