我正在创建自己的第一款游戏,但在处理碰撞时遇到了一些麻烦。我所拥有的是使用同一键盘,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
我能够通过添加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