以下是一个使用pygame编写的简单的贪吃蛇程序:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口尺寸
width, height = 640, 480
screen = pygame.display.set_mode((width, height))

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

# 设置游戏时钟
clock = pygame.time.Clock()

# 定义贪吃蛇初始位置和大小
snake_pos = [[100, 50], [90, 50], [80, 50]]
snake_size = 10

# 定义食物初始位置和大小
food_pos = [random.randrange(1, width // 10) * 10, random.randrange(1, height // 10) * 10]
food_size = 10

# 定义初始移动方向
direction = 'RIGHT'
change_to = direction

# 定义分数
score = 0

# 游戏结束标志
game_over = False

# 定义贪吃蛇移动函数
def move_snake():
    global direction
    if change_to == 'UP' and direction != 'DOWN':
        direction = 'UP'
    if change_to == 'DOWN' and direction != 'UP':
        direction = 'DOWN'
    if change_to == 'LEFT' and direction != 'RIGHT':
        direction = 'LEFT'
    if change_to == 'RIGHT' and direction != 'LEFT':
        direction = 'RIGHT'
    if direction == 'UP':
        snake_pos[0][1] -= snake_size
    if direction == 'DOWN':
        snake_pos[0][1] += snake_size
    if direction == 'LEFT':
        snake_pos[0][0] -= snake_size
    if direction == 'RIGHT':
        snake_pos[0][0] += snake_size

# 定义游戏结束函数
def game_over_screen():
    font_style = pygame.font.SysFont(None, 50)
    game_over_text = font_style.render('Game Over', True, red)
    screen.blit(game_over_text, [width // 2 - game_over_text.get_width() // 2, height // 2 - game_over_text.get_height() // 2])
    pygame.display.update()
    pygame.time.wait(2000)

# 游戏主循环
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_UP:
                change_to = 'UP'
            if event.key == pygame.K_DOWN:
                change_to = 'DOWN'
            if event.key == pygame.K_LEFT:
                change_to = 'LEFT'
            if event.key == pygame.K_RIGHT:
                change_to = 'RIGHT'

    if direction == 'UP':
        snake_pos[0][1] -= snake_size
    if direction == 'DOWN':
        snake_pos[0][1] += snake_size
    if direction == 'LEFT':
        snake_pos[0][0] -= snake_size
    if direction == 'RIGHT':
        snake_pos[0][0] += snake_size

    # 判断贪吃蛇是否吃到食物
    if snake_pos[0] == food_pos:
        score += 1
        food_pos = [random.randrange(1, width // 10) * 10, random.randrange(1, height // 10) * 10]
        snake_pos.append([0, 0])

    # 判断贪吃蛇是否撞到自己或撞到边界
    if snake_pos[0][0] < 0 or snake_pos[0][0] >= width or snake_pos[0][1] < 0 or snake_pos[0][1] >= height:
        game_over = True
    for block in snake_pos[1:]:
        if snake_pos[0] == block:
            game_over = True

    if game_over:
        game_over_screen()
        break

    screen.fill(black)

    # 绘制贪吃蛇身体
    for pos in snake_pos:
        pygame.draw.rect(screen, white, pygame.Rect(pos[0], pos[1], snake_size, snake_size))

    # 绘制食物
    pygame.draw.rect(screen, red, pygame.Rect(food_pos[0], food_pos[1], food_size, food_size))

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

    # 控制游戏帧率
    clock.tick(15)

# 退出pygame
pygame.quit()

这是一个简单的贪吃蛇程序,使用pygame库进行图形化显示。贪吃蛇的移动和游戏规则已经实现,当贪吃蛇撞到自己或撞到边界时,游戏结束并显示'Game Over'。


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

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