import pgzrun import random import time WIDTH = 1000 # 窗口的宽度 HEIGHT = 725 # 窗口的高度 def draw(): screen.blit('火眼金睛.png', (0, 0)) if win: screen.blit('游戏成功.png', (0, 0)) screen.draw.text(f"{total_t:.1f}", center=(520, 570), fontsize=90, color="yellow") else: screen.draw.text(f"{t:.1f} s", center=(900, 662), fontsize=40, color="yellow") for grid in grids: screen.draw.filled_rect(Rect(grid['pos'], (side, side)), grid['color']) def update(): """更新函数,用于实时更新游戏状态相关信息,这里主要更新总耗时""" global t t = time.time() - start_t def base_color(): """生成随机基础颜色""" return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) def dif_color(base): """ 参数: base:表示基础颜色的元组,格式为(r,g,b) """ r = min(base[0] + random.randint(20, 30), 255) g = min(base[1] + random.randint(20, 30), 255) b = min(base[2] + random.randint(20, 30), 255) return (r, g, b) def game(): global grids, side, dc # 根据格子数量计算格子边长和间距 side = 700 // n * 0.9 space = 700 // n * 0.1 # 创建列表grids,后面存储每个方格信息 grids = [] # 获取基础色和异色 bc = base_color() dc = dif_color(bc) # 记录方格编号 i = 1 # 随机选择一个位置的方格设置略微不同的颜色 dif_index = random.randint(1, n * n) for row in range(n): for col in range(n): info = { 'pos': (space + col * (side + space), space + row * (side + space)), 'color': dc if i == dif_index else bc } grids.append(info) i += 1 def on_mouse_down(pos): global n, win, total_t for grid in grids: grid_rect = Rect(grid['pos'], (side, side)) if grid_rect.collidepoint(pos) and grid['color'] == dc: if n > 10: win = True total_t = t else: n += 1 # 进入下一关 game() # 初始化游戏参数 n = 3 # 初始为3×3的矩阵 win = False # 记录游戏成功状态 start_t = time.time() # 记录开始时间 game() # 启动游戏 pgzrun.go()