Python碰撞检测

标签 python python-2.7 collision

我已经尝试添加碰撞检测有一段时间了,但似乎无法做到。

为了绘制 map ,我只使用 x,y 坐标:

from setup import *

treeload = "Images/tree.jpg"
tree = pygame.image.load(treeload).convert_alpha()

class drawtree:

     def __init__(self, x, width, step1, y, height, step2):

        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.step1 = step1
        self.step2 = step2

    def draw(self):

        for x in range(self.x, self.x+self.width, self.step1):
            for y in range(self.y, self.y+self.height, self.step2):
                window.blit(tree, (x,y))

t1 = drawtree(100, 100, 20, 0, 90, 30)
t2 = drawtree(400, 300, 20, 0, 90, 30)
t3 = drawtree(100, 270, 20, 450, 150, 30)
t4 = drawtree(400, 300, 20, 450, 150, 30)

trees = [t1, t2, t3 ,t4]

使用这种方法我想出了这个检测:

from setup import *
from drawtree import *
from player import *

def wall_detect(x1, y1, w1, h1, x2, y2, w2, h2):
    if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):
        return True
    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):
        return True
    elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):
        return True
    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):
        return True
    else:
        return False

我一直在尝试使用 for 循环来遍历树来检测玩家(一个矩形)是否穿过树,但我想不出任何东西。

我已经尝试过

    for i in trees:
        collision = wall_detect(player.x, player.y, player.width, player.height, i.x, i.y, i.width, i.height)
    player.update(collision)

player.update(collision) 如果碰撞 = true,则将矩形更改为红色;如果 false,则将其保留为黑色。

我尝试使用 for 和 if 例如:

for i in trees:
        if wall_detect(player.x, player.y, player.width, player.height, i.x, i.y, i.width, i.height) == wall_detect(player.x, player.y, player.width, player.height, t1.x, t1.y, t1.width, t1.height):
            collision = wall_detect(player.x, player.y, player.width, player.height, t1.x, t1.y, t1.width, t1.height)
           player.update(collision)

        if wall_detect(player.x, player.y, player.width, player.height, i.x, i.y, i.width, i.height) == wall_detect(player.x, player.y, player.width, player.height, t2.x, t2.y, t2.width, t2.height):
            collision = wall_detect(player.x, player.y, player.width, player.height, t2.x, t2.y, t2.width, t2.height)
            player.update(collision)

等等..但这不起作用,它只适用于 1 个 if 语句,其余的被注释掉。

最佳答案

一个问题在于这个循环的性质:

for i in trees:
    collision = wall_detect(player.x, player.y, player.width, player.height, i.x, i.y, i.width, i.height)
player.update(collision)

假设玩家与trees中的第一棵树发生碰撞。那么collision将为True,但for循环继续。因此,只有当玩家与列表中的最后一棵树发生碰撞时,collision 才会最终为 True。有多种方法可以解决此问题。

# Change the assignment to check if it is already true
collision = False
for i in trees:
    collision = collision or wall_detect(...

# Exit the loop if it is true
for i in trees:
    collision = wall_detect(...)
    if collision:
        break

# Turn it into a function that returns when it is true
def walls_detect(player, trees):
    for tree in trees:
        if wall_detect(...):
            return True
    return False

...
# Call it like this
player.update(walls_detect(player, trees))
<小时/>

我认为您的碰撞发生逻辑是正确的,尽管它也可以简化很多:

def wall_detect(x1, y1, w1, h1, x2, y2, w2, h2):
    return (x2+w2>=x1>=x2 or x2+w2>=x1+w1>=x2) and (y2+h2>=y1>=y2 or y2+h2>=y1+h1>=y2)

关于Python碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23549887/

相关文章:

Python Praw 在 subreddits 中跳过粘性

python - 如何在 GAE/Python 上进行 'access_type=offline'/server-only OAuth2 操作?

python - 是什么导致我的三角形求和解中出现 NZEC(非零退出代码)错误?

python - 可以随时随地创建和更新Python字典吗

swift - didBeginContact 函数未被调用

python - python3-mock 和 unittest.mock 有什么区别?

python - 来自动态导入模块的 ImportError 尝试在同一目录中加载模块

python - 如何在 Python 中最小化特定窗口

Java 平台游戏登陆平台

javascript - 两个第三方JavaScript库命名冲突