繁体   English   中英

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

[英]How to find the button on the entered position by user and change it's color in 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()

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


编辑:

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()

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

永远不要在应用程序循环中多次获取事件( pygame.event.get() )。

使用pygame.mouse.get_pos()获取鼠标的position。 使用单元格的大小创建pygame.Rect object 并使用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)]

单击单元格时更改单元格的 state:

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]

根据单元格的 state 设置颜色:

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()

如果要阻止 select 单元格,在单击 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

    # [...]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM