我正在嘗試使用 for 循環(huán)來檢查用戶是否單擊了我的 pygame 中的庫存槽。我創(chuàng)建了名稱為“slot1”、“slot2”、“slot3”等的插槽,一直到“slot13”。我的問題真的是;如何為每次迭代添加“槽”后面的數(shù)字?if event.type == pg.MOUSEBUTTONDOWN and event.button == 3: for i in range(14): if mouse_click and inventory_state and slot+str(i).collidepoint(pos): inventory.equip_item(inventory.items[i])
1 回答
一只甜甜圈
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
不要將插槽存儲(chǔ)在不同的變量中,而是存儲(chǔ)在列表中。然后你可以遍歷這個(gè)列表:
for i in range(13): if mouse_click and inventory_state and slots[i].collidepoint(pos): inventory.equip_item(inventory.items[i])
添加回答
舉報(bào)
0/150
提交
取消
