You need to enable JavaScript to run this app. 为什么使用Pygame开发扫雷游戏时鼠标点击无法立即响应?
优惠活动
大模型
产品
解决方案
定价
更多

为什么使用Pygame开发扫雷游戏时鼠标点击无法立即响应?

扫雷游戏重制实现

我正在重制扫雷游戏。

我的代码会遍历800×600分辨率画布上的所有方块,每个方块尺寸为50×50像素,总共有192个方块。我会判断点击位置是否落在对应方块内,再根据方块属性绘制炸弹或者空白区域。

def check_for_click(x_click,  y_click, x_square, y_square, x_circle, y_circle, a_round, a_count):
    if x_click < x_square and y_click < y_square and x_click > (x_square - 50) and y_click > (y_square - 50):
        if grid[(a_round - 1)][(a_count)] == 1:
            bomb.play()
            choose = random.randint(0, 6)
            the_color = random.choice(light_colors[choose])
            make_square(the_color, (x_square - 50), (y_square - 50))
            the_color = random.choice(dark_colors[choose])
            make_circle(the_color, x_circle, y_circle)
            pygame.display.update()
        if grid[(a_round - 1)][(a_count)] == 0:
            the_grass.play()
            make_square(light_brown, (x_square - 50), (y_square - 50))
            pygame.display.update()
            grid[(a_round - 1)][(a_count)] = 2

上述函数用于判断点击位置是否落在指定方块范围内。

word = True
while word:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            word = False
    
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            click = pygame.mouse.get_pos()
            check = True
            while check == True:
                round = 1
                count = 0
                the_x = 50
                the_y = 50
                cir_x = 25
                cir_y = 25
                x = click[0]
                y = click[1]
                for i in range(12):
                    for i in range(16):
                        check_for_click(x, y, the_x, the_y, cir_x, cir_y, round, count)
                        the_x = the_x + 50
                        cir_x = cir_x + 50
                        count = count + 1
                    the_y = the_y + 50
                    cir_y = cir_y + 50
                    the_x = 50
                    cir_x = 25
                    count = 0
                    round = round + 1
                check = False

以上是游戏主循环部分:每次检测到点击操作时,会进入一轮循环,遍历所有方块的位置,判断点击是否落在该方块内。

update_score()
    draw_numbers()
    pygame.display.update()

pygame.quit()

以上是我编写的其他功能函数,为精简篇幅此处不附上具体实现逻辑。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 06:24:05