Pygame, Столкновение между 2 объектами в одной группе - PullRequest
0 голосов
/ 06 июня 2018

Итак, я пытаюсь создать игру, в которой пришельцы появляются из 3 определенных мест.Каждый инопланетянин будет случайным образом появляться в одном из 3. Но всегда будет хотя бы один инопланетянин, который будет появляться поверх другого.Я хочу удалить этого инопланетянина и вызвать его случайно в другой точке появления.Если он пуст, он останется, если не будет повторен процесс.Дело в том, что я не могу найти способ обнаружить столкновение двух объектов, находящихся в одной и той же группе.

Я только начал изучать Pygame, поэтому 1) Мой вопрос может быть глупым 2) Мой способ нереста, вероятно, оченьнеэффективно

Вот класс Alien:

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)

Ответы [ 2 ]

0 голосов
/ 06 июня 2018

Я все еще не совсем уверен, чего вы хотите достичь, но я думаю, что этот пример будет вам полезен.

Когда спрайт покидает экран, я вызываю метод reset_pos, в котором яВыполните итерацию по трем точкам появления, чтобы установить позицию для одного появления за другим, а затем я использую другой цикл for для итерации по спрайтам, чтобы проверить, сталкивается ли один из них.

Если сталкивается спрайт, я продолжаю со следующей точкой появления.

Если спрайт не сталкивается, я просто возвращаюсь из метода.

Если спавн не свободен, я удаляю спрайт (но вы можете сделать что-то еще).

import random

import pygame
from pygame.math import Vector2


pygame.init()
WIDTH, HEIGHT = 640, 480


class Alien(pygame.sprite.Sprite):

    def __init__(self, aliens):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((80, 60))
        self.image.fill((120, random.randrange(255), random.randrange(255)))
        self.rect = self.image.get_rect()
        self.spawn_points = [(-30, 70), (-30, 150), (-30, 230)]
        self.aliens = aliens
        self.reset_pos()
        self.speedx = 10

    def update(self):
        self.rect.x += self.speedx

        if self.rect.x > WIDTH + 20:
            self.reset_pos()

    def reset_pos(self):
        random.shuffle(self.spawn_points)  # Shuffle the spawns.

        for spawn in self.spawn_points:
            # Set the position to one of the spawns.
            self.rect.center = spawn
            # Check if this sprite collides with another one.
            for sprite in self.aliens:
                if sprite is self:  # Skip self.
                    continue
                if self.rect.colliderect(sprite.rect):
                    break  # Break out of the loop if the spawn is occupied.
            else:  # The else means no 'break' occurred in the for loop above,
                   # so the spawn must be free.
                return  # Break out of the method if the spawn is free.

        # I just remove the sprite if no spawn is free. You can do something else here.
        self.kill()


def main():
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    aliens = pygame.sprite.Group()
    for _ in range(3):
        # I pass the aliens group to the sprite because we need to
        # iterate over it to see if a sprite collides.
        alien = Alien(aliens)
        aliens.add(alien)
    all_sprites = pygame.sprite.Group(aliens)

    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                  al = Alien(aliens)
                  all_sprites.add(al)
                  aliens.add(al)

        all_sprites.update()

        screen.fill((30, 30, 30))
        all_sprites.draw(screen)

        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pygame.quit()
0 голосов
/ 06 июня 2018

Вот реализация теста Bounding Box.

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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...