Oventoaster
Oventoaster

Reputation: 299

Spawning Multiple of the same sprite

I am currently working on a game called Table Wars, a turn based strategy game for two players. Progress has been going smoothly, until I ran into a problem with spawning units.

The program won't spawn multiple of the same unit, or respawn new ones after the old ones die.

Here is some information that may help:

What should I do?

As requested, here is the class for the Red Infantry:

class Red_Infantry(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image('Soldier_red.png', -1)
        self.selected = 0
        self.area = screen.get_rect()
        self.rect.topleft = (100, 300)
        self.health = 100 #Soldiers are have mediocre toughness.
        self.attack_damage = 25
        self.range_maximum = 20 #in pixels, this is melee range
        self.range_minimum = 0
        self.update()
    def update(self):
        if self.health <= 0:
            self.kill()

and the code to spawn this unit:

if spawned_units < 3 and PHASE == 1 and TURN == 'Red':
    if REDGOLD < 10:
        print "Out of money! Moving to Phase 2!"
        PHASE = 2
        spawned_units = 0
    elif event.type == KEYDOWN and event.key == K_1:
        if REDGOLD >= 10 and REDCOMMAND >= 5:
            Sprites.append(redI)
            REDGOLD -= 10
            REDCOMMAND -= 5
            spawned_units = spawned_units + 1
        else:
            print "Not enough gold!"

This is similar style with all units. It performs correctly the first time, but not in the second, third, and so on, meaning I can only have one Soldier. Also, when that soldier dies via self.kill, it won't come back if I try to spawn it.

Upvotes: 3

Views: 6918

Answers (1)

dank.game
dank.game

Reputation: 4719

The part of the spawn procedure you posted doesn't create any new instances. Unless redI is declared as a new Red_Infantry somewhere else, you need to modify the code to create a new instance of Red_Infantry every time you want to spawn a soldier.

sprites.append(Red_Infantry())

To update the sprites:

for sprite in sprites:
    sprite.update()

Put movement and other state changes in the update method. This is what pygame expects, but you can use a different style if you want. The main point is you must have multiple instances of Red_Infantry in order to see multiple sprites.

You could also use pygame's Group class instead of a simple list to hold the sprites.


Here's a full example that uses Group instead of list. In the example, an Enemy is spawned each time a key is pressed. Each Enemy prints its unique ID to stdout.

import sys
import random

import pygame
from pygame.locals import *

def main():
    pygame.init()
    screen = pygame.display.set_mode((480, 320))
    enemies = pygame.sprite.Group()
    while True:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                enemies.add(Enemy(screen))
            elif event.type == QUIT:
                sys.exit()
        enemies.update()
        screen.fill(pygame.Color("black"))
        enemies.draw(screen)
        pygame.display.update()

class Enemy(pygame.sprite.Sprite):

    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)
        print "created a new sprite:", id(self)
        self.image = pygame.image.load("sprite.png")
        self.rect = self.image.get_rect()
        self.rect.move_ip(random.randint(0, screen.get_width()),
                          random.randint(0, screen.get_height()))

    def update(self):
        self.rect.move_ip(random.randint(-3, 3), 0)

if __name__ == "__main__":
    main()

Upvotes: 4

Related Questions