贪吃蛇

 

import pygame
import random

# 初始化
pygame.init()

# 游戏界面宽高和方块大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 1080
CELL_SIZE = 20

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# 创建游戏界面
pygame.display.set_caption("大东牌-贪吃蛇")
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

# 创建时钟对象,控制游戏帧率
clock = pygame.time.Clock()

# 蛇的初始位置
snake_head_x = WINDOW_WIDTH / 2 // CELL_SIZE * CELL_SIZE
snake_head_y = WINDOW_HEIGHT / 2 // CELL_SIZE * CELL_SIZE

# 食物的初始位置
food_x = random.randint(0, WINDOW_WIDTH // CELL_SIZE - 1) * CELL_SIZE
food_y = random.randint(0, WINDOW_HEIGHT // CELL_SIZE - 1) * CELL_SIZE

# 蛇的初始长度、方向和速度
snake_length = 3
snake_direction = "right"
snake_speed = CELL_SIZE

# 存储蛇身体的各个方块坐标的列表
snake_body = [(snake_head_x - i * CELL_SIZE, snake_head_y) for i in range(snake_length)]

# 游戏主循环
while True:
    # 检测事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and snake_direction != "down":
                snake_direction = "up"
            elif event.key == pygame.K_DOWN and snake_direction != "up":
                snake_direction = "down"
            elif event.key == pygame.K_LEFT and snake_direction != "right":
                snake_direction = "left"
            elif event.key == pygame.K_RIGHT and snake_direction != "left":
                snake_direction = "right"

    # 更新蛇头位置
    if snake_direction == "up":
        snake_head_y -= snake_speed
    elif snake_direction == "down":
        snake_head_y += snake_speed
    elif snake_direction == "left":
        snake_head_x -= snake_speed
    elif snake_direction == "right":
        snake_head_x += snake_speed

    # 判断是否吃到食物
    if snake_head_x == food_x and snake_head_y == food_y:
        # 食物坐标重置
        food_x = random.randint(0, WINDOW_WIDTH // CELL_SIZE - 1) * CELL_SIZE
        food_y = random.randint(0, WINDOW_HEIGHT // CELL_SIZE - 1) * CELL_SIZE
        # 蛇长度增加1
        snake_length += 1
        # 将新的蛇身体块加入列表
        snake_body.append((snake_body[-1][0], snake_body[-1][1]))

    # 判断蛇头是否撞到边界或撞到自己
    if (
        snake_head_x < 0
        or snake_head_x >= WINDOW_WIDTH
        or snake_head_y < 0
        or snake_head_y >= WINDOW_HEIGHT
    ):
        # 游戏结束
        pygame.quit()
        quit()
    if (snake_head_x, snake_head_y) in snake_body[:-1]:
        # 游戏结束
        pygame.quit()
        quit()

    # 更新蛇身体坐标
    for i in range(snake_length - 1, 0, -1):
        snake_body[i] = (snake_body[i - 1][0], snake_body[i - 1][1])
    snake_body[0] = (snake_head_x, snake_head_y)

    # 在屏幕上绘制蛇和食物
    screen.fill(BLACK)
    for x, y in snake_body:
        pygame.draw.rect(screen, GREEN, (x, y, CELL_SIZE, CELL_SIZE))
    pygame.draw.rect(screen, RED, (food_x, food_y, CELL_SIZE, CELL_SIZE))
    pygame.display.update()

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

 

posted @ 2023-04-11 17:29  Hello_worlds  阅读(49)  评论(0编辑  收藏  举报