같은 그룹 내에서 두 스프라이트의 충돌을 계산하는 알고리즘을 어떻게 작성할 수 있습니까?

Arif Othman |

나는 적과 플레이어를 포함한 작은 게임을 만들고 있습니다. 어느 시점에서 플레이어는 적을 쏠 수 있고 적들은 자신의 샷으로 반응 할 수 있습니다. 일부 레벨에는 둘 이상의 적이 포함됩니다. 따라서 어떤 경우에는 플레이어가 그들 사이에 있고 방해가되지 않으면 한 적이 "우연히"다른 적을 쏠 수 있습니다. 두 경비원이 쏘고 플레이어가 비켜 갈 때 두 적의 총알을 모두 전멸시키고 싶습니다. 이것을하는 방법이 있습니까?

현재 무기 클래스에이 메서드가 있습니다.

def selfannihilation(self, guardbulletsprites):
    guardbulletsprites.remove(self)
    selfannihilation = pygame.sprite.spritecollide(self, guardbulletsprites, True)
    f not selfannihilation:
        guardbulletsprites.add(self)

And i call this method within a seperate function, checking bullet conditions. collision with walls, enemies, player, player bullets

```py
for bullet in guardbulletsprites:
    bullet.selfannihilation(guardbulletsprites)

실행 중에 알고리즘이 예상대로 작동하지 않습니다. 글 머리 기호 중 하나만 제거됩니다. 아마도 spritecollide내의 "True"때문일 것입니다 . 두 총알을 guardbulletsprites그룹 에서 제거하고 싶습니다 .

미리 감사드립니다

Arif Othman |

아래 코드는 무기 클래스 내의 메서드입니다.

def selfannihilation (self, guardbulletsprites) :

changed = False
temp = pygame.sprite.Group.copy(guardbulletsprites)
temp.remove(self)
selfannihilation = pygame.sprite.spritecollide(self, temp, True)
if not selfannihilation:
    guardbulletsprites.add(self)
else:
    changed = True

return changed, guardbulletsprites

그리고 이것을 호출하는 함수에서 내가 썼습니다.

def bulletannihilation (guardbulletsprites) :

for bullet in guardbulletsprites:
    change, guardbulletsprites = bullet.selfannihilation(guardbulletsprites)
    if change:
        bulletannihilation(guardbulletsprites)

if change : 부분은 재귀를 사용하여 함수를 다시 호출합니다. 그렇지 않으면 guardbulletsprites의 내용이 변경된 경우에만 guardbulletsprites를 잘못된 횟수로 반복합니다.

나는 실제 guardbulletsprites의 내용이 우연히 제거되는 것처럼 느꼈기 때문에 guardbulletsprites와 거의 같은 내용을 담고있는 temp 변수를 사용했습니다.

솔직히 나는 이것이 어떻게 작동하는지 모른다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관