You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyGame制作Simon Says游戏时鼠标悬停颜色显示异常求助

Simon Says游戏PyGame悬停效果异常问题修复

问题现象

  • 程序启动时右上角蓝色方块默认呈暗色
  • 鼠标移至蓝色方块上时它反而变亮(与预期的悬停变暗逻辑相反)
  • 鼠标移到红色方块上时,蓝色方块会变暗,但点击检测显示选中的是红色方块

错误原因分析

  1. findColor()函数逻辑漏洞:第二个elif判断未限制鼠标X坐标范围,只要鼠标Y处于上方区域,无论X位置在哪都会返回"blue",导致非蓝色区域触发蓝色方块状态错误。
  2. 蓝色方块明暗逻辑写反:当前代码中,findColor()返回"blue"时绘制亮蓝色,否则绘制暗蓝色,与其他方块的悬停变暗逻辑完全相反。
  3. 窗口尺寸变量冲突:先定义width,height=690,690创建窗口,之后又重新赋值为700,导致坐标计算混乱,方块位置与鼠标检测范围不匹配。

修复后的完整代码

import pygame
import sys
pygame.init()

score = 0

def findColor():
    mouse = pygame.mouse.get_pos()
    box_size = (width // 2) - (margin * 2)
    # 红色方块:左上区域
    if margin <= mouse[0] <= margin + box_size and margin <= mouse[1] <= margin + box_size:
        return "red"
    # 蓝色方块:右上区域
    elif (width // 2) + margin <= mouse[0] <= (width // 2) + margin + box_size and margin <= mouse[1] <= margin + box_size:
        return "blue"
    # 绿色方块:左下区域
    elif margin <= mouse[0] <= margin + box_size and (height // 2) + margin <= mouse[1] <= (height // 2) + margin + box_size:
        return "green"
    # 黄色方块:右下区域
    elif (width // 2) + margin <= mouse[0] <= (width // 2) + margin + box_size and (height // 2) + margin <= mouse[1] <= (height // 2) + margin + box_size:
        return "yellow"
    # 不在任何方块上时返回None
    else:
        return None

# 统一窗口尺寸,避免变量冲突
width, height = 700, 700
window = pygame.display.set_mode((width, height))
pygame.display.set_caption(f"Score: {score}") 

def drawRect(color, x, y, w, h):
    pygame.draw.rect(window, color, pygame.Rect(x, y, w, h))

margin = 10
box_size = (width // 2) - (margin * 2)

# 颜色定义
red = (255, 0, 0)
dred = (200, 0, 0)
black = (0, 0, 0)
blue = (0, 0, 255)
dblue = (0, 0, 200)
green = (0, 255, 0)
dgreen = (0, 200, 0)
yellow = (255, 255, 0)
dyellow = (200, 200, 0)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            current_color = findColor()
            if current_color:
                print(current_color)
                score += 1
                pygame.display.set_caption(f"Score: {score}")

    window.fill(black)
    current_color = findColor()
    
    # 红色方块:悬停变暗
    drawRect(dred if current_color == "red" else red, margin, margin, box_size, box_size)
    # 蓝色方块:修正逻辑,悬停变暗
    drawRect(dblue if current_color == "blue" else blue, (width//2)+margin, margin, box_size, box_size)
    # 绿色方块:悬停变暗
    drawRect(dgreen if current_color == "green" else green, margin, (height//2)+margin, box_size, box_size)
    # 黄色方块:添加悬停变暗效果
    drawRect(dyellow if current_color == "yellow" else yellow, (width//2)+margin, (height//2)+margin, box_size, box_size)

    pygame.display.flip()
     
pygame.quit()

关键修复点

  • 修复findColor()逻辑:为每个方块区域添加完整的X、Y坐标范围判断,避免误判;不在方块上时返回None,避免无意义的状态切换。
  • 修正蓝色方块明暗逻辑:与其他方块保持一致,悬停时显示暗色,正常状态显示亮色。
  • 统一窗口尺寸变量:删除重复的width、height赋值,确保坐标计算与窗口实际尺寸匹配。
  • 优化代码结构:提前获取当前鼠标对应的颜色,避免多次调用findColor();给黄色方块添加悬停变暗效果,保持视觉统一。

内容的提问来源于stack exchange,提问作者ImBadAtMath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 07:36:17