[英]Using an image as a button with pygame
So I am trying to create a game with buttons that I have created images for.因此,我正在尝试使用已为其创建图像的按钮创建游戏。 Usually I would use:通常我会使用:
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()
Im not sure if its just me not knowing pygame very well but im pretty sure this wouldnt work for an image due to the fact an image only has x and y when being blitted.我不确定是否只是我不太了解 pygame 但我很确定这不适用于图像,因为图像在被 blitted 时只有 x 和 y。
What do I need to do to get the image to act like a button?我需要做什么才能使图像像按钮一样起作用?
This was quite simple actually but before now I had literally no idea what to do to solve this!这实际上很简单,但在此之前我真的不知道该怎么做才能解决这个问题!
All i had to do was我所要做的就是
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.