我正嘗試使用碰撞檢測來檢測我的鼠標何時擊中了我導入的圖像。我得到錯誤「元組沒有屬性矩形」Pygame中使用精靈的碰撞檢測
def main():
#Call the SDL arg to center the window when it's inited, and then init pygame
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
#Set up the pygame window
screen = pygame.display.set_mode((600,600))
image_one = pygame.image.load("onefinger.jpg").convert()
screen.fill((255, 255, 255))
screen.blit(image_one, (225,400))
pygame.display.flip()
while 1:
mousecoords = pygame.mouse.get_pos()
left = (mousecoords[0], mousecoords[1], 10, 10)
right = image_one.get_bounding_rect()
if pygame.sprite.collide_rect((left[0]+255, left[1]+400, left[2], left[3]), right):
print('Hi')
非常好的幫助!我發現了一個更簡單的方法來解決它。當你將圖像「blit」到屏幕上時,它會返回一個Rect。您只需指定一個等於該矩形的變量,即:FirstL_Rect = screen.blit(image_one,(185,400)),然後使用「碰撞」功能執行碰撞檢測。你可以通過簡單地做這個來改變你的mous einto: mousecoords = pygame.mouse.get_pos() coordinateRect = Rect(mousecoords [0],mousecoords [1],10,10) – Parseltongue 2011-01-16 00:39:30