Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame,同一组中两个对象之间的碰撞_Python_Pygame_Collision - Fatal编程技术网

Python Pygame,同一组中两个对象之间的碰撞

Python Pygame,同一组中两个对象之间的碰撞,python,pygame,collision,Python,Pygame,Collision,所以,我试图创建一个游戏,让外星人从3个特定的地方产卵。每一个外星生物都会在3个区域中随机繁殖。但总会有至少一个外星生物在另一个上面繁殖。我想删除那个外星人并在另一个繁殖点随机繁殖他。如果是空的,他会留下来,如果不是,这个过程会重复。问题是我找不到一种方法来检测同一组中两个对象的碰撞 我刚开始学习pygame,所以1)我的问题可能很愚蠢2)我的产卵方式可能效率很低 这是外星人类: class Alien(pygame.sprite.Sprite): def __init__(self):

所以,我试图创建一个游戏,让外星人从3个特定的地方产卵。每一个外星生物都会在3个区域中随机繁殖。但总会有至少一个外星生物在另一个上面繁殖。我想删除那个外星人并在另一个繁殖点随机繁殖他。如果是空的,他会留下来,如果不是,这个过程会重复。问题是我找不到一种方法来检测同一组中两个对象的碰撞

我刚开始学习pygame,所以1)我的问题可能很愚蠢2)我的产卵方式可能效率很低

这是外星人类:

class Alien(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface((80,60))
    self.image.fill(GREY)
    self.rect = self.image.get_rect()
    spawn_point1 = x1,y1 = -30, 70
    spawn_point2 = x2,y2 = -30, 150
    spawn_point3 = x3,y3 = -30, 230
    random_spawn = random.choice([spawn_point1,spawn_point2,spawn_point3])
    self.rect.center = random_spawn
    self.speedx = 10

def update(self):
    spawn_point1 = x1,y1 = -30, 70
    spawn_point2 = x2,y2 = -30, 150
    spawn_point3 = x3,y3 = -30, 230
    self.speedx = 10
    random_spawn = random.choice([spawn_point1,spawn_point2,spawn_point3])
    self.rect.x += self.speedx

    if self.rect.x > WIDTH + 20:
        self.rect.center = random_spawn
这是我检测碰撞的部分(这部分不起作用)


下面是边界框测试的一个实现

import random


class Rectangle:

    def __init__(self, height, width, x, y):
        self.height = height
        self.width = width
        self.x = x
        self.y = y

    def collided_with_another_rectangle(self, rect):
        """ Assumes rectangles are same size or that this rectangle is smaller than the other rectangle"""
        if self.x > (rect.x + rect.width):
            # Is to the right of the other rectangle
            return False
        elif (self.x + self.width) < rect.x:
            # is to the left of the other rectangle
            return False
        elif (self.y + self.height) < rect.y:
            # is above the other rectangle
            return False
        elif self.y > (rect.y + rect.height):
            # is below the other rectangle
            return False
        else:
            return True


collision_count = 0
for i in range(0, 1000):
    # Here I pick random locations on a 1000X1000 screen for the first rectangle 
    x1 = random.randint(0, 1000)
    y1 = random.randint(0, 1000)
    # Here I pick random locations on a 1000X1000 screen for the second rectangle 
    rect1 = Rectangle(100, 100, x1, y1)
    x2 = random.randint(0, 1000)
    y2 = random.randint(0, 1000)
    rect2 = Rectangle(100, 100, x2, y2)
    """
     I use the collided with another rectangle function to test if the first rectangle is above,below, 
     to the right or to the left of the other rectangle. If neither of these are true then the rectangles 
     have collided.
    """
    if rect1.collided_with_another_rectangle(rect2):
        collision_count += 1
        print("Rect1 X and Y:" + str(x1) + " " + str(y1))
        print("Rect2 X and Y:" + str(x2) + " " + str(y2))
        print("collided")

print("Collision Count:" + str(collision_count))
随机导入
类矩形:
定义初始值(自身、高度、宽度、x、y):
自我高度=高度
self.width=宽度
self.x=x
self.y=y
def与另一个矩形(self,rect)发生碰撞:
“”“假定矩形大小相同,或者此矩形比另一个矩形小”“”
如果self.x>(矩形x+矩形宽度):
#在另一个矩形的右侧
返回错误
elif(自x+自宽度)(矩形y+矩形高度):
#在另一个矩形的下面
返回错误
其他:
返回真值
碰撞计数=0
对于范围(0,1000)内的i:
#在这里,我在1000X1000屏幕上为第一个矩形选择随机位置
x1=random.randint(0,1000)
y1=random.randint(0,1000)
#在这里,我在1000X1000屏幕上为第二个矩形选择随机位置
rect1=矩形(100100,x1,y1)
x2=随机随机随机数(0,1000)
y2=random.randint(0,1000)
rect2=矩形(100100,x2,y2)
"""
我使用“与另一个矩形碰撞”函数来测试第一个矩形是否在上方、下方,
在另一个矩形的右边或左边。如果这两个都不是真的,那么矩形
发生了碰撞。
"""
如果rect1.与另一个矩形碰撞(rect2):
碰撞计数+=1
打印(“矩形1 X和Y:+str(x1)+”“+str(y1))
打印(“Rect2 X和Y:+str(x2)+”“+str(y2))
打印(“冲突”)
打印(“碰撞计数:+str(碰撞计数))

我仍然不能完全确定您想要实现什么,但我认为这个示例将对您有所帮助

当精灵离开屏幕时,我调用
reset\u pos
方法,在该方法中,我迭代三个繁殖点,将位置设置为一个接着一个繁殖点,然后使用另一个for循环在精灵上迭代,以检查是否有一个碰撞

如果精灵发生碰撞,我将继续下一个繁殖点

如果没有精灵碰撞,我就从方法返回

如果没有免费的繁殖,我将移除精灵(但你可以做其他事情)


当在组冲突中使用同一组时,总是考虑组中的子组与A组中的同一个子组冲突。这会导致groupcollide始终返回碰撞

为了解决这个问题,我在pygame的sprite.py中创建了一个新函数,它忽略单个碰撞,只返回大于等于2的碰撞。我唯一的改变是添加:

if len(collision) >=2:
然后是下一行所需的选项卡

我添加到sprite.py的代码粘贴在下面,但def intra_GROUPCLIDE的选项卡太远了:

def intra_groupcollide(groupa, groupb, dokilla, dokillb, collided=None):
"""detect collision between a group and itself.
This is modified from groupcollide but excludes collisions <=1

pygame.sprite.groupcollide(groupa, groupb, dokilla, dokillb):
    return dict

"""
crashed = {}
# pull the collision function in as a local variable outside
# the loop as this makes the loop run faster
sprite_collide_func = spritecollide
if dokilla:
    for group_a_sprite in groupa.sprites():
        collision = sprite_collide_func(group_a_sprite, groupb,
                                        dokillb, collided)
        if collision:
            if len(collision) >=2:
                crashed[group_a_sprite] = collision
                group_a_sprite.kill()
else:
    for group_a_sprite in groupa:
        collision = sprite_collide_func(group_a_sprite, groupb,
                                        dokillb, collided)
        if collision:
            if len(collision) >=2:
                crashed[group_a_sprite] = collision



                
            
#print(crashed)
return crashed
def intra_groupcollide(groupa、groupb、dokilla、dokillb、collide=None):
“”“检测组与自身之间的冲突。

这是从groupcollide修改而来的,但不包括冲突。请向我们显示代码(a)。我把它放进去了。正如我上面所说的,第二部分不起作用。这是一篇关于碰撞检测的好文章。当我不久前实施边界盒测试时,我使用了边界盒测试。每个车道应该只有一个外星精灵,还是每个车道可以有多个精灵,而你只想避免两个精灵直接相互叠加?应该有一个是的,我想避免它们相互重叠。谢谢你的回答。你能解释一下最后一部分发生了什么吗?我不知道,这只是我的实现,所以假设你是1000 X 1000像素的屏幕。我随机选择一个(X,y)坐标为rect1和rect2。然后我测试rect1是否与rect2冲突。我只是想向您展示如何实现冲突。重要的部分实际上是“Collized_with_”另一个矩形方法“如果你想知道两个矩形是否碰撞。哦,好吧,我知道了:)只有一件事。在它们发生碰撞后,如何删除其中一个?是否有.remove()方法?谢谢您的回答!你在那里做的基本上就是我想再次做的,谢谢!
if len(collision) >=2:
def intra_groupcollide(groupa, groupb, dokilla, dokillb, collided=None):
"""detect collision between a group and itself.
This is modified from groupcollide but excludes collisions <=1

pygame.sprite.groupcollide(groupa, groupb, dokilla, dokillb):
    return dict

"""
crashed = {}
# pull the collision function in as a local variable outside
# the loop as this makes the loop run faster
sprite_collide_func = spritecollide
if dokilla:
    for group_a_sprite in groupa.sprites():
        collision = sprite_collide_func(group_a_sprite, groupb,
                                        dokillb, collided)
        if collision:
            if len(collision) >=2:
                crashed[group_a_sprite] = collision
                group_a_sprite.kill()
else:
    for group_a_sprite in groupa:
        collision = sprite_collide_func(group_a_sprite, groupb,
                                        dokillb, collided)
        if collision:
            if len(collision) >=2:
                crashed[group_a_sprite] = collision



                
            
#print(crashed)
return crashed