# ===== CodeLab: Python 小游戏⑤:贪吃蛇(pygame) =====
# 来源: https://aoerliang.dpdns.org/articles/game-python-snake
# 以下代码片段按文章出现顺序拼接, 共 6 段

# ----- 片段 1 (bash) -----
pip install pygame
python3 -c "import pygame; print(pygame.version.ver)"

# ----- 片段 2 (python) -----
import pygame

W, H = 480, 480
CELL = 20
pygame.init()
screen = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((20, 20, 20))
    pygame.display.flip()
    clock.tick(10)        # 每秒 10 帧,先慢一点

pygame.quit()

# ----- 片段 3 (python) -----
import random

GRID = W // CELL
snake = [(10, 10), (9, 10), (8, 10)]   # 头在 (10,10),向右排
direction = (1, 0)
food = (random.randint(0, GRID-1), random.randint(0, GRID-1))

# ----- 片段 4 (python) -----
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP and direction != (0, 1):
            direction = (0, -1)
        elif event.key == pygame.K_DOWN and direction != (0, -1):
            direction = (0, 1)
        elif event.key == pygame.K_LEFT and direction != (1, 0):
            direction = (-1, 0)
        elif event.key == pygame.K_RIGHT and direction != (-1, 0):
            direction = (1, 0)

# ----- 片段 5 (python) -----
head = (snake[0][0] + direction[0], snake[0][1] + direction[1])
if head[0] < 0 or head[0] >= GRID or head[1] < 0 or head[1] >= GRID:
    print("撞墙了,游戏结束!")
    break
snake.insert(0, head)
if head != food:
    snake.pop()

# ----- 片段 6 (python) -----
import pygame, random

W = H = 480
CELL = 20
GRID = W // CELL

pygame.init()
screen = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()

snake = [(10, 10), (9, 10), (8, 10)]
direction = (1, 0)
food = (random.randint(0, GRID-1), random.randint(0, GRID-1))
score = 0
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direction != (0, 1):
                direction = (0, -1)
            elif event.key == pygame.K_DOWN and direction != (0, -1):
                direction = (0, 1)
            elif event.key == pygame.K_LEFT and direction != (1, 0):
                direction = (-1, 0)
            elif event.key == pygame.K_RIGHT and direction != (-1, 0):
                direction = (1, 0)

    head = (snake[0][0] + direction[0], snake[0][1] + direction[1])
    if head[0] < 0 or head[0] >= GRID or head[1] < 0 or head[1] >= GRID:
        break
    snake.insert(0, head)
    if head == food:
        score += 1
        food = (random.randint(0, GRID-1), random.randint(0, GRID-1))
    else:
        snake.pop()
    if head in snake[1:]:
        break

    screen.fill((20, 20, 20))
    for x, y in snake:
        pygame.draw.rect(screen, (0, 200, 80), (x*CELL, y*CELL, CELL-2, CELL-2))
    pygame.draw.rect(screen, (220, 40, 40), (food[0]*CELL, food[1]*CELL, CELL-2, CELL-2))
    pygame.display.set_caption(f"贪吃蛇 · 分数: {score}")
    pygame.display.flip()
    clock.tick(10 + score // 2)

pygame.quit()
print(f"游戏结束,得分: {score}")
