# Pygame Scrolling Bug With Enemys

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

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!

• 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? – Habib Ismail May 28 at 0:42
• 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 – Habib Ismail May 28 at 1:29
• 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 – sciencepi May 28 at 8:06
• 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? – Habib Ismail May 28 at 18:13
• 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. – sciencepi May 28 at 20:46