0
我有解釋的數組:如何使用我的播放器的座標和陣列中任意塊的座標來檢測碰撞?
blocks0 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1 ,1 ,1 ,1]
blocks1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks2 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks3 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks4 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks5 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks6 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks7 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks8 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks9 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks10= [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks11= [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1 ,1 ,1 ,1 ,1]
blockList = [blocks0, blocks1, blocks2, blocks3, blocks4, blocks5, blocks6, blocks7, blocks8, blocks9, blocks10, blocks11]
A 1表示的塊。 A 0表示開放空間。 每個數字是一個50x50像素塊/空間。 使用blockList [y] [x]我可以單獨找到每個數字。
y表示列表組中的哪個列表。 x表示列表y中的哪個數字。
但是我沒有在我的代碼中使用x和y。
我有一個角色是50x50像素,如果我使用箭頭鍵,每個循環移動10個像素。
如何檢測我的播放器和數字1之間的碰撞?
我曾嘗試:
lefX = x
rigX = x + player_width
topY = y
botY = y + player_height
lefX -= int(lefX % 50)
rigX -= int(rigX % 50)
topY -= int(topY % 50)
botY -= int(botY % 50)
lefX = int(lefX/50)
rigX = int(rigX/50)
topY = int(topY/50)
botY = int(botY/50)
if blockList[topY][lefX] == 1: ##This is just for going left.
if not((blockList[topY][lefX]*50)+50 >= x):
x += x_changeLeft
else:
x += x_changeLeft
我曾嘗試一些其他的方式,但我擺脫他們的時候,他們沒有工作。
任何幫助表示讚賞。 任何問題都將盡快回復。
感謝
雖然我不確定如何回答這個問題,但在實際的pygame窗口中,您可以使用pygame組來檢測碰撞。查看此鏈接,瞭解可用於檢測精靈組之間衝突的方法的詳細信息。 http://www.pygame.org/docs/ref/sprite.html –
我看了一下,但是,我沒有看到我可以如何將它納入我的代碼。不管怎麼說,還是要謝謝你。 – Ruarri