python - 如何在pygame中找到用户输入位置上的按钮并更改其颜色?

标签 python python-3.x pygame

import pygame

pygame.init()

width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))

def drawGrid():
    for y in range (50):
        for x in range (50):
            pygame.draw.rect (screen , ( 0 , 0 , 255 ) , ( ( x * 20 ) , ( y * 20 ) , 20 , 20 ) , 1)
        for event in pygame.event.get():    
            if event.type == pygame.MOUSEBUTTONUP:
                pos = event.pos
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((0, 0, 0))
    drawGrid()
    pygame.display.update()

所以我基本上创建了一个网格,并且还弄清楚了如何获取用户单击屏幕的位置。我现在想做的是,使用用户单击的位置,我想找到该位置对应的按钮。一旦我找到哪个按钮对应于用户单击的位置,我想将按钮的颜色更改为其他颜色。


编辑:

import pygame

pygame.init()

width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))

cell_size = 20
size_x = width // cell_size
size_y = height // cell_size
grid = [[False for y in range(size_y)] for x in range(size_x)]

def drawGrid():
    mouse_pos = pygame.mouse.get_pos()
    for y in range (size_y):
        for x in range (size_x):
            # rectangle and color of cell
            cell_rect = pygame.Rect(x * 20, y * 20, 20, 20)           
            cell_color = (0, 0, 255)

            # change color of button if the mouse is on the button
            if cell_rect.collidepoint(mouse_pos):
                cell_color = (255, 255, 255)
            elif grid[x][y]:
                cell_color = (0, 255, 255)
            pygame.draw.rect(screen, cell_color, cell_rect, 1)   

z = 0

while running and z < 2:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
            grid[cell_x][cell_y] = not grid[cell_x][cell_y]
            z += 1
    screen.fill((0, 0, 0))
    drawGrid()
    pygame.display.update()

所以基本上我想要做的是我想停止单击两个矩形之后的矩形。就像如果用户已经单击了两个框(起点和终点)一样,那么每当用户单击矩形时我不想再添加更多框。我让它工作,但问题是我只有在单击两次整个窗口关闭时才让它工作。我该如何解决这个问题?

最佳答案

切勿在应用程序循环中多次获取事件 (pygame.event.get())。

使用pygame.mouse.get_pos()来获取鼠标的位置。创建 pygame.Rect具有单元格大小和我们的对象 collidepoint()评估鼠标是否在单元格中。

def drawGrid():

    # get current mouse position
    mosue_pos = pygame.mouse.get_pos()

    for y in range (50):
        for x in range (50):

            # rectangle and color of cell
            cell_rect = pygame.Rect(x * 20, y * 20, 20, 20)           
            cell_color = (0, 0, 255)

            # change color of button if the mouse is on the button
            if cell_rect.collidepoint(mosue_pos):
                cell_color = (255, 255, 255)

            pygame.draw.rect(screen, cell_color, cell_rect, 1)


如果您想在单击时更改单元格,则必须创建状态网格:

grid = [[False for y in range(size_y)] for x in range(size_x)]

单击单元格时更改单元格的状态:

if event.type == pygame.MOUSEBUTTONDOWN:
    cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
    grid[cell_x][cell_y] = not grid[cell_x][cell_y]

根据单元格的状态设置颜色:

if grid[x][y]:
    cell_color = (255, 255, 255)

参见示例:

import pygame

pygame.init()

width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))

cell_size = 20
size_x = width // cell_size
size_y = height // cell_size
grid = [[False for y in range(size_y)] for x in range(size_x)]

def drawGrid():

    for y in range(size_y):
        for x in range(size_x):

            # rectangle and color of cell
            cell_rect = pygame.Rect(x * 20, y * 20, 20, 20)           
            cell_color = (0, 0, 255)

            # change color of button if the mouse is on the button
            if grid[x][y]:
                cell_color = (255, 255, 255)

            pygame.draw.rect(screen, cell_color, cell_rect, 1)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
            grid[cell_x][cell_y] = not grid[cell_x][cell_y]

    screen.fill((0, 0, 0))
    drawGrid()
    pygame.display.update()

如果您想阻止选择单元格,则在单击 2 个框后,您必须在 MOUSEBUTTONDOWN 事件中计算所选单元格的数量:

z = 0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:

            if z < 2:

                cell_x, cell_y = event.pos[0] // cell_size, event.pos[1] // cell_size
                grid[cell_x][cell_y] = not grid[cell_x][cell_y]
                z += 1 if grid[cell_x][cell_y] else -1

    # [...]

关于python - 如何在pygame中找到用户输入位置上的按钮并更改其颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61767256/

相关文章:

Python:检查 globals() 中变量是否存在使其在本地上下文中不可见

python - 将 Pandas Dataframe 中的数据行输入到 csv 文件中的下一个空行

Python3 在网络摄像头 fps 处处理和显示网络摄像头流

python - 尝试做汤。选择多个案例

python - Pandas:将日期划分为 30 分钟间隔并计算平均值

python - Mac OSX 10.7.4 上的 pygame 错误

python - 在python中获取所有可能的单字节

python - 调用另一个类下面的类

python 套接字阻止 recv 挂起?

python - 如何停止一秒钟并计算一秒钟内的点击次数