Question

I am trying to make a program to generate up to ten "zombies" in pygame. however, i am having trouble with the following code:

ZOMBIE = pygame.image.load('zombie.png')

zombieamount = 0

**zombiepos(1) = [-1,-1]
zombiepos(2) = [-1,-1]
zombiepos(3) = [-1,-1]
zombiepos(4) = [-1,-1]
zombiepos(5) = [-1,-1]
zombiepos(6) = [-1,-1]
zombiepos(7) = [-1,-1]
zombiepos(8) = [-1,-1]
zombiepos(9) = [-1,-1]
zombiepos(10) = [-1,-1]**

while True:
    for i in range (1,10):
         time.sleep(5)
         x=randint(0,30)
         y=randint(0,20)
         **zombiepos(i) = [x,y]**
         global zombieamount
         zombieamount =+ 1

     for i in range (1,zombieamount):
         window.blit(ZOMBIE, (zombiepos(i)[0]*TILESIZE, zombiepos(i)[1]*TILESIZE))

however, the program highlights the bolded staement, stating "can't assign to function call" my teacher stated that it might be because i am trying to assign a two-dimensional variable to a one-dimensional array. Any tips?

Was it helpful?

Solution

You need to tell python your zombiepos is an array before you assign to it through indexing:

zombiepos = []
for _ in range(10):
    zombiepos.append([-1,-1])

you'll have to change the for statement in the while to range(10) as well.

the global statement is unnecessary. you'd need to use it if you were accessing zombieamounts in a function.

also, as mentioned in some of the comments to your code, when you access the items within zombiepos you need to use square brackets [] not parentheses

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top