0

im making a Pygame game, and I want to move one sprite from the middle to the left, and when it reaches a limit move to the right, and viceversa.

class ColorLineCursor(Generic):
    def __init__(self, position: tuple, surface: pygame.Surface, line_width, *groups: pygame.sprite.Group,
                 z=LAYERS["MAIN"]):
        super().__init__(position, surface, *groups, z=z)

        self.image = surface
        self.rect = self.image.get_rect(center=position)
        self.hitbox = self.rect.copy().inflate(-30, -10)

        self.direction = "right"
        self.origin = position
        self.pathLenght = line_width
        self.leftLimit = self.origin[0] - self.pathLenght / 2
        self.rightLimit = self.origin[0] + self.pathLenght / 2
        self.animationSpeed = 1
        print(f"Origin: {self.origin} Left: {self.leftLimit}, Right: {self.rightLimit} ")

    def animate(self, dt: float):

        if self.direction == "left":
            if self.rect.x <= self.leftLimit:
                self.direction = "right"
            else:
                self.rect.x -= self.animationSpeed * dt
        else:
            if self.rect.x >= self.rightLimit:
                self.direction = "left"
            else:
                self.rect.x += self.animationSpeed * dt

But i get an error trying to update the sprite and moving it exclusively to the right, I tought it was an float addition error, but I made an isolated test when the same values, and it works, so I dont know what Im making wrong.

direction = "left"
origin = 640
x = 640
length = 256
leftlimit = origin - length / 2
rightlimit = origin + length / 2
animationSpeed = 1

while True:

    if direction == "left":
        if x <= leftlimit:
            direction = "right"
        else:
            x -= animationSpeed * 0.002
    else:
        if x >= rightlimit:
            direction = "left"
        else:
            x += animationSpeed * 0.002

   

The code works only if I remove the dt in the "right" movement, but in the "left" movement works fine

  • Pygame Rects have integer values, and because of the truncation or whatever it behaves differently when adding vs subtracting floats less than 1. – Starbuck5 Oct 11 '22 at 05:04
  • You could use a pygame.Vector2 (which uses floats) and then set the Rect to the position of the vector every frame. – Starbuck5 Oct 11 '22 at 05:05
  • I would change `direction` to `1` or `-1` depending on the starting movement. then when it hits the side of the screen, you can use `direction = -direction` to reverse the movement. This gets around the need to add or subtract the movement, it's always `position += speed * direction`. – Kingsley Oct 12 '22 at 02:34
  • A [`pygame.Rect`](https://www.pygame.org/docs/ref/rect.html) object can only store integral data. If you add `0.1` to `rect.x` the position will not change at all. See [Pygame doesn't let me use float for rect.move, but I need it](https://stackoverflow.com/questions/63468413/pygame-doesnt-let-me-use-float-for-rect-move-but-i-need-it/63468637#63468637). – Rabbid76 Oct 14 '22 at 15:08

0 Answers0