[英]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.