在PYGAME中,如何使两个角色在碰撞时不互相碰撞?

2024-09-28 22:19:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建我的第一个游戏,我在处理碰撞时遇到了一些问题。我有一个两人游戏,在同一个键盘上玩,awsd和上下左右控制。当两个玩家发生碰撞时,我希望他们不能互相移动。我很难弄明白

player_one_pos = [300,310]
player_two_pos = [600,310]

def detect_collision(player_one_pos, player_two_pos):
    p1_x = player_one_pos[0]
    p1_y = player_one_pos[1]
    p2_x = player_two_pos[0]
    p2_y = player_two_pos[1]
    if (p1_x + player_width/2) == (p2_x - player_width/2):
        return True
    return False

if detect_collision(player_one_pos, player_two_pos):
    ## players collide, can't go through each other

Tags: pos游戏returnif玩家键盘widthone
1条回答
网友
1楼 · 发布于 2024-09-28 22:19:51

我可以通过添加and语句来完成它。这个修复现在应该足够了

if key_pressed[pygame.K_RIGHT] and x < (goal_right[0] - player_width) and (x != a - player_width):
    x += speed_of_travel
if key_pressed[pygame.K_a] and a > (goal_left[0] + goal_width) and (a != x + player_width):
    a -= speed_of_travel

相关问题 更多 >