Search code examples
pythonpygame

I am trying to create an instance of a class every time 5 seconds pass in pygame, but instead it creates a ton of instances instead of 1


import pygame as pg
import random
from player import Player

pg.init()
pg.font.init()


#screen settings
(width, height) = (800, 600)
bg = (128, 128, 128)
screen = pg.display.set_mode((width, height))
pg.display.set_caption("2D game")


#speed of the game and color rgb values
speed = 1

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
cyan = (0,255,255)
colors = [red, green, blue, yellow, cyan]


#player
player = Player((255, 0, 127), 50, 50, 1)
player.rect.x = 380
player.rect.y = 500

#enemy
enemy1 = Player(random.choice(colors), 30, 30, random.randint(50, 100))
enemy1.rect.x = 50
enemy1.rect.y = -50


#adds the player and enemies to their respective lists
all_sprites_list = pg.sprite.Group()
all_sprites_list.add(player)
all_sprites_list.add(enemy1)

all_enemies = pg.sprite.Group()
all_enemies.add(enemy1)

#clock
clock = pg.time.Clock()
passed_time = 0

def make_enemy(name, color, width, height, speed):
    name = Player(color, width, height, speed)
    name.add(all_sprites_list)
    name.add(all_enemies)

def textboard(font ,text, color, x, y):
    font = pg.font.SysFont(font, 50)
    textsurf = font.render(text, False, color)
    screen.blit(textsurf, (x,y))


#game loop
running = True
while running:

    passed_time = round(pg.time.get_ticks()/1000)
    if passed_time%5 == 0:
        variable_name = 0
        variable_name += 1
        speed += 0.01
        make_enemy('enemy' + str(variable_name), random.choice(colors), 30, 30, random.randint(50, 100))

    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_x:
                running = False

    keys = pg.key.get_pressed()
    if keys[pg.K_RIGHT]:
        player.moveRight(5)
    if keys[pg.K_LEFT]:
        player.moveLeft(5)

    for enemy in all_enemies:
        enemy.enemyFall(speed)
        if enemy.rect.y > height:
            enemy.changeSpeed(random.randint(50, 100))
            enemy.rect.y = random.randint(50, 100) * -1
            enemy.changePosition()
            enemy.randColor(random.choice(colors))

    collisions = pg.sprite.spritecollide(player, all_enemies, False)
    for enemy in collisions:
        running = False

    all_sprites_list.update()

    screen.fill(bg)

    textboard('ComicSans', 'timer: ' + str(passed_time), cyan, 100, 400)

    all_sprites_list.draw(screen)

    pg.display.flip()
    clock.tick(60)

pg.quit()
pg.font.quit()

This is the game itself. I am having trouble with the make_enemy() function making like tens of instances instead of the one instance I want. I think it has something to do with the ticks, but I am not able to figure it out.

Basically when I run this, after 5 seconds it creates like 10-30 enemies which overpopulate the screen, and I want it to create only one every 5 seconds.


Solution

  • " I think it has something to do with the ticks,"

    Yes of course. The condition if passed_time%5 == 0: is fulfilled for a second.

    Set a time point when the first enemy has t o spawn. If the time point has exceeded, then spawn an enemy and set the next time point:

    variable_name = 0
    next_enemy_time = 0  
    
    running = True
    while running:
    
        if pg.time.get_ticks() > next_enemy_time:
            next_enemy_time += 5000
    
            variable_name += 1
            speed += 0.01
            make_enemy('enemy' + str(variable_name), random.choice(colors), 30, 30, random.randint(50, 100)) 
    

    Alternatively you can use a timer event. Use pygame.time.set_timer() to repeatedly create an USEREVENT. e.g.:

    milliseconds_delay = 5000 # 0.5 seconds
    enemy_event = pygame.USEREVENT + 1
    pygame.time.set_timer(enemy_event, milliseconds_delay)
    

    Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event, which spawns the enemies.

    Create a new enemy when the event occurs in the event loop:

    milliseconds_delay = 5000 # 0.5 seconds
    enemy_event = pygame.USEREVENT + 1
    pygame.time.set_timer(enemy_event, milliseconds_delay)
    variable_name = 0
    
    running = True
    while running:
    
        clock.tick(FPS)
    
        for event in pygame.event.get():
            if event.type == pg.QUIT:
                running = False
    
            elif event.type == enemy_event:
                variable_name += 1
                speed += 0.01
                make_enemy('enemy' + str(variable_name), random.choice(colors), 30, 30, random.randint(50, 100)) 
    
            # [...]