User: python 贪吃蛇代码
Assistant: 下面是一个用 Python 和 Pygame 编写的简单贪吃蛇游戏示例代码。你可以运行它来体验基本的贪吃蛇游戏玩法。

```python
import pygame
import random

# 初始化 pygame
pygame.init()

# 游戏屏幕大小
WIDTH, HEIGHT = 640, 480
CELL_SIZE = 20

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

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")

clock = pygame.time.Clock()

font = pygame.font.SysFont("Arial", 24)

def draw_text(surface, text, color, x, y):
    textobj = font.render(text, True, color)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

def main():
    # 贪吃蛇初始位置和长度
    snake_pos = [100, 50]
    snake_body = [[100, 50], [80, 50], [60, 50]]
    direction = 'RIGHT'
    change_to = direction

    # 食物位置
    food_pos = [random.randrange(1, (WIDTH // CELL_SIZE)) * CELL_SIZE,
                random.randrange(1, (HEIGHT // CELL_SIZE)) * CELL_SIZE]
    food_spawn = True

    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 != 'DOWN':
                    change_to = 'UP'
                elif event.key == pygame.K_DOWN and direction != 'UP':
                    change_to = 'DOWN'
                elif event.key == pygame.K_LEFT and direction != 'RIGHT':
                    change_to = 'LEFT'
                elif event.key == pygame.K_RIGHT and direction != 'LEFT':
                    change_to = 'RIGHT'

        direction = change_to

        # 根据方向移动蛇头
        if direction == 'UP':
            snake_pos[1] -= CELL_SIZE
        elif direction == 'DOWN':
            snake_pos[1] += CELL_SIZE
        elif direction == 'LEFT':
            snake_pos[0] -= CELL_SIZE
        elif direction == 'RIGHT':
            snake_pos[0] += CELL_SIZE

        # 蛇体增长机制
        snake_body.insert(0, list(snake_pos))

        # 吃到食物
        if snake_pos == food_pos:
            score += 1
            food_spawn = False
        else:
            snake_body.pop()

        # 重新生成食物
        if not food_spawn:
            food_pos = [random.randrange(1, (WIDTH // CELL_SIZE)) * CELL_SIZE,
                        random.randrange(1, (HEIGHT // CELL_SIZE)) * CELL_SIZE]
            food_spawn = True

        # 填充背景
        screen.fill(BLACK)

        # 画蛇
        for pos in snake_body:
            pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], CELL_SIZE, CELL_SIZE))

        # 画食物
        pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], CELL_SIZE, CELL_SIZE))

        # 边界碰撞检测
        if (snake_pos[0] < 0 or snake_pos[0] >= WIDTH or
            snake_pos[1] < 0 or snake_pos[1] >= HEIGHT):
            running = False

        # 自身撞击检测
        for block in snake_body[1:]:
            if snake_pos == block:
                running = False

        # 显示得分
        draw_text(screen, f"得分: {score}", WHITE, 10, 10)

        pygame.display.update()
        clock.tick(10)

    # 游戏结束
    screen.fill(BLACK)
    draw_text(screen, f"游戏结束! 你的得分: {score}", RED, WIDTH // 6, HEIGHT // 3)
    pygame.display.update()
    pygame.time.wait(3000)
    pygame.quit()

if __name__ == "__main__":
    main()
```

### 运行说明：
1. 你需要先安装 pygame 库：
   ```bash
   pip install pygame
   ```
2. 将上述代码保存为 `snake_game.py`，然后运行：
   ```bash
   python snake_game.py
   ```
3. 通过键盘上的上下左右箭头控制蛇的移动。
4. 贪吃蛇吃到红色方块（食物）会增长，碰到边界或自身时游戏结束。

如果你需要更高级的功能，欢迎随时告诉我！
User: 你是gpt几
Assistant: