我一直在编写一个测试函数来了解 pygame.rect 上的鼠标“单击”操作将如何产生响应。
至今:
def test():
pygame.init()
screen = pygame.display.set_mode((770,430))
pygame.mouse.set_visible(1)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250,250,250))
screen.blit(background, (0,0))
pygame.display.flip()
## set-up screen in these lines above ##
button = pygame.image.load('Pictures/cards/stand.png').convert_alpha()
screen.blit(button,(300,200))
pygame.display.flip()
## does button need to be 'pygame.sprite.Sprite for this? ##
## I use 'get_rect() ##
button = button.get_rect()
## loop to check for mouse action and its position ##
while True:
for event in pygame.event.get():
if event.type == pygame.mouse.get_pressed():
## if mouse is pressed get position of cursor ##
pos = pygame.mouse.get_pos()
## check if cursor is on button ##
if button.collidepoint(pos):
## exit ##
return
我在谷歌上遇到过人们正在使用或建议使用pygame.sprite.Sprite
图像类的页面,我认为这就是我的问题所在。我已经检查了 pygames 文档并且方法之间没有太大的凝聚力,恕我直言。我显然遗漏了一些简单的东西,但是,我认为get_rect
pygames 中的图像能够检查按下时鼠标位置是否在其上方?
编辑:我想我需要调用该pygame.sprite.Sprite
方法来使图像/矩形交互?