我試圖在Pygame的創建工作跳躍系統返回True,但我有帶衝突檢測的一些問題。我有一個級別存儲在這樣的列表中:pygame的功能將不會在colliderect
level = [
"WWWWWWWWWWWWWWWWWWWW",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WWWWWWWWWWWWWWWWWWWW",
]
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
x += 32
y += 32
x = 0
其中每個牆都附加到列表並根據其在網格中的位置給出位置。
這是牆壁類:
class Wall (pygame.sprite.Sprite):
def __init__(self,pos):
pygame.sprite.Sprite.__init__(self)
walls.append(self)
self.rect = pygame.Rect(pos[0],pos[1],32,32)
我試圖移動玩家RECT 30個像素高達當上鍵被按下,當檢測到的球員,瓷磚之一之間的碰撞:
if event.type == KEYDOWN and event.key == K_UP:
if player.hitCheck():
player.move(0,-30)
hitCheck(),播放器類的功能,如下所示:
def hitCheck(self):
for wall in walls:
if self.rect.colliderect(wall.rect):
return True
然而,無論實際發生什麼碰撞,該函數都不會返回True。我能夠移動玩家RECT沒有設置碰撞球員方有問題的對面牆上方的價值的價值穿牆裁剪,但這種產生任何結果。
爲什麼不是函數返回true碰撞?
牆壁最初聲明爲空列表。它在運動功能,是玩家級的一部分,這樣使用:
def move_single_axis(self, dx, dy):
# Move the rect
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
這工作沒有問題,這就是爲什麼我無法理解爲什麼hitCheck()不能檢測碰撞。
在hitCheck,怎麼就沒有返回false最後一條語句? – 2014-09-02 08:23:40
爲了解決問題,我刪除了它,因爲我只是想知道爲什麼hitCheck不會返回True。 – Efonias 2014-09-02 08:28:05
爲了記錄,您是否驗證過,以便您的牆實際上被添加到牆上列表中,並且初始化代碼中沒有某種錯誤? – Dolda2000 2014-09-02 08:52:09