# Pygame, how can I move a rect/sprite from point A to B smoothly?

As the title suggests, i want to move a rect/sprite from point A to point B in a smooth manner. What I mean more specifically is that the speed of the movement should change throughout the course. So it starts off slowly then speeds up as it reaches the half-way point then slows down again to finally land exactly on B. Or it can just start off fast then to slow down right till it hits B.

I've already tried to do this once by making the acceleration of the object 1/2 of the distance it needed to travel then halving the acceleration again before every frame. Then after a while it just reassigns the objects position to exactly where it needs to be. But i don't really like this method and i thought there had to be a more elegant and efficient way of doing it. But I can't think think of anything.

Here's what I've got so far(It's not the one that 1/2s acceleration):

clock = pygame.time.Clock()

class Block():
def __init__(self, x, y, width, height):
self.x = x
self.y = y

self.rect = pygame.rect.Rect((self.x, self.y, width, height))

def draw(self, surface):
pygame.draw.rect(screen, (55,150,55), self.rect)

self.x = self.rect.left
self.y = self.rect.top

def move(self, vect):
x = vect[0] - self.rect.left
y = vect[1] - self.rect.top

self.rect.move_ip(x, y)

block = Block(100, 100, 50, 50)

def gameloop():

loopExit = True

while loopExit == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loopExit = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_d:
block.move([5,0])

if event.type == pygame.MOUSEBUTTONUP:
block.move(pygame.mouse.get_pos())

screen.fill((25,25,25))

block.draw(screen)

clock.tick(60)

pygame.display.flip()

gameloop()

• What you're looking for is called tweening. It's relatively easy to write yourself, but there's a great, easy-to-use library out there already: pypi.python.org/pypi/PyTweening – Chris Jan 17 '17 at 4:56

As mentioned in a comment, you can use a tweening library, I will not go over that again, but it can be a useful learning experience to try programing those functions yourself (at the very least its good to understand the math).

Part of your problem is that acceleration does not work the way your description seems to think it is.

Position: Where you are