如何在游戏中创建两种类型的碰撞



嗨,我试图创建一个2D运行游戏,所以我试图定义两个碰撞函数。一个是在我撞到障碍物时结束比赛,另一个是当我撞到硬币时增加分数。我把与障碍物的碰撞定义为"碰撞",把与硬币的碰撞定义成"special_collide"。我面临的问题是"碰撞"有效,但"special_collide"无效。这是代码:-

# The two functions
def collide(x1, y1, x2, y2):
distance = math.hypot(x1 - x2, y1 - y2)
return distance <= 27

def special_collide(coinX, coinY, playerX, playerY):
distance_square = math.pow(coinX - playerX, 2) + math.pow(coinY - playerY, 2)
if distance_square == 27*27:
return True
else:
return False
# Inside the while loop
# For increasing our score
s_collision = special_collide(coinX, coinY, playerX, playerY)
if s_collision:
print("Special collision occured!")
# Collision Detection
obstacles = [(binX, binY), (snowX, snowY), (glacierX, glacierY), (boardX, boardY), (iceX, iceY)]
for x, y in obstacles:
collision = collide(x, y, playerX, playerY)
if collision:
print("You have collided!")

special_collide中的距离计算可能错误

试试这个代码:

def special_collide(coinX, coinY, playerX, playerY):
distance_square = (math.pow(coinX - playerX, 2) + math.pow(coinY - playerY, 2))**.5
if distance_square <= 27:
return True
else:
return False

最新更新