1 回答

TA貢獻1993條經驗 獲得超6個贊
對象沒有pygame.Surface
位置。返回的矩形的位置get_rect()
總是 (0, 0)。
請注意,當表面為 時,您指定一個位置blit
:
root.blit(house, (400, 100))
當您檢索碰撞的矩形時,您必須設置相同的位置。您可以將關鍵字參數值傳遞給此函數,這些值設置為pygame.Rect
對象:
house.get_rect(topleft = (400, 100))
例如:
while window_is_open:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = event.pos
dx = mouse_x - x
dy = mouse_y - y
if market.get_rect(topleft = (400, 300)).collidepoint(mouse_x, mouse_y):
if abs(dx) <= 50 and abs(dy) <= 50:
open_market()
if house.get_rect(topleft = (400, 100)).collidepoint(mouse_x, mouse_y):
if abs(dx) <= 50 and abs(dy) <= 50:
open_house()
for tree in trees:
if tree.trunk.collidepoint(mouse_x, mouse_y):
if abs(dx) <= 50 and abs(dy) <= 50:
countdown = 3
destroy_tree = tree
# [...]
此外,您必須在函數中使用global
語句將其happiness
視為全局命名空間中的變量open_market
:
def open_market():
global happiness
happiness = score / 2
添加回答
舉報