尝试编写点击时消失的正方形网格,错误的正方形消失,我不知道为什么

Trying to code a grid of squares that disappear when clicked, wrong squares disappear and I have no idea why

我是编程新手,我决定编写扫雷游戏作为一种锻炼方式。 为此,我首先创建了一个代码,该代码使 10 x 10 单元格的网格在单击时消失。然而,点击方块有时会使其他方块以不可预知的方式消失。

这是一张显示所发生情况的 gif:

square_disappearing

单击同一个方块可能会使多个方块消失或消失,我不知道是什么触发了它,这让我觉得问题可能不是来自我的代码?顺便说一句:

import pygame
import sys
listeSuppr = []  # list of squares that shouldn't be drawn
pygame.init()
WIDTH = 800
HEIGHT = 800
GREY = (50, 50, 50)
BLACK = (0, 0, 0)


def add_to_list(a, liste_locale):            # looks at the position of the mouse and says what square it corresponds to
    a = pygame.mouse.get_pos()               # then adds it to the list of squares that shouldn't be drawn (listeSuppr)
    print(a)
    for x in range(HEIGHT // hauteur):
        if hauteur * x < int(list(a)[1]) < hauteur * (x + 1):
            break
    print(x)
    for y in range(WIDTH // longueur):
        if longueur*y < int(list(a)[0]) < longueur*(y+1):
            break
    print(y)
    if not [longueur*y, hauteur*x] in listeSuppr:
        liste_locale.append([longueur*y, hauteur*x])
    print(liste_locale)


longueur = WIDTH//10
hauteur = HEIGHT//10
screen = pygame.display.set_mode((WIDTH, HEIGHT))
game_over = False
while not game_over:
    for x in range(HEIGHT//hauteur):          # looks if the program should draw the squares or not
        for y in range(WIDTH//longueur):
            if not [longueur*y, hauteur*x] in listeSuppr:
                pygame.draw.rect(screen, GREY, (longueur*y, hauteur*x, int(longueur*0.90), int(hauteur*0.90)))
            else:
                pygame.draw.rect(screen, BLACK, (longueur * y, hauteur * x, int(longueur * 0.90), int(hauteur * 0.90)))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # detects if the game has to close
            sys.exit()
        elif pygame.mouse.get_pressed() == (1, 0, 0):  # detects when right-click has been pressed to delete the square
            print(pygame.mouse.get_pos())
            add_to_list(0, listeSuppr)
            listeSuppr.sort()
            print(listeSuppr)
        elif pygame.mouse.get_pressed() == (0, 0, 1):  # detects when left-click has been pressed to reset the grid
            listeSuppr.clear()
    pygame.display.update()

抱歉,如果代码看起来像垃圾,哈哈。也很抱歉 vars 和 fuctions 的半法文半英文名称...... 如果有人知道代码或我编码网格的方式是否有问题,或者这不是我的错但你知道解决问题的方法,请告诉我!

add_to_list 中的第一个循环运行到最后并且找不到有效坐标时,问题总是会发生。当您单击一个像素时,总是会发生这种情况,该像素的 x 坐标可被 hauteur 整除,而 y 坐标可被 longueur 整除。在这种情况下,条件 hauteur * x < int(list(a)[1]) < hauteur * (x + 1) and/or longueur*y < int(list(a)[0]) < longueur*(y+1) 永远不会满足。
您必须为第一个或第二个条件评估 <= 而不是 <,以覆盖整个像素范围:

if hauteur * x < int(list(a)[1]) < hauteur * (x + 1):

if hauteur * x <= int(list(a)[1]) < hauteur * (x + 1):

if longueur*y < int(list(a)[0]) < longueur*(y+1):

if longueur*y <= int(list(a)[0]) < longueur*(y+1):

无论如何,可以通过使用 //(底除法)运算符来计算单元格索引来简化代码:

def add_to_list(a, liste_locale):  
    a = pygame.mouse.get_pos()  
    print(a)
    x = a[1] // hauteur
    print(x)
    y = a[0] // longueur
    print(y)
    if not [longueur*y, hauteur*x] in listeSuppr:
        liste_locale.append([longueur*y, hauteur*x])
    print(liste_locale)