I have created a pygame, the object of the game is to avoid the balls moving around the screen (random)
ballpic = pygame.image.load('ball.png').convert_alpha()
I have a level function:
def levels(score):
global enemies
global velocity
enemies = enemies
velocity = velocity
#level one
if score >= 500:
enemies = 6
velocity = 2
#level two
if score >= 1000:
enemies = 6
velocity = 2
#....And so on
Game with indentation: ///Expired/
if I try to do that I get this error:
Traceback (most recent call last):
File "C:\Users\MO\Desktop\Twerk\twerk-bck.py", line 252, in <module>
game()
File "C:\Users\MO\Desktop\Twerk\twerk-bck.py", line 199, in game
positionx[i]=positionx[i]+positionxmove[i]
IndexError: list index out of range
I understand I need to append new values to the lists to expand them by 3 more entries. what I’m trying to achieve is to add more balls to the screen score hits certain figure. But I don’t have any idea how I can do that ?
Thank you
You initialize
positionxand friends at line 134 before the main loop at 147. Then you calllevels(score)inside the loop, which updates theenemiescount but doesn’t actually allocate more space inpositionxfor the extra enemies.levels()should be responsible for resizing the arrays when it increases the enemy count.As Lattyware commented, using objects for each enemy would be a much better design. Generally if you’re keeping any kind of parallel arrays like
positionxandpositiony, you should replace them with an array of objects. Then all your different loops overrange(enemies)would be replaced by one big loop like:where
globalGameStateis one or more variables keeping track of stuff like the mouse position that is the same for all enemies.