简体   繁体   English

Python冲突检测

[英]Python collision detection

I have been trying to add collision detection for a while now and just can't seem to do it.. 我已经尝试添加冲突检测了一段时间了,但是似乎无法做到。

To draw my map I'm just using x,y coords: 要绘制地图,我只是使用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]

Using this method I came up with this detection: 使用这种方法,我想到了这种检测方法:

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

I have been trying to use for loops to loop through trees to detect if player (a rectangle) crosses into the trees but I cant think of anything. 我一直在尝试使用for循环遍历树木,以检测玩家(一个矩形)是否穿过树木,但是我什么都没想到。

I've tried 我试过了

    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) changes the rectangle to red if collision = true and leaves it black if false. 如果冲突= true,player.update(collision)会将矩形更改为红色,如果为false,则将矩形保留为黑色。

I've tried using for and if eg: 我试过使用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)

etc.. but that doesn't work, it only for with 1 if statement and the rest commented out. 等等。但这不起作用,仅适用于1 if语句,其余注释掉。

One problem is with the nature of this loop: 一个问题是此循环的性质:

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)

Suppose the player collides with the first tree in trees . 假设玩家在第一树碰撞trees Then collision will be True, but the for loop continues. 然后collision将为True,但是for循环继续。 So collision will only end up being True if the player is colliding with the last tree in your list. 因此,只有当玩家与列表中的最后一棵树碰撞时, collision才会最终为True。 There are multiple ways to fix this. 有多种解决方法。

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

or 要么

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

or 要么

# 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))

Your logic for when collisions occur looks right to me, though it can also be simplified quite a bit: 您的发生冲突的逻辑对我来说很正确,尽管也可以将其简化很多:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM