繁体   English   中英

使用图像作为按钮与 pygame

[英]Using an image as a button with pygame

因此,我正在尝试使用已为其创建图像的按钮创建游戏。 通常我会使用:

rect = pygame.Rect(x, y, width, height)

pygame.draw.rect(screen, color, rect)

for event in pygame.event.get()
    if event.type == pygame.MOUSEBUTTONUP:
        if rect.collidepoint(event.pos[0], event.pos[1]):
            do_something()

我不确定是否只是我不太了解 pygame 但我很确定这不适用于图像,因为图像在被 blitted 时只有 x 和 y。

我需要做什么才能使图像像按钮一样起作用?

这实际上很简单,但在此之前我真的不知道该怎么做才能解决这个问题!

我所要做的就是

img = pygame.image.load("path/to/image.png")
x = 10
y = 10

while True:
    screen.blit(img, (x, y))

    rect = img.get_rect()

    rect[0] = x
    rect[1] = y

    for event in pygame.event.get():
        if event.type == MOUSEBUTTONUP:
            if rect.collidepoint(event.pos[0], event.pos[1]):
                do_something()

暂无
暂无

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

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