0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

The issue is caused by how you draw the enemies. When you draw an object, you are using something like: screen.blit(image, pos) however, when you move, that pos variable does not update to compensate for the moving plane of view. Instead, do something like (this is pseudocode, so it wont work):

def onscroll(enemies_list, delta_scroll): # the delta_scroll variable is the change in 
#the screens location (positive or negative)
    for x in enemies_list:
        x.position = (x.position[0] - delta_scroll, x.position[1])

also, if you have a return position, you want to also compensate using:

x.return_pos -= delta_scroll

Hope this helps!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ wait I make a new def function? and add for the x.return_pos -= delta_scroll I put it on my scrolling left right up? \$\endgroup\$ – Habib Ismail May 28 at 0:42
  • \$\begingroup\$ I placed the scroll function in my enemy class under it and then called the x.return_pos -= delta_scroll on my Camera Movement For left and Right but I feel like I am doing something wrong I am getting this erroridk what I am doing wrong \$\endgroup\$ – Habib Ismail May 28 at 1:29
  • \$\begingroup\$ no, what I am saying is when, in your code you are moving the camera, you need to call this function for all enemies. This should move them to their correct positions in the camera’s view field \$\endgroup\$ – sciencepi May 28 at 8:06
  • \$\begingroup\$ I am sorry if I am asking to much questions but I am fairly new to pygame could you show me how I could apply that to my code? \$\endgroup\$ – Habib Ismail May 28 at 18:13
  • 1
    \$\begingroup\$ yes, after checking all the code I realized there was an issue - use this code here and it should work (replace contents of the onscroll function with this): for on in enemy_list: on.y += delta_scroll. This code should 100% work. \$\endgroup\$ – sciencepi May 28 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.