有点长,但我不是在寻找复制和粘贴的答案。我只想知道如何解决这个问题的好方法。粘贴代码以获得视觉偏好。
更好的说法。我的图像与另一个图像发生碰撞,该对象代表瓷砖(地板/屋顶/墙壁/等)。
我将这些图块放置在整个屏幕的不同位置。我使用 rect 对象存储这些位置,并将它们存储到一个名为terrain
. 所以我遍历我的列表,当我遇到我的英雄和一些地形对象的碰撞时,停止移动。
我现在意识到这是一个问题。当发生这种情况并且我确实发生了碰撞时,我无法再移动我的对象,它被卡住了。
那么我怎样才能让我的对象仍然移动,但是当它与某个对象发生碰撞时。不允许继续朝同一方向进一步移动。
这是运动控制的主要部分:
该moveDown
声明是我尝试实现它的部分。
floorCollusion
是 5 个 False 布尔值的列表。
#0 represents - no collision
#1 " " - top face has collided
#2 " " - right face has collided
#3 " " - bottom face has collided
#4 " " - left face has collided
我看到我有二进制操作。要么我搬家,要么不搬家。我只是想不出另一种方法来实现这一点。
您无需添加到代码中。关于如何看到这一点的基本想法将不胜感激。
moveDown = False
moveUp = False
moveLeft = False
moveRight = False
#when key down either move the hero or quit game
elif event.type == KEYDOWN:
if event.key in (K_UP, K_w):
moveDown = False
moveUp = True
playerObj['jumping'] = True #@@@@@@
elif event.key in (K_DOWN,K_s):
moveUp = False
moveDown = True
elif event.key in (K_LEFT, K_a):
moveRight = False
moveLeft = True
#if the image is not facing left, display the left img
if (playerObj['facing']) != LEFT:
playerObj['surface'] = L_HERO_IMG
playerObj['facing'] = LEFT
elif event.key in (K_RIGHT, K_d):
moveLeft = False
moveRight = True
#if the image is not facing right, display the right img
if (playerObj['facing']) != RIGHT:
playerObj['surface'] = R_HERO_IMG
playerObj['facing'] = RIGHT
elif event.key == K_ESCAPE:
terminate()
#testing hit point system
elif event.key == K_SPACE:
playerObj['health'] -= 3
if playerObj['health'] <= 0:
gameOver = True
#when key is lifted stop movement
elif event.type == KEYUP:
if event.key in (K_UP, K_w):
moveUp = False
elif event.key in (K_DOWN, K_s):
moveDown = False
elif event.key in (K_LEFT,K_a):
moveLeft = False
elif event.key in (K_RIGHT,K_d):
moveRight = False
#moving the hero
if not gameOver:
if moveLeft:
playerObj['x'] -= MOVERATE
if moveRight:
playerObj['x'] += MOVERATE
#if the player pressed key up or 'w' and hero isn't jump
#the hero will seems to have a jumping affect
if moveUp:
playerObj['y'] -= MOVERATE
if moveDown:
playerObj['y'] += MOVERATE
for i in range(len(terrain)):
tile = terrain[i]
if playerObj['rect'].colliderect(tile):
floorCollusion[2] = True
break
else:
floorCollusion[2] = False
if floorCollusion[2]:
playerObj['x'] -= MOVERATE