如何在迭代时从组中删除一个精灵(列表)?

发布于 2025-01-29 21:29:26 字数 1511 浏览 2 评论 0 原文

我希望,如果子弹触摸(幽灵),则组中的一个幽灵(列表)将消失。这是代码的一部分。 PS;很抱歉我以前的问题

import pygame, math, random, os

class Bullet(pygame.sprite.Sprite):
     def __init__(self, x, y, direction):
        super().__init__() 
         self.x = x + 15
         self.y = y + 25
         self.fx = 10
         self.fy = 10
         self.direction = direction

     def draw_bullet(self, screen):
         screen.blit(bullet_img, (self.x, self.y))

      def move(self):
          if self.direction == 1:
              self.x += 15
         if self.direction == -1:
             self.x -= 15                  
      def off_screen(self):
          return not(self.x >= 0 and self.x <= width)
           
          def update(self):
              self.rect.x += 5
   class Ghost(pygame.sprite.Sprite):
        def __init__(self, x, y, fx,fy):
           pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('ghost.png').convert_alpha()
            self.rect = self.image.get_rect() 
            self.rect.x = x
            self.rect.y = y
            self.fartx = fx
            self.farty = fy 

     lst = [0,1,2,3,4,5,6,7]
     for i in range(len(lst)): 
     ghost = Ghost(random.randint(0,width),random.randint(0,height),random.randint(1,8),random.randint(1,8))
             ghostgruppe.add(ghost)
    
    ghosttruffet = pygame.sprite.groupcollide(ghostgruppe, bulletgruppe, True, True, pygame.sprite.collide_mask)

I want that if a bullet is touching (ghost), the one ghost from the group(list) would disappear. Here is a part of the code. Ps; I'm sorry for my previous questions

import pygame, math, random, os

class Bullet(pygame.sprite.Sprite):
     def __init__(self, x, y, direction):
        super().__init__() 
         self.x = x + 15
         self.y = y + 25
         self.fx = 10
         self.fy = 10
         self.direction = direction

     def draw_bullet(self, screen):
         screen.blit(bullet_img, (self.x, self.y))

      def move(self):
          if self.direction == 1:
              self.x += 15
         if self.direction == -1:
             self.x -= 15                  
      def off_screen(self):
          return not(self.x >= 0 and self.x <= width)
           
          def update(self):
              self.rect.x += 5
   class Ghost(pygame.sprite.Sprite):
        def __init__(self, x, y, fx,fy):
           pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('ghost.png').convert_alpha()
            self.rect = self.image.get_rect() 
            self.rect.x = x
            self.rect.y = y
            self.fartx = fx
            self.farty = fy 

     lst = [0,1,2,3,4,5,6,7]
     for i in range(len(lst)): 
     ghost = Ghost(random.randint(0,width),random.randint(0,height),random.randint(1,8),random.randint(1,8))
             ghostgruppe.add(ghost)
    
    ghosttruffet = pygame.sprite.groupcollide(ghostgruppe, bulletgruppe, True, True, pygame.sprite.collide_mask)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2025-02-05 21:29:26

如果Glost重叠,并且您只想删除触摸子弹的拳头幽灵,则需要将 false 传递给 dokill1 pygame.sprite.groupcollide.groupcolleide() =“ https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.sprite.sprite.kill” rel =“ nofollow noreferrer”> 第一个幽灵:

ghosttruffet = pygame.sprite.groupcollide(
    ghostgruppe, bulletgruppe, False, True, pygame.sprite.collide_mask)
if ghosttruffet:
    list(ghosttruffet.keys())[0].kill()

请参阅

group1中的每个精灵都添加到返回字典中。每个项目的值是相交的组2中的精灵列表。

If the Glosts overlap and you just want to delete the fist ghost touching the bullet, you need to pass False to the dokill1 argument of pygame.sprite.groupcollide() and manually kill() the first ghost:

ghosttruffet = pygame.sprite.groupcollide(
    ghostgruppe, bulletgruppe, False, True, pygame.sprite.collide_mask)
if ghosttruffet:
    list(ghosttruffet.keys())[0].kill()

See pygame.sprite.groupcollide():

Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文