import pygame
import random

# 初始化 Pygame
pygame.init()

# 全屏显示
window = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
window_width, window_height = pygame.display.get_surface().get_size()
pygame.display.set_caption('贪吃蛇游戏')

# 定义颜色
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
yellow = pygame.Color(255, 255, 0)
peach = pygame.Color(255, 153, 153)

# 定义蛇的初始位置和移动速度
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50], [70, 50], [60, 50]]
snake_speed = 10

# 定义食物的初始位置和效果
food_position = [random.randrange(1, (window_width // 10)) * 10, random.randrange(1, (window_height // 10)) * 10]
food_spawn = True
food_effect = 'apple'  # 物件效果(apple, banana, peach)

# 定义游戏时钟
clock = pygame.time.Clock()

# 定义游戏结束标志
game_over = False

# 定义蛇方向的初始状态
direction = 'RIGHT'
change_to = direction

# 定义初始生命和分数
life = 1
score = 0

# 定义字体
font = pygame.font.Font(None, 36)

# 游戏主循环
while not game_over:
    # 处理键盘事件
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP or event.key == ord('w'):
                change_to = 'UP'
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                change_to = 'DOWN'
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                change_to = 'LEFT'
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                change_to = 'RIGHT'
        if event.type == pygame.QUIT:
            game_over = True

    # 根据蛇的方向改变蛇的移动
    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_position[1] -= 10
    if direction == 'DOWN':
        snake_position[1] += 10
    if direction == 'LEFT':
        snake_position[0] -= 10
    if direction == 'RIGHT':
        snake_position[0] += 10

    # 增加蛇的身体长度
    snake_body.insert(0, list(snake_position))
    if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
        if food_effect == 'apple':
            score += 1
        elif food_effect == 'banana':
            snake_speed = max(2, snake_speed - 2)  # 限制最低速度
        elif food_effect == 'peach':
            life += 1
        food_spawn = False
    else:
        snake_body.pop()

    # 如果食物消失了,重新生成一个食物
    if not food_spawn:
        food_position = [random.randrange(1, (window_width // 10)) * 10, random.randrange(1, (window_height // 10)) * 10]
        food_effect = random.choice(['apple', 'banana', 'peach'])
        food_spawn = True

    # 绘制游戏窗口
    window.fill(black)
    for pos in snake_body:
        pygame.draw.rect(window, green, pygame.Rect(pos[0], pos[1], 10, 10))
    pygame.draw.rect(window, red, pygame.Rect(food_position[0], food_position[1], 10, 10))

    # 绘制生命和得分
    life_text = font.render('Life: ' + str(life), True, white)
    score_text = font.render('Score: ' + str(score), True, white)
    window.blit(life_text, (10, 10))
    window.blit(score_text, (window_width - score_text.get_width() - 10, 10))

    # 检查游戏结束的条件
    if snake_position[0] < 0 or snake_position[0] > window_width - 10:
        life -= 1
        snake_position = [100, 50]  # 重置蛇的位置
        direction = 'RIGHT' 
        change_to = direction
    if snake_position[1] < 0 or snake_position[1] > window_height - 10:
        life -= 1
        snake_position = [100, 50]  # 重置蛇的位置
        direction = 'RIGHT' 
        change_to = direction
    for block in snake_body[1:]:
        if snake_position[0] == block[0] and snake_position[1] == block[1]:
            life -= 1
            snake_position = [100, 50]  # 重置蛇的位置
            direction = 'RIGHT' 
            change_to = direction
    if life <= 0:
        game_over = True

    # 更新游戏窗口
    pygame.display.update()

    # 控制游戏速度
    clock.tick(snake_speed)

# 输出游戏得分
print(f'游戏结束!得分:{score}')

# 退出 Pygame
pygame.quit()

这款Python贪吃蛇游戏代码简单易懂,非常适合游戏开发初学者学习。它不仅实现了贪吃蛇的基本功能,还添加了全屏显示、道具和生命值等元素,提升了游戏的趣味性和挑战性。希望这段代码能够帮助你更好地理解Python游戏开发,并激发你创造更多有趣游戏的热情!

Python全屏贪吃蛇游戏:带你体验多道具与生命值的游戏乐趣

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

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