0

I am trying to handle the collision with shot and asteroid, but after the collision I want to add an explosion.

I added a timer so the explosion is going to show on the screen after some time, but the the explosion shows immediately, and I dont see the problem. This is the function with the problem in my code.

def handle_collision_with_shot_asteroid(self):
    for asteroid_rect in self.asteroid_list:
        for shot_rect in self.shots:
            # check for collision
            if asteroid_rect.colliderect(shot_rect):
                # remove the shot and the asteroid from the screen
                self.asteroid_list.remove(asteroid_rect)
                self.shots.remove(shot_rect)

                # create an effect after the asteroid get shot
                explosion_rect = self.explosion_big.get_rect(center=(asteroid_rect.x+29, asteroid_rect.y+29))


                # record the time the explosion started
                self.explosion_start_time = pygame.time.get_ticks()
    try:            
        if pygame.time.get_ticks() - self.explosion_start_time > 1000:
            # show the explosion
            screen.blit(self.explosion_big, explosion_rect)

            # Reset the explosion start time so it doesn't show again
            self.explosion_start_time = 0
    except:
        pass
  • You set `explosion_start_time` to `pygame.time.get_ticks()` and then check if it is greater than 2000. `pygame.time.get_ticks()` is greater than 2000. – khelwood Feb 01 '23 at 22:04
  • Seems like you just need `if self.explosion_start_time - pygame.time.get_ticks() > 2000`? I say this because according to [the documentation](https://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks), `pygame.time.get_ticks()` gets the number of ticks since `pygame.init()` was called. Meaning that your code is designed to show an explosion if the game has been running for more than 2000 ticks (aka 2 seconds). – Random Davis Feb 01 '23 at 22:04

2 Answers2

0

I believe that you should be doing something like

  if pygame.time.get_ticks() - self.explosion_start_time > 2000:

Also check if the api for ticks() returns what you expect it to.

Dino Dini
  • 433
  • 3
  • 6
  • i try it but i kept getting an error so i did this (look at the question i change the code there) and i dont see the explosion at all now why is that? –  Feb 01 '23 at 22:24
  • @RoMMk if you're getting an error you need to show it to us and not just catch it. The explosion probably isn't happening because the error is happening every time. – Random Davis Feb 01 '23 at 22:34
0

i fix it with this code:

def handle_collision_with_shot_asteroid(self):

    for asteroid_rect in self.asteroid_list:

        for shot_rect in self.shots:
            # check for collision
            if asteroid_rect.colliderect(shot_rect):
                # remove the shot and the asteroid from the screen
                self.asteroid_list.remove(asteroid_rect)
                self.shots.remove(shot_rect)

                # record the time the explosion started
                self.explosion_start_time = pygame.time.get_ticks()

                # defined the x,y of the explosion
                self.x,self.y = asteroid_rect.x-20, asteroid_rect.y+15

    
    if self.explosion_start_time:
        if pygame.time.get_ticks() - self.explosion_start_time > 200:
            # show the explosion
            screen.blit(self.explosion_big, (self.x,self.y))
            

            # Reset the explosion start time so it doesn't show again
            self.explosion_start_time = 0