I have a bunch of objects at different coordinates, and I need to animate them moving to another coordinate
ie.
one object is at coodinate (234,30) and I need it to go to (310,540).
How do I do this in pygame? I tried messing around with slope and stuff like that but I couldn't get my object,s position to smoothly go there.
Well, you should just change it's coordinates in your game loop.
There's two ways you can do this.
First, for example, if you want your object to move 50px per second, you should send your update function ticks in every frame, and then change your object's x and y coordinates by x + x_speed * ticks / 1000
(here, ticks is in miliseconds, so you should convert it to seconds).
def update_object(ticks):
object.x += float(x_speed) * ticks / 1000 # converting to float is needed
# this is your main game loop
time = pygame.time.Clock()
ticks = 0
while running:
...
update_object(ticks)
ticks = time.tick(30) # this 30 means time.tick() waits enough to make your max framerate 30
Note that you may need to convert object.x
to int while you're blitting(or drawing) it to a surface. With this, your object moves same amount in every second.
Second way is moving your object in every frame. This means your object's speed may change depending on your frame rate(for example, in a crowded scene, your framerate may drop and this makes your objects move slower).
def update_object():
object.x += object_vx
object.y += object.vy
# this is your main game loop
while running:
...
update_object()
BTW, I forgot to mention your object implementation. My preferred way to save coordinates in my objects is by using pygame.Rect's. pygame.Rect has 4 properties, x, y, height, and width. With this you can also hold your object's sprite's length and height.
For example:
character = pygame.Rect(10, 10, 20, 30)
character_image = pygame.image.load(...)
screen.blit(character_image, character) # screen is you surface
And when you want to change your character's coordinates:
character.x += x_amount
character.y += y_amount