Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My enemy only moves when my player image is scrolling [VIDEO](https://gyazo.com/85ec003fe8b4b6277d3ae8698e376ee4)

first I made my enemy class 

```
class enemy:
    def __init__(self,x,y,height,width,end):
        self.x = x
        self.y =y
        self.esright = [pygame.image.load("esright1.png"),
        pygame.image.load("esright1.png"),
        pygame.image.load("esright2.png"),
        pygame.image.load("esright3.png"),
        pygame.image.load("esright4.png"),
        pygame.image.load("esright5.png"),
        pygame.image.load("esright6.png"),
        pygame.image.load("esright7.png"),
        pygame.image.load("esright8.png"),
        pygame.image.load("esright9.png"),
        pygame.image.load("esright10.png"),
        pygame.image.load("esright11.png"),
        pygame.image.load("esright12.png"),
        pygame.image.load("esright13.png"),
        pygame.image.load("esright14.png"),
        pygame.image.load("esright15.png"),
        pygame.image.load("esright16.png"),
        pygame.image.load("esright17.png"),
                          ]
        self.esleft = [pygame.image.load("esleft1.png"),
        pygame.image.load("esleft1.png"),
        pygame.image.load("esleft2.png"),
        pygame.image.load("esleft3.png"),
        pygame.image.load("esleft4.png"),
        pygame.image.load("esleft5.png"),
        pygame.image.load("esleft6.png"),
        pygame.image.load("esleft7.png"),
        pygame.image.load("esleft8.png"),
        pygame.image.load("esleft9.png"),
        pygame.image.load("esleft10.png"),
        pygame.image.load("esleft11.png"),
        pygame.image.load("esleft12.png"),
        pygame.image.load("esleft13.png"),
        pygame.image.load("esleft14.png"),
        pygame.image.load("esleft15.png"),
        pygame.image.load("esleft16.png"),
        pygame.image.load("esleft17.png"),
                          ]
        self.esright = [pygame.transform.scale(image,(image.get_width()//3,image.get_height()//3)) for image in self.esright]
        self.esleft = [pygame.transform.scale(image,(image.get_width()//3,image.get_height()//3)) for image in self.esleft]
        self.height = height
        self.width = width
        self.anim_index = 0
        self.distance = 80
        self.speed = 8
        self.vel = 3
        self.path = [x,end]
        self.walking_index = 0
        self.rect = pygame.Rect(x,y,height,width)
 ```

then I made the function for it to move but as you can see on the video it only moves left and right if my player moves 
 ```
    def draw(self,window):
        self.move()
        if self.Walking_index + 1 >= 33:
            self.Walking_index = 0
        if self.vel > 0:
            window.blit(self.esright[self.Walking_index//3], (self.x,self.y))
            self.Walking_index += 1
        else:
            window.blit(self.esleft[self.Walking_index//3], (self.x,self.y))
            self.Walking_index += 1
    def move(self):
        if self.vel > 0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.Walking_index = 0
        else:
            if self.x - self.vel >  self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.Walking_index = 0


 ```
then I defined the class
 ```

black = (0,0,0)
enemys1 = enemy(550,436,50,50,300)
enemys = [enemys1]


                
 ```
and then on my main loop I made it so the enemy doesnt scroll with my screen left and right and up
 ```
runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False

     for enemy in enemys:
         enemy.y += playerman.speed
 ```


 ```
then I drawed my enemy on the main loop the problem is when I move the enemy moves 
 ```
    for enemy in enemys:
        enemy.draw(window)

My full code: its to long to fit in here

[script](https://pastebin.com/raw/q2FvXxBc) - 

It's too long to fit in. This is my attempt to move it but its not working well any help is appreciated! this is the making my player move left and right attempt 


What I have tried:

what I have tried is if I am scrolling left I said for enemys in enemying: enemy.x -= playerman.speed
Posted
Updated 28-May-20 10:55am

1 solution

This part:
Python
for enemy in enemys:
   enemy.y += playerman.speed

changes the y position of the enemy based on the speed of the player. If the player is not moving (speed equals zero), then obviously the enemy won't move either.
You could instead try:
Python
for enemy in enemys:
   enemy.y += enemy.speed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900