我不知道为什么,但是当我在移动我的播放器的同时按下右键然后按下左键时,它会出现故障,它会抛出矩形 VIDEO 它可以完美地反过来工作,但它会继续这样做如果我按住右键然后在按住右键的同时单击左键,它将传送我扔块我不知道如何解决这个问题,任何帮助表示赞赏! VIDEO <问题的另一个视频
我的侧面碰撞
# sides for player and player screen movement
platform_rect_list = [p.rect for p in collids]
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)
playerman.y = py
if player_rect.collidelist(platform_rect_list) < 0:
playerman.x = px
move_right = keys[pygame.K_RIGHT]
move_left = keys[pygame.K_LEFT]
if move_right:
base1.x -= playerman.speed
base2.x -= playerman.speed
base3.x -= playerman.speed
base4.x -= playerman.speed
base5.x -= playerman.speed
base6.x -= playerman.speed
crate1.x -= playerman.speed
base44.x -= playerman.speed
end1.x -= playerman.speed
base45.x -= playerman.speed
stopmove1.x -= playerman.speed
text_show_collision.x -= playerman.speed
text1.x -= playerman.speed
rop1.x -= playerman.speed
text22.x -= playerman.speed
text_show_collision2.x -= playerman.speed
portal1.x -= playerman.speed
base46.x -= playerman.speed
for shot in shots:
shot.x -= playerman.speed
for collid in collids:
collid.x -= playerman.speed
if move_left:
base1.x += playerman.speed
base2.x += playerman.speed
base3.x += playerman.speed
base4.x += playerman.speed
base5.x += playerman.speed
base6.x += playerman.speed
crate1.x += playerman.speed
base44.x += playerman.speed
end1.x += playerman.speed
base45.x += playerman.speed
stopmove1.x += playerman.speed
text_show_collision.x += playerman.speed
text1.x += playerman.speed
rop1.x += playerman.speed
text22.x += playerman.speed
text_show_collision2.x += playerman.speed
portal1.x += playerman.speed
base46.x += playerman.speed
for shot in shots:
shot.x += playerman.speed
for collid in collids:
collid.x += playerman.speed
platform_rect_list = [p.get_rect() for p in collids] # get_rect()
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)
playerman.y = py
cI = player_rect.collidelist(platform_rect_list)
if cI >= 0:
# undo movement of platforms dependent on the direction and intersection distance
dx = 0
if move_right:
dx = platform_rect_list[cI].left - player_rect.right
if move_left:
dx = platform_rect_list[cI].right - player_rect.left
for collid in collids:
collid.x -= dx
collid.get_rect() # update rectangle
base1.x -= dx
base2.x -= dx
base3.x -= dx
base4.x -= dx
base5.x -= dx
base6.x -= dx
crate1.x -= dx
base44.x -= dx
end1.x -= dx
base45.x -= dx
stopmove1.x -= dx
text_show_collision.x -= dx
text1.x -= dx
rop1.x -= dx
text22.x -= dx
text_show_collision2.x -= dx
portal1.x -= dx
base46.x -= dx
for shot in shots:
shot.x -= dx
答案 0 :(得分:1)
很可能同时按下 left 和 right 很可能会导致问题。
按下两个按钮时根本不要移动播放器。清理您的代码,使其更易于维护和理解:
object_list = [
base1, base2, base3, base4, base5,
crate1, base44, end1, base45, stopmove1,
text_show_collision, text1, rop1, text22,
text_show_collision2, portal1, base46]
def move_in_x(dx):
for obj in object_list:
obj.x += dx
for shot in shots:
shot.x += dx
for collid in collids:
collid.x += dx
# sides for player and player screen movement
platform_rect_list = [p.rect for p in collids]
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)
playerman.y = py
if player_rect.collidelist(platform_rect_list) < 0:
playerman.x = px
move_right = keys[pygame.K_RIGHT]
move_left = keys[pygame.K_LEFT]
if move_right and not move_right: # <--- move to the left, but not to the right
move_in_x(-playerman.speed)
if move_left and not move_left: # <--- move to the right, but not to the left
move_in_x(playerman.speed)
platform_rect_list = [p.get_rect() for p in collids] # get_rect()
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)
playerman.y = py
cI = player_rect.collidelist(platform_rect_list)
if cI >= 0:
if move_left != move_right: # <--- move either left or right
# undo movement of platforms dependent on the direction and intersection distance
dx = 0
if move_right:
dx = platform_rect_list[cI].left - player_rect.right
if move_left:
dx = platform_rect_list[cI].right - player_rect.left
move_in_x(-dx)
for collid in collids:
collid.get_rect() # update rectangle