0

I am making a small game using pygame in python 3.3 and I have a class for the enemies (see below). If I want to spawn in a new enemy every 10 or so seconds, so that theoretically, the game could go on forever. This however required the instances of the classes to be generated automatically every 10 seconds. Is there a way to create a new instance of the enemy class automatically?

Enemy class:

class Enemy(object):
    def __init__(self, image, posx, posy, speed, damage):
        self.image = image
        self.posx = posx
        self.posy = posy
        self.speed = speed
        self.damage = damage
    def move(self):
        self.posy = self.posy + self.speed
    def draw(self):
        screen.blit(self.image, (self.posx, self.posy))

Edit: Sorry! I posted this, not realising I didn't finish writing my explanation. My apologies!

jakoubeck503
  • 55
  • 1
  • 6

4 Answers4

3

You need to store spawned enemies somewhere. Suppose it's a list:

enemies = []
...
# every 10 secs
enemy = Enemy()
enemies.add(enemy)

Then, when enemy is killed, you'd like to remove it from that list.

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
2

look into a module called time

you would need to do something like this:

import time

nSeconds = 11
t0 = time.clock()
dt = 0
enemyCount = 0

# the following would be in an appropriate
# place in the main game loop
if dt < nSeconds:
    t1 = time.clock()
    dt = t1 - t0
else:
    enemyInstance = Enemy()
    enemyCount += 1
    t0 = time.clock()
    dt = 0

you can keep track of the enemy count just in case you want to send a special wave after a number of enemies or give bonus points or whatever.

also have a look at this book about game development in python for other ideas: http://inventwithpython.com/makinggames.pdf

samkhan13
  • 3,315
  • 2
  • 33
  • 54
  • PyGame has [pygame.time.get_ticks()](http://www.pygame.org/docs/ref/time.html) so you don't need to use `time` library – furas Nov 25 '13 at 19:15
  • what furas pointed out is correct. but do be careful when you are using time related functions from any library so that you don't end up with negative time on the particular hardware configuration. have a look at: [measure elapsed time in python](http://stackoverflow.com/q/7421641/1463143) – samkhan13 Nov 26 '13 at 05:14
  • I'm sorry if my original question isn't clear, but I don't have a problem with the time aspect, but with the actual automatic spawning of the enemies; the automatic generation of class instances. – jakoubeck503 Nov 26 '13 at 07:25
  • @jakoubeck503 all the answers here tell you how to automatically create an instance of a class. i'm not sure if you were able to grasp the concept that your entire game is inside a while loop or similar loop. you can make use of that loop to figure out the "game state" and accordingly create instances of a class based on the game state. please look at page 338 in makinggames.pdf which shows how you can automatically and even randomly create game elements like enemies. – samkhan13 Nov 26 '13 at 09:33
1

In a game usually there is a main game loop, where inputs are read, the simulation is advanced and the screen is rendered. In that loop, you can check how much time has passed since the last time a enemy was created, and you can create one if enough time flew by.

Thiago Chaves
  • 9,218
  • 5
  • 28
  • 25
1
# before mainloop
enemies = []
time_for_next_enemy = pygame.time.get_ticks() + 10000 # 10 seconds , 1000ms = 1s

# in mainloop
if pygame.time.get_ticks() >= time_for_next_enemy:
    enemy = Enemy()
    enemies.add(enemy)
    time_for_next_enemy = pygame.time.get_ticks() + 10000
furas
  • 134,197
  • 12
  • 106
  • 148