1
votes

I have a problem with my enemy where when my player scrolls left the enemy scrolls with the player is there anyway to fix this even Video my full code is below its to long to fit here though

what I was trying to do is make my enemy not to change its position when my window is scrolling its doing the same for right and left keeps following the player


while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
  [...]
# I did the same for my players key movement  this part suppose to not make my player scroll
if playerman.y < 250:
        playerman.y += 1
        for platform in platforms:
            platform.y += playerman.speed
        for enemys in enemying:
            enemys.y += playerman.speed

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)
       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


where I defined the enemy class

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

my full code its to long to fit here script

1
What is your question? Please create a minimal reproducible example. - mkrieger1
Instead of self.esleft = [pygame.image.load("esleft1.png"), pygame.image.load("esleft1.png"), ...] you could do self.esleft = [pygame.image.load("esleft%d.png") for i in range(1, 18)] (though it doesn't include the duplicated _1.png) - Cireo
In the video you show it looks like things are working as expected, you can see the enemy is walking to the right but moves at roughly the same rate as the screen, so doesn't move. - Cireo
if you watch it fully when my player moves right it moves right doesnt the same for left - Habib Ismail

1 Answers

2
votes

It is not sufficient to scroll the position of the enemy. The path of the enemy is limited by an interval (self.path). When the x coordinate of the enemy reaches self.path[0] or self.path[1], then the enemy turns:

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

If you scroll the enemy, the you have to scroll the path, too.

Add a method scroll to the class enemys. Change the position and the start and end of the path:

class enemys:
    # [...]

    def scroll(self, sx, sy):
        self.x += sx
        self.y += sy
        self.path[0] += sx
        self.path[1] += sx

Use the method to scroll the enemy:

runninggame = True
while runninggame:
    # [...]

    if playerman.y < 250:
        playerman.y += 1

        # [...]
        for enemys in enemying:
            enemys.scroll(0, playerman.speed)

    if playerman.y > 450:
        playerman.y -= playerman.fall

        # [...]
        for enemys in enemying:
            enemys.scroll(0, -playerman.fall)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        playerman.x -= playerman.speed
        playerman.direction = "left"
        # camera controll for left
        if playerman.x < 100:

            # [...]
            for enemys in enemying:
                enemys.scroll(playerman.speed, 0)

    elif keys[pygame.K_d]:
        playerman.x += playerman.speed
        playerman.direction = "right"     
        # camera for right:
        if playerman.x > 400:
            playerman.x -= playerman.speed

            # [...]
            for enemys in enemying:
                enemys.scroll(-playerman.speed, 0)