0

I've been working on a tower defence game for a school project but when I summon in more than one enemy, the speed of all enemies slower down. I've been trying to fix it but it hasn't been working. I used both pygame and pygame zero

enum is which enemy position is being changed emove is a list with the position I want the enemy to get to efullmove is the which index in emove is the enemy in enemies is a list of enemies

def emovement():
        global enum, emove, efullmove, enemies
        speed1=5

        if enum<len(enemies) and len(enemies)!=0:
            while enemies[enum].pos!=emove[efullmove[enum]]:
                if edirect[efullmove[enum]]=='N':
                    enemies[enum].y=enemies[enum].y-speed1
                    enemyhit[enum]=py.Rect.move(enemyhit[enum],0,-speed1)
                    if (enum+1)!=len(enemies):
                        enum+=1
                    else:
                        enum=0
                    return
                if edirect[efullmove[enum]]=='S':
                    enemies[enum].y=enemies[enum].y+speed1
                    enemyhit[enum]=py.Rect.move(enemyhit[enum],0,speed1)
                    if (enum+1)!=len(enemies):
                        enum+=1
                    else:
                        enum=0
                    return
                if edirect[efullmove[enum]]=='E':
                    enemies[enum].x=enemies[enum].x+speed1
                    enemyhit[enum]=py.Rect.move(enemyhit[enum],speed1,0)
                    if (enum+1)!=len(enemies):
                        enum+=1
                    else:
                        enum=0
                    return
                if edirect[efullmove[enum]]=='W':
                    enemies[enum].x=enemies[enum].x-speed1
                    enemyhit[enum]=py.Rect.move(enemyhit[enum],-speed1,0)
                    if (enum+1)!=len(enemies):
                        enum+=1
                    else:
                        enum=0
                    return

            if enemies[enum].pos==emove[efullmove[enum]]:
                efullmove[enum]=efullmove[enum]+1
            if (enum+1)!=len(enemies):

                enum+=1
            else:
                enum=0
            if enemies[enum].pos==emove[-1]:
                del(enemies[enum])
                del(enemyhit[enum])
                del(efullmove[enum])
                enum=0
                return
John
  • 1
  • 1
    you should use `clock` to display every frame with the same delay - i.e. 25 Frames Per Second - and it will have time to update positions before drawing frame. – furas May 17 '22 at 00:21
  • 1
    you should use spaces after `,` and around `<`, `=`, `==`, `!=`, `+=`, etc. to make code more readable. See more: [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/) – furas May 17 '22 at 00:24
  • 1
    frankly, I don't know how this function work but I don't like `while` in this code - it usually means that code may repeate something too long - and this may slow down code. Maybe all code has to be rebuild to run in different way. But without full code we can't check it. – furas May 17 '22 at 00:29

0 Answers0