"if" command is only running once and not repeating

Viewed 45

I'm very new to python and Pygame and am trying to create a game. I would like a "powerup" to appear after a set time, then repeat every time the timer hits so many seconds.

In the code, the "powerup" appears and drops down the screen after 5 seconds, the timer resets, but the next time the timer reaches 5 seconds, it continues to count and no "powerup" is produced.

I've spent hours trying to work out why this isn't working.

import pygame
import random

WIDTH = 480
HEIGHT = 600
FPS = 60

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

time_difference = 0

font_name = pygame.font.match_font('arial')
def draw_text(surf, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

class HealthPack(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 40))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.radius = 50
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-150, -100)
        self.speedy = 6
        self.speedx = 0

    def update(self):
        self.rect.y += self.speedy
        if self.rect.top > HEIGHT:
            self.kill()

all_sprites = pygame.sprite.Group()

healthpack = HealthPack()

running = True
while running:
    clock.tick(FPS)
    time_difference = pygame.time.get_ticks()
    if time_difference >= 5000:
        all_sprites.add(healthpack)
        time_difference -= 5000
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    all_sprites.update()

    screen.fill(BLACK)
    all_sprites.draw(screen)
    draw_text(screen, str(time_difference), 18, WIDTH / 2, 55)

    pygame.display.flip()

pygame.quit()
2 Answers

You're not calculating the time difference since the previous powerup. time_difference is always the time since the game started. You need to save the ticks each time you want to reset the timer, and compare with that.

prev_time = pygame.time.get_ticks() #initialize tick counter
while running:
    clock.tick(FPS)
    time_difference = pygame.time.get_ticks() - prev_time
    if time_difference >= 5000:
        all_sprites.add(healthpack)
        prev_time = pygame.time.get_ticks() # reset counter
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    all_sprites.update()

    screen.fill(BLACK)
    all_sprites.draw(screen)
    draw_text(screen, str(time_difference), 18, WIDTH / 2, 55)

    pygame.display.flip()

@Barmar has given part of the answer. He is correct in his fix for time_difference, but there is another problem with your code as well.

The other issue is with the healthpack. You are doing this all_sprites.add(healthpack), which just puts the old healthpack back in the all_sprites list. That sprite is off the screen and descending. You need a new instance of HealthPack. So you need to modify @Barmar's solution like this:

prev_time = pygame.time.get_ticks() #initialize tick counter
while running:
    clock.tick(FPS)
    time_difference = pygame.time.get_ticks() - prev_time
    if time_difference >= 5000:
        healthpack = HealthPack()      # <-----------------   add this line
        all_sprites.add(healthpack)
        prev_time = pygame.time.get_ticks() # reset counter
Related