Skip to content Skip to sidebar Skip to footer

Enemy Not Staying At Original Spot

So I been trying make my enemy move at the same spot while my player moves the camera but it's not working, when the screen moves my enemy will slowly move from its original spot b

Solution 1:

Improve the robustness of your code. Make sure that the movement of a Snake works even if the position of the snake is far out of the path. If proposed_move is less than self.path[0]-self.vel or self.path[1]+self.vel, then the Snake will never return to the path, because self.vel will be inverted continuously. Make sure that the direction of movement is always set to the right if proposed_move < self.path[0] and make sure that the direction of movement is always set to the left if proposed_move > self.path[1]:

class Snake:
    # [...]

    def move(self):
        if self.visible:
            # turn around if the move would go out-of-bounds
            proposed_move = self.x + self.vel

            # Move hits a boundary, so we need to turn around
            if proposed_move < self.path[0]:
                self.vel = abs(self.vel)
                self.Walking_index = 0
            if proposed_move > self.path[1]:
                self.vel = -abs(self.vel)
                self.Walking_index = 0

            # now make the correct move
            self.x += self.vel   # add +/- velocity

Do the same for Bat:

class Bat:
    # [...]

    def move(self):
        if self.visible:
            # relizes when it hits bounds
            proposed_move = self.x + self.vel
            if proposed_move < self.path[0]:
                self.vel = abs(self.vel)
                self.Walking_index = 0
            if proposed_move > self.path[1]:
                self.vel = -abs(self.vel)
                self.Walking_index = 0
                # starting movment
            self.x += self.vel

You have to simplify your code.

Store the initial position of the player (start_x, start_y) and a to methods to the class player that return the current position of the player in relation to the start position (get_x, get_y):

class Player:
    def __init__(self,x,y,width,height,color):
        self.start_x = x
        self.start_y = y
        self.x = x
        self.y = y
        #[...]

    def get_x(self):
        return self.x - self.start_x
    def get_y(self):
        return self.y - self.start_y

Compute the positions of Snake, Bat, Platform and Rule in relation to the player:

class Snake:
    # [...]

    def draw(self):
        self.move()
        self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())
        window.blit(self.no,self.rect)
class Bat:
    # [...]

    def draw(self):
        self.move()
        self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())
        window.blit(self.no, self.rect)
class Platform:
    # [...]

    def get_rect(self):
        self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())
        return self.rect
    def draw(self):
        pygame.draw.rect(window,self.color,self.get_rect())
class Rule:
    # [...]

    def draw(self):
        self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())

Get rid of player movement compensation (scroll etc.). This is no longer necessary as the objects are drawn in relation to the player. Complete main loop:

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
 
    # lets player move
    keys = pygame.key.get_pressed()
    
    if playerman.fall > 0 and keys[pygame.K_SPACE]:
        if keys[pygame.K_d]:
            playerman.direction = "jump1"
    else:
        if playerman.direction == "left":
            if keys[pygame.K_SPACE]:
                playerman.direction = "jump2"
 
    # direction for player animation and screen movment x
    if keys[pygame.K_d]:
        if not playerman.isJump:
            playerman.direction = "right"
    elif keys[pygame.K_a]:
        if not playerman.isJump:
            playerman.direction = "left"
    
    if playerman.direction == "right" and keys[pygame.K_SPACE]:
        playerman.direction = "jump1"
    if playerman.direction == "left" and keys[pygame.K_SPACE]:
        playerman.direction = "jump2"
 
    px, py = playerman.x, playerman.y

    # sides for player and player screen movment
    platform_rect_list = [p.rect for p in platforms]
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)
 
    playerman.y = py
    if keys[pygame.K_d]:
        playerman.x += playerman.speed
    if keys[pygame.K_a]:
        playerman.x -= playerman.speed
    if player_rect.collidelist(platform_rect_list) > 0:
        playerman.x = px      
 
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False
 
        # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            player_rect = playerman.get_rect()
            player_rect.topleft = (playerman.x, playerman.y)   
            platform_rect = Platform.get_rect()
            if playerman.get_rect().colliderect(platform_rect):
                collide = True
                playerman.isJump = False
                if platform_rect.top > playerman.x:
                    playerman.y = platform_rect.top - playerman.height
                if player_rect.right > platform_rect.left and player_rect.left < platform_rect.left:
                    playerman.x = platform_rect.left - playerman.width
                if player_rect.left < platform_rect.right and player_rect.right > platform_rect.right:
                    playerman.x = platform_rect.right
 
            # colliding with floor      
            if player_rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height
 
        # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
                playerman.y -= playerman.speed
            playerman.fall = 0
 
 
    # Jump Count
 
    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10
 
    redrawwindow()
    if playerman.rect.colliderect(rule1.rect):
        window.blit(move,(-40,-100))
 
    pygame.display.update()

Post a Comment for "Enemy Not Staying At Original Spot"