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

标签 python pygame collision

因此,我正在尝试创建一个外星人从 3 个特定位置生成的游戏。每个外星人都会在 3 个中的一个中随机生成。但是总会有至少一个外星人生成在另一个外星人之上。我想删除那个外星人并在另一个重生点随机重生他。如果它是空的,他将留下来,否则将重复该过程。问题是我找不到一种方法来检测同一组中的 2 个对象的碰撞。

我刚开始学习 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

这是我检测碰撞的部分(这部分不起作用)

aliens_col = pygame.sprite.groupcollide(aliens, aliens, True, False)

for i in aliens_col:
    alien = Alien()
    aliens.add(alien)
    all_sprites.add(aliens)

最佳答案

这是边界框测试的实现。

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

关于python - Pygame,同一组中两个对象之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50725706/

相关文章:

c# - 碰撞时播放随机声音片段 (Unity)

javascript - 碰撞检测问题还是我的逻辑

java - Andengine - 检测与多个对象的碰撞并删除它们 - Java

python scapy : how to translate port numbers to service names?

python - 结合 PySide 和 PyGame

python - 使用 django 创建个人资料页面

Python VLC 不会播放音频?

python - python中的屏幕共享

python - float() 参数必须是字符串或数字,而不是 'zip'

python - 如何使用pip打包的django项目?