与PYGAME碰撞时如何使两个字符不互相贯穿?

问题描述 投票:2回答:1

我正在创建自己的第一款游戏,但在处理碰撞时遇到了一些麻烦。我所拥有的是使用同一键盘,awsd和updownleftright控件进行的2人游戏。当两个玩家发生冲突时,我希望他们彼此之间不能移动。我很难弄清楚。

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
python pygame game-physics
1个回答
2
投票

我能够通过添加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
© www.soinside.com 2019 - 2024. All rights reserved.