Habib Ismail Ответов: 1

Мои враги двигаются только тогда, когда прокручивается изображение моего игрока - как это исправить? Pygame


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 


Что я уже пробовал:

то, что я пробовал, если я прокручиваю влево, я сказал для enemys в enemying: enemy.x -= playerman.speed

1 Ответов

Рейтинг:
2

phil.o

Эта часть:

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

изменяет y-позицию противника в зависимости от скорости игрока. Если игрок не движется (скорость равна нулю), то очевидно, что и противник не будет двигаться.
Вы могли бы вместо этого попробовать:
for enemy in enemys:
   enemy.y += enemy.speed