9月24日贪吃蛇

import pygame
import sys
import random

# 初始化
pygame.init()

# 游戏设置
WIDTH, HEIGHT = 400, 400
GRID_SIZE = 20
GRID_WIDTH = WIDTH // GRID_SIZE
GRID_HEIGHT = HEIGHT // GRID_SIZE
FPS = 10

# 颜色
WHITE = (255, 255, 255)
GRID_COLOR = (200, 200, 200)

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

# 初始化窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('贪吃蛇')

# 初始化蛇
snake = [(2, 2)]
snake_direction = (1, 0)
next_direction = (1, 0)  # 新的方向
snake_color = (0, 128, 0)  # 初始蛇颜色

# 初始化食物
food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))
food_image = pygame.Surface((GRID_SIZE, GRID_SIZE))
food_image.fill((255, 0, 0))  # 红色

# 初始化得分
score = 0

# 蛇头的图像
head_image = pygame.Surface((GRID_SIZE, GRID_SIZE))
head_image.fill(snake_color)

# 游戏循环
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and snake_direction != (0, 1):
                next_direction = (0, -1)
            elif event.key == pygame.K_DOWN and snake_direction != (0, -1):
                next_direction = (0, 1)
            elif event.key == pygame.K_LEFT and snake_direction != (1, 0):
                next_direction = (-1, 0)
            elif event.key == pygame.K_RIGHT and snake_direction != (-1, 0):
                next_direction = (1, 0)

    # 确保蛇不能在一帧内反向移动
    if (snake_direction[0] != -next_direction[0] or
            snake_direction[1] != -next_direction[1]):
        snake_direction = next_direction

    # 移动蛇
    x, y = snake[0]
    new_x = (x + snake_direction[0]) % GRID_WIDTH
    new_y = (y + snake_direction[1]) % GRID_HEIGHT
    new_head = (new_x, new_y)

    # 判断是否吃到食物
    if new_head == food:
        snake.insert(0, food)
        food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))
        score += 1

        # 随机生成新的蛇颜色
        snake_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        head_image.fill(snake_color)
    else:
        snake.insert(0, new_head)
        snake.pop()

    # 判断游戏是否结束
    if len(snake) > 1 and new_head in snake[1:]:
        pygame.quit()
        sys.exit()

    # 绘制背景
    screen.fill(WHITE)

    # 绘制方格背景
    for x in range(GRID_WIDTH):
        for y in range(GRID_HEIGHT):
            rect = pygame.Rect(x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE)
            pygame.draw.rect(screen, GRID_COLOR, rect, 1)

    # 绘制蛇
    for segment in snake:
        # 绘制蛇头
        if segment == snake[0]:
            screen.blit(head_image, (segment[0] * GRID_SIZE, segment[1] * GRID_SIZE))
        else:
            # 绘制蛇身体
            pygame.draw.rect(screen, snake_color, (segment[0] * GRID_SIZE, segment[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))

    # 绘制食物
    screen.blit(food_image, (food[0] * GRID_SIZE, food[1] * GRID_SIZE))

    # 显示得分
    score_text = FONT.render(f"得分: {score}", True, snake_color)
    screen.blit(score_text, (10, 10))

    pygame.display.flip()

    # 控制游戏速度
    clock.tick(FPS)
posted @   scxlzb  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示