json代码{ permissions: { allow: [ Bash(python -m py_compile snake.py), Bash(python -c \import pygame; print\\(pygame, pygame.version.ver\\)\), Bash(python -m pip install pygame), Bash(python -m pip --version), Bash(python -c \import struct; print\\(64bit if struct.calcsize\\(P\\)8 else 32bit\\)\), Bash(python -m pip install --only-binary :all: pygame -i https://pypi.tuna.tsinghua.edu.cn/simple), Bash(python -m pip install --only-binary :all: pygame-ce -i https://pypi.tuna.tsinghua.edu.cn/simple), Bash(python -), Bash(python snake.py) ] } }python代码import random import sys from collections import deque import pygame # ---------- 配置 ---------- CELL 20 # 每格像素大小 COLS, ROWS 30, 20 # 网格列数、行数 WIDTH, HEIGHT COLS * CELL, ROWS * CELL FPS 60 BASE_INTERVAL 150 # 初始每步毫秒数越小越快 MIN_INTERVAL 60 # 最快速度上限 SPEED_UP 4 # 每吃一个食物每步加快的毫秒数 # 颜色 BG (18, 18, 24) GRID (30, 30, 40) SNAKE_HEAD (0, 200, 100) SNAKE_BODY (0, 160, 80) FOOD (235, 60, 60) TEXT (220, 220, 220) # 方向向量 UP, DOWN, LEFT, RIGHT (0, -1), (0, 1), (-1, 0), (1, 0) OPPOSITE {UP: DOWN, DOWN: UP, LEFT: RIGHT, RIGHT: LEFT} def find_cjk_font(): 优先找系统中文字体找不到返回 None此时界面改用英文。 for name in (microsoftyahei, simhei, dengxian, simsun): path pygame.font.match_font(name) if path: return path return None class SnakeGame: def __init__(self): pygame.init() self.screen pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption(贪吃蛇穿墙版) self.clock pygame.time.Clock() font_path find_cjk_font() self.cn font_path is not None self.font_big pygame.font.Font(font_path, 36) self.font_small pygame.font.Font(font_path, 20) self.reset() def text(self, zh, en): 根据是否找到中文字体返回对应语言的文案。 return zh if self.cn else en def reset(self): cx, cy COLS // 2, ROWS // 2 self.snake deque([(cx, cy), (cx - 1, cy), (cx - 2, cy)]) self.direction RIGHT self.pending deque() # 转向输入缓冲保证一次转向一格 self.score 0 self.interval BASE_INTERVAL self.game_over False self.paused False self.food self.spawn_food() def spawn_food(self): 在蛇身以外的空位随机生成食物。 empty [(x, y) for x in range(COLS) for y in range(ROWS) if (x, y) not in self.snake] if not empty: self.game_over True # 填满整个棋盘通关 return (-1, -1) return random.choice(empty) def handle_event(self, event): if event.type pygame.QUIT: pygame.quit() sys.exit() if event.type ! pygame.KEYDOWN: return if event.key pygame.K_ESCAPE: pygame.quit() sys.exit() if self.game_over: if event.key in (pygame.K_r, pygame.K_SPACE): self.reset() return if event.key pygame.K_p: self.paused not self.paused return key_map { pygame.K_UP: UP, pygame.K_w: UP, pygame.K_DOWN: DOWN, pygame.K_s: DOWN, pygame.K_LEFT: LEFT, pygame.K_a: LEFT, pygame.K_RIGHT: RIGHT, pygame.K_d: RIGHT, } if event.key in key_map: new_dir key_map[event.key] last self.pending[-1] if self.pending else self.direction # 禁止 180° 掉头和重复入队 if new_dir ! last and new_dir ! OPPOSITE[last] and len(self.pending) 3: self.pending.append(new_dir) def step(self): 前进一格。 if self.pending: self.direction self.pending.popleft() hx, hy self.snake[0] dx, dy self.direction # 穿墙核心坐标取模从左出则从右进从上出则从下进 head ((hx dx) % COLS, (hy dy) % ROWS) # 撞到自己尾巴本回合会移开追上尾巴不算撞 if head in self.snake and head ! self.snake[-1]: self.game_over True return self.snake.appendleft(head) if head self.food: self.score 1 self.interval max(MIN_INTERVAL, self.interval - SPEED_UP) self.food self.spawn_food() else: self.snake.pop() def draw(self): self.screen.fill(BG) # 网格线 for x in range(0, WIDTH, CELL): pygame.draw.line(self.screen, GRID, (x, 0), (x, HEIGHT)) for y in range(0, HEIGHT, CELL): pygame.draw.line(self.screen, GRID, (0, y), (WIDTH, y)) # 食物 fx, fy self.food pygame.draw.rect(self.screen, FOOD, (fx * CELL 3, fy * CELL 3, CELL - 6, CELL - 6), border_radius6) # 蛇蛇头颜色略亮 for i, (x, y) in enumerate(self.snake): color SNAKE_HEAD if i 0 else SNAKE_BODY pygame.draw.rect(self.screen, color, (x * CELL 2, y * CELL 2, CELL - 4, CELL - 4), border_radius5) # 分数 score_txt self.text(f得分{self.score}, fScore: {self.score}) self.screen.blit(self.font_small.render(score_txt, True, TEXT), (10, 6)) # 操作提示 hint self.text(方向键/WASD 移动 · P 暂停 · ESC 退出, Arrows/WASD move · P pause · ESC quit) hint_surf self.font_small.render(hint, True, (120, 120, 130)) self.screen.blit(hint_surf, (10, HEIGHT - hint_surf.get_height() - 6)) # 暂停 / 结束遮罩 if self.paused: self.show_overlay(self.text(已暂停, Paused), self.text(按 P 继续, Press P to resume)) elif self.game_over: self.show_overlay(self.text(游戏结束, Game Over), self.text(按 R 或空格重新开始, Press R or Space to restart)) pygame.display.flip() def show_overlay(self, title, hint): overlay pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) overlay.fill((0, 0, 0, 140)) self.screen.blit(overlay, (0, 0)) t self.font_big.render(title, True, TEXT) self.screen.blit(t, t.get_rect(center(WIDTH // 2, HEIGHT // 2 - 20))) h self.font_small.render(hint, True, TEXT) self.screen.blit(h, h.get_rect(center(WIDTH // 2, HEIGHT // 2 20))) def run(self): acc 0 while True: for event in pygame.event.get(): self.handle_event(event) dt self.clock.tick(FPS) if not self.game_over and not self.paused: acc dt if acc self.interval: acc 0 self.step() self.draw() if __name__ __main__: SnakeGame().run()成品资源链接https://download.csdn.net/download/BigPanda12/93334367