|
给学生的贪吃蛇PYTHON游戏制作导学案 贪吃蛇游戏 (Python3 +Pygame) 以下是一个完整的单文件贪吃蛇游戏实现,不需要任何外部素材文件: 请在PYCHARM内完成后 import pygame import random import sys
# 初始化pygame pygame.init()
# 游戏常量 WIDTH, HEIGHT =800, 600 GRID_SIZE = 20 GRID_WIDTH =WIDTH // GRID_SIZE GRID_HEIGHT =HEIGHT // GRID_SIZE
# 颜色定义 BLACK = (0, 0,0) WHITE = (255,255, 255) RED = (255, 0,0) GREEN = (0, 255,0) DARK_GREEN = (0,155, 0) GRAY = (128,128, 128)
# 创建游戏窗口 screen =pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("贪吃蛇") clock = pygame.time.Clock()
# 字体 font =pygame.font.SysFont('simhei', 36) small_font =pygame.font.SysFont('simhei', 24)
class Snake: def __init__(self): self.reset() def reset(self): self.body = [(GRID_WIDTH // 2,GRID_HEIGHT // 2)] self.direction = (1, 0) # 初始向右 self.length = 1 self.score = 0 self.speed = 10 # 初始速度 def move(self): head_x, head_y = self.body[0] dx, dy = self.direction new_head = (head_x + dx, head_y + dy) # 检查碰撞 if (new_head in self.body[1:] or new_head[0] < 0 or new_head[0]>= GRID_WIDTH or new_head[1] < 0 or new_head[1]>= GRID_HEIGHT): return False self.body.insert(0, new_head) if len(self.body) > self.length: self.body.pop() return True def grow(self): self.length += 1 self.score += 10 # 每增长5节增加速度 if self.length % 5 == 0 and self.speed< 20: self.speed += 1 def change_direction(self, new_direction): # 防止180度转弯 opposite_directions = { (1, 0): (-1, 0), (-1, 0): (1, 0), (0, 1): (0, -1), (0, -1): (0, 1) } if new_direction !=opposite_directions.get(self.direction, (0, 0)): self.direction = new_direction
class Food: def __init__(self, snake): self.position =self.generate_position(snake) def generate_position(self, snake): while True: position = ( random.randint(0, GRID_WIDTH -1), random.randint(0, GRID_HEIGHT -1) ) if position not in snake.body: return position def draw(self): x, y = self.position rect = pygame.Rect(x * GRID_SIZE, y *GRID_SIZE, GRID_SIZE, GRID_SIZE) # 绘制苹果形状的食物 pygame.draw.rect(screen, RED, rect) # 苹果柄 pygame.draw.rect(screen, DARK_GREEN, (x * GRID_SIZE + GRID_SIZE//2 -2, y * GRID_SIZE - 5, 4, 5))
def draw_grid(): for x in range(0, WIDTH, GRID_SIZE): pygame.draw.line(screen, GRAY, (x, 0),(x, HEIGHT)) for y in range(0, HEIGHT, GRID_SIZE): pygame.draw.line(screen, GRAY, (0, y),(WIDTH, y))
defshow_message(text, color, y_offset=0): text_surface = font.render(text, True,color) text_rect =text_surface.get_rect(center=(WIDTH//2, HEIGHT//2 + y_offset)) screen.blit(text_surface, text_rect)
defshow_small_message(text, color, position): text_surface = small_font.render(text,True, color) screen.blit(text_surface, position)
def main(): snake = Snake() food = Food(snake) game_over = False paused = False whileTrue: # 事件处理 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if not game_over: if event.type == pygame.KEYDOWN: if event.key ==pygame.K_UP: snake.change_direction((0, -1)) elif event.key ==pygame.K_DOWN: snake.change_direction((0, 1)) elif event.key ==pygame.K_LEFT: snake.change_direction((-1, 0)) elif event.key ==pygame.K_RIGHT: snake.change_direction((1, 0)) elif event.key ==pygame.K_SPACE: paused = not paused if event.type == pygame.KEYDOWN: if event.key == pygame.K_r andgame_over: snake.reset() food = Food(snake) game_over = False elif event.key ==pygame.K_SPACE and game_over: paused = not paused # 游戏逻辑 if not game_over and not paused: if not snake.move(): game_over = True # 检查是否吃到食物 if snake.body[0] == food.position: snake.grow() food = Food(snake) # 绘制 screen.fill(BLACK) draw_grid() # 绘制蛇 for i, segment inenumerate(snake.body): x, y = segment rect = pygame.Rect(x * GRID_SIZE, y* GRID_SIZE, GRID_SIZE, GRID_SIZE) # 蛇头用不同颜色 if i == 0: pygame.draw.rect(screen,DARK_GREEN, rect) # 蛇头眼睛 eye_size = GRID_SIZE // 4 eye_offset = GRID_SIZE // 4 if snake.direction == (1,0): # 右 pygame.draw.rect(screen,BLACK, (x * GRID_SIZE + eye_offset*2, y * GRID_SIZE+ eye_offset, eye_size,eye_size)) elif snake.direction == (-1,0): # 左 pygame.draw.rect(screen,BLACK, (x * GRID_SIZE + eye_offset, y *GRID_SIZE + eye_offset, eye_size,eye_size)) elif snake.direction == (0,-1): # 上 pygame.draw.rect(screen,BLACK, (x *GRID_SIZE + eye_offset, y * GRID_SIZE + eye_offset, eye_size,eye_size)) elif snake.direction == (0,1): # 下 pygame.draw.rect(screen,BLACK, (x *GRID_SIZE + eye_offset, y * GRID_SIZE + eye_offset*2, eye_size,eye_size)) else: pygame.draw.rect(screen, GREEN,rect) # 绘制食物 food.draw() # 显示分数 show_small_message(f"分数: {snake.score}", WHITE, (10, 10)) show_small_message(f"速度: {snake.speed}", WHITE, (10, 40)) # 游戏状态显示 if game_over: show_message("游戏结束!", RED) show_message(f"最终分数: {snake.score}", WHITE, 50) show_message("按R键重新开始", WHITE,100) show_message("按空格键暂停/继续", WHITE,150) elif paused: show_message("已暂停", WHITE) show_message("按空格键继续", WHITE, 50) pygame.display.flip() clock.tick(snake.speed)
if __name__ =="__main__": main() 游戏功能说明 1. 基本功能: o 使用方向键控制蛇的移动 o 随机生成食物(苹果形状) o 蛇吃到食物后长度增加,分数增加 o 碰撞检测(墙壁和自身) 2. 游戏机制: o 蛇不能180度转弯(不能直接反向移动) o 随着蛇长度增加,游戏速度会逐渐加快 o 网格背景辅助玩家判断位置 3. 界面元素: o 实时显示分数和当前速度 o 游戏结束画面 o 暂停功能(空格键) 4. 视觉效果: o 蛇头有眼睛,方向不同眼睛位置不同 o 食物设计成苹果形状 o 网格背景 操作说明 · 方向键:控制蛇的移动方向 · 空格键:暂停/继续游戏 · R键:游戏结束后重新开始 · 关闭窗口:退出游戏 这个实现完全自包含,不需要任何外部素材文件,所有图形都是通过代码绘制的。
|