私は、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
編集:画像/四角形をインタラクティブにするためにメソッドを呼び出す必要があると思いますか?