Python Forum
[PyGame] Timing the spawning enemies
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Timing the spawning enemies
#1
Hi Guys, I am beyond frustrated with this problem and need some desperate help...Stack Overflow wasn't at all helpful. Outside of my game loop, I have created a function that creates a list of 200 enemies with random coordinates. These enemies are suppose to start at the stop of the screen and then drop down at random speeds. Inside the loop, I use a "for" loop to blit the enemies on screen - it works, but all 200 hundred are spawned and fall at the same time, albeit, at different speeds. So I know I need a timer... and herein lies the problem, nothing I do works. Ive tried clock.tick(), pygame.delay(), import time and do the time.time() method...everything either strobes or the system just crashes... can anyone help me here please?

import pygame
import sys
import random
import time

pygame.init()
        
#MAIN GAME

game_screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Beer Goggles")

bg = pygame.image.load("bg.png")
bg_image = pygame.transform.scale(bg, (600, 600))

class Object:

    def __init__(self, image_path, width, height, x, y):

        self.image_path = image_path
        self.width = width
        self.height = height
        self.x = x
        self.y = y

        player = pygame.image.load(image_path)
        self.player_main = pygame.transform.scale(player, (width,height))

    def draw(self, background):

        background.blit(self.player_main, (self.x, self.y))

#enemies

def enemy():

    enemy_list = []

    for e in range(200):

        x_cor = random.randint(25, 361)
        
        e = Object("enemy.png", 70, 70, x_cor, 25)
        enemy_list.append(e)

    return enemy_list

#Main Objects
        
player1 = Object("crate.png", 70, 70, 25, 500)
list1 = enemy()

#ladies
fat_lady = Object("fat_lady.png", 300, 300, 360, 180)
chubby = Object("chubby.png", 300, 300, 360, 180)
thin = Object("thin.png", 300, 300, 360, 180)
topless = Object("topless.png", 300, 300, 360, 180)

#title

#title = Object("bgog.png", 150, 100, 360, 25)

# Main Loop

direction = 0

game_on = True

while game_on:

    clock = pygame.time.Clock() 
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_on = False
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                direction = 1
            elif event.key == pygame.K_LEFT:
                direction = -1
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                direction = 0

    game_screen.fill((0,0,0))
    game_screen.blit(bg_image, (0,0))

    #title.draw(game_screen)
    player1.draw(game_screen)
    fat_lady.draw(game_screen)

    #move

    if direction > 0:
        player1.x = player1.x + 10
    elif direction < 0:
        player1.x = player1.x - 10

    #boundries

    if player1.x <= 25:
        player1.x = 25
    elif player1.x >= 360:
        player1.x = 360

    #for enemy in list1:

    for enemy in list1:

        a = random.randint(1, 30)

        enemy.draw(game_screen)
        enemy.y += a
    
    #collisions

    #scoring
       
    pygame.display.update()

quit()
Reply
#2
This is one way to time things.
now = pygame.time.get_ticks()
if now - self.last_spawn > 500:
    self.last_spawn = now
    e = Object("enemy.png", 70, 70, x_cor, 25)
    enemy_list.append(e)
Reply
#3
Outside the while loop create an index variable which starts at 0. Also create another variable which will store a time and set its value to inline]pygame.time.get_ticks()[/inline]. Now in the while loop create an if statement something like this:
timeStamp = pygame.time.get_ticks()
if timeStamp - lastTimeStamp >= 500: # lastTimeStamp is the variable storing time and replace 500 with another integer that works well
    # get the enemy with the number value from the index variable
    lastTimeStamp = timeStamp # resets the time stamp
    index += 1 # The index variable
I think this will work
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  spawning enemies in pygame Elberg 2 5,526 Mar-05-2020, 09:45 AM
Last Post: Windspar
  [PyGame] Spawning platforms that don't touch michael1789 15 5,967 Jan-26-2020, 04:02 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020