当前位置: 首页 > 知识库问答 >
问题:

Python碰撞检测

狄宇
2023-03-14

我已经尝试添加冲突检测一段时间了,但似乎做不到...

要绘制地图,我只需使用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)

如果碰撞=true,player.update(碰撞)将矩形更改为红色,如果为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)

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

共有1个答案

陈朗
2023-03-14

一个问题是该循环的性质:

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)

假设玩家与中的第一棵树相撞。然后碰撞将为True,但循环的将继续。因此,只有当玩家与列表中的最后一棵树碰撞时,碰撞才会成为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)

 类似资料:
  • 碰撞检测 现在你知道了如何制造种类繁多的图形对象,但是你能用他们做什么?一个有趣的事情是利用它制作一个简单的 碰撞检测系统 。你可以用一个叫做:hitTestRectangle 的自定义的函数来检测两个矩形精灵是否接触。 hitTestRectangle(spriteOne, spriteTwo) 如果它们重叠, hitTestRectangle 会返回 true。你可以用 hitTestRect

  • 本节暂未进行完全的重写,错误可能会很多。如果可能的话,请对照原文进行阅读。如果有报告本节的错误,将会延迟至重写之后进行处理。 当试图判断两个物体之间是否有碰撞发生时,我们通常不使用物体本身的数据,因为这些物体常常会很复杂,这将导致碰撞检测变得很复杂。正因这一点,使用重叠在物体上的更简单的外形(通常有较简单明确的数学定义)来进行碰撞检测成为常用的方法。我们基于这些简单的外形来检测碰撞,这样代码会变得

  • 问题内容: 有人可以帮我了解JS中冲突检测的工作原理吗?我不能使用jQuery或gameQuery-已经使用了原型- 因此,我正在寻找非常简单的东西。不要求完整的解决方案,只需为我指明正确的方向。 假设有: 现在球正在移动(任何方向)。“ Someobject”(0-X)已经预先定义,其中有20-60个随机放置,如下所示: 我可以创建一个位置为“ someobject(X)”的数组,并在“球”移动

  • 所以我试图用Python和Pyplay创建一个益智平台游戏,但是我遇到了一点麻烦。当我为主要角色使用单点图像,而不是矩形图像时,如何制作碰撞检测器?我知道直角图像有左、右、顶部和底部像素功能(这对冲突检测非常有用),但是对于单片图像有这样的功能吗?或者我只需要为x和y坐标创建一个变量图像的宽度/高度?我试过用那个 但是catImg一直在通过窗户的尽头。我做错了什么?提前谢谢。

  • 我有一些关于碰撞角度的问题。我正在尝试为一个游戏编写物理代码,我不想使用任何第三方库,实际上我想自己编写每一个东西。我知道如何检测两个球体之间的碰撞,但我不知道如何找到两个球体之间的碰撞/斥力角。我试过扭转物体的方向,但没有成功。如果你把我链接到一个有趣的.pdf文件教物理编程,那将是非常好的。

  • 文:youyou Cocos Creator 提供了一套用于检测 3D 物体碰撞的 API,用户可以使用这些 API 做射线检测之类的检测。 射线检测 cc.geomUtils.intersect.raycast(rootNode, ray, handler, filter) // 根据点击的点获取一条由屏幕射向屏幕内的射线 let ray = camera.getRay(touchPos); /