在这个程序中,我有一个带有网格的表面。我正在尝试编写一个程序,可以检测鼠标在表面上的位置,并且如果我的鼠标在其中一个方格内,该方格将变为绿色。问题是,我只成功让左上角的一个方格工作起来了。我希望所有的方格都能像左上角的那个一样正常工作。有谁可以帮忙吗?
import pygame
pygame.init()
WIDTH = 540
TILESIZE = 60
RED = (255, 0, 0)
BLACK = (0, 0, 0)
GREY = (211, 211, 211)
GREEN = (0, 255, 0)
win = pygame.display.set_mode((WIDTH+1, WIDTH+1))
win.fill(GREY)
surface = pygame.Surface((TILESIZE-1, TILESIZE-1))
surface.fill(RED)
def mouse_pos():
surface_place = (1, 1)
for x in range(0, 9, TILESIZE):
for y in range(x):
surface_place = list(surface_place)
surface_place[0] += TILESIZE
surface_place[1] += TILESIZE
surface_place = tuple(surface_place)
win.blit(surface, surface_place)
if surface.get_rect().collidepoint(pygame.mouse.get_pos()):
win.blit(surface, surface_place)
surface.fill(GREEN)
else:
win.blit(surface, surface_place)
surface.fill(RED)
def draw_grid():
for x in range(0, WIDTH+1, TILESIZE):
pygame.draw.line(win, BLACK, (x, 0), (x, WIDTH))
for y in range(0, WIDTH+1, TILESIZE):
pygame.draw.line(win, BLACK, (0, y), (WIDTH, y))
clicked = False
while not clicked:
draw_grid()
mouse_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
clicked = True
pygame.display.update()