无法从群体中删除奇异精灵

时间:2017-12-06 09:17:34

标签: python pygame

现在我试图做两件涉及从群体中删除精灵的事情。我想完成两件事:

  1. 首先,我有子弹和通电。一旦我的子弹与我的通电相撞,我希望删除那些特定的子弹和通电。
  2. 其次,如果在一段时间后没有击中我的通电,那么我希望它们被移除。
  3. 目标1的问题:一旦我的子弹与通电发生碰撞,我的所有子弹都会冻结 目标2的问题:我的所有通电都被移除,而不是一次一个通电。

    这是我的子弹课程:

    class Bullet(Image):
    
        def __init__(self, screen_size, width, height, filename, color = (255, 0, 0)):
    
            super().__init__(screen_size, width, height, filename, color = (255, 0, 0))
    
        def checkCollision(self, powerup):
    
            return pygame.sprite.collide_mask(self, powerup)
    
        def removeBullet(self, powerup):
            if self.checkCollision(powerup):
                self.kill()
    
        def update(self, powerup):
            self.rect.centerx += 3
            self.removeBullet(powerup)
    

    这是我的PowerUp Class

    class PowerUp(Image):
    
        def __init__(self, screen_size, width, height, filename,  color = (255, 0, 0)):
    
            super().__init__(screen_size, width, height, filename, color = (255, 0 , 0))
    
    
            _centerx = random.randint(20, 1100) # <-- local variable _centerx (don't need it in other methods)
            _centery = random.randint(20, 700) # <-- local variable _centery (don't need it in other methods)
            self.radius = 10
            self.rect = pygame.Rect(_centerx-self.radius,
                                    _centery-self.radius,
                                    self.radius*2, self.radius*2)
    
        def update(self):
    
            pass
    

    这是我运行一切的地方:

    class GameScreen(SceneBase):
    
        def __init__(self):
            SceneBase.__init__(self)
    
            self.my_font = pygame.font.SysFont("Arial", 50)
    
            self.bullets_group = pygame.sprite.Group()
            self.powerup_group = pygame.sprite.Group()
            self.player_paddle = classes.PlayerPaddle(screen_size, 20, 100, "naruto.png", self.bullets_group)
            self.ai_paddle = classes.AIPaddle(screen_size, 20, 100, "naruto.png")
            self.pong = classes.Pong(screen_size, 20, 20, "pokeball.png")
    
            self.now1 = pygame.time.get_ticks()
            self.now2 = pygame.time.get_ticks()
    
    
        def ProcessInput(self, events, pressed_keys):
            for event in events:
                self.player_paddle.handle_event(event)
    
        def Update(self):
            self.player_paddle.update() 
            self.ai_paddle.update(self.pong, self.player_paddle) 
            self.pong.update(self.player_paddle, self.ai_paddle)
    
            for powerup in self.powerup_group:
                self.bullets_group.update(powerup)
            self.powerup_group.update()
    
    
            # if someone reaches a score of 2, then exit the game 
            if self.player_paddle.score == 2 or self.ai_paddle.score == 2:
                SceneBase.is_game_over = True 
                self.SwitchToScene(GameOverScreen())
    
            time_difference1 = pygame.time.get_ticks() - self.now1
            time_difference2 = pygame.time.get_ticks() - self.now2
    
            if time_difference1 >= 2000:
                self.powerup_group.add(classes.PowerUp(screen_size, 40, 40, "ramen.png"))
                self.now1 = pygame.time.get_ticks()
    
            for powerup in self.powerup_group:
                if time_difference2 >= 5000:
                    self.powerup_group.remove(powerup)
    

    如果我的PlayerPaddle类,如果我点击一个键,子弹就会从它启动。然而,子弹没有向右移动(就像我在Bullet的更新功能中要求的那样)。

    def handle_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                self.direction = -1
            elif event.key == pygame.K_DOWN:
                self.direction = 1      
            elif event.key == pygame.K_LEFT:
                self.turnLeft()
            elif event.key == pygame.K_RIGHT:
                self.turnRight()
            elif event.key == pygame.K_u:
                bullet = Bullet(screen_size, 25, 25, "naruto.png", color = (255, 0 , 0))
                bullet.rect.centerx = self.rect.centerx
                bullet.rect.centery = self.rect.centery 
    
                self.bullet_group.add(bullet)
    

1 个答案:

答案 0 :(得分:1)

目标2:您为

中的所有通电使用相同的计时器
 time_difference2 = pygame.time.get_ticks() - self.now2 

 ...

 if time_difference2 >= 5000:

所以它会在同一时间内删除所有通电。

每个通电都需要在课堂内拥有自己的计时器。我将名称kill_timecurrent_time+5000

一起使用
    time_difference1 = pygame.time.get_ticks() - self.now1

    # ---

    current_time = pygame.time.get_ticks()

    if time_difference1 >= 2000:
        powerup = classes.PowerUp(screen_size, 40, 40, "ramen.png")
        powerup.kill_time = current_time + 5000
        self.powerup_group.add(power)
        self.now1 = pygame.time.get_ticks()

    # ----

    current_time = pygame.time.get_ticks()

    for powerup in self.powerup_group:
        if powerup.kill_time <= current_time:
            self.powerup_group.remove(powerup)