好的!下面是一个基本的贪吃蛇游戏的Python代码示例:

import pygame
import time
import random

pygame.init()

# 游戏窗口尺寸
window_width = 800
window_height = 600

# 蛇身方块尺寸
block_size = 20

# 游戏速度,值越小速度越快
game_speed = 15

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

# 游戏窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('贪吃蛇')

clock = pygame.time.Clock()

font_style = pygame.font.SysFont(None, 50)

def snake(block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(window, black, [x[0], x[1], block_size, block_size])

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

def gameLoop():
    game_over = False
    game_close = False

    x1 = window_width / 2
    y1 = window_height / 2

    x1_change = 0
    y1_change = 0

    snake_list = []
    snake_length = 1

    foodx = round(random.randrange(0, window_width - block_size) / 20.0) * 20.0
    foody = round(random.randrange(0, window_height - block_size) / 20.0) * 20.0

    while not game_over:
        while game_close:
            window.fill(white)
            message('你输了!按Q-退出或C-重新开始', red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    game_over = True
                    game_close = False
                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 = -block_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = block_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -block_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = 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
        window.fill(white)
        pygame.draw.rect(window, red, [foodx, foody, block_size, block_size])
        snake_head = []
        snake_head.append(x1)
        snake_head.append(y1)
        snake_list.append(snake_head)
        if len(snake_list) > snake_length:
            del snake_list[0]

        for x in snake_list[:-1]:
            if x == snake_head:
                game_close = True

        snake(block_size, snake_list)

        pygame.display.update()

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

        clock.tick(game_speed)

    pygame.quit()


gameLoop()

这是一个简单的贪吃蛇游戏代码,你可以将其复制到一个Python脚本中,并使用Python解释器运行它。游戏窗口将会打开,并且你可以使用方向键来控制蛇移动,目标是吃到食物并尽可能长大。如果蛇碰到边界或自己的身体,则游戏结束。按Q键可以退出游戏,按C键可以重新开始游戏。希望你喜欢这个小游戏!如有需要,可以根据个人喜好进行自定义和扩展。

Python 贪吃蛇游戏代码示例 - 简单易懂

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

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