6.0.1 - Animate

If we want to animate an object that we are going to draw inside the main loop using the pygame template we should:

  • First : create an initialization block of commands outside the Main program loop where we define the starting position (x , y) of the object and the starting speed in each axis:
     
    rect_x = 50    # X starting position of rectangle
    rect_y = 50    # Y starting position of rectangle
    speed_x = 5   # X speed of rectangle
    speed_y = 5   # Y speed of rectangle

  • Second : inside the main loop change the position according to the speed:

    rect_x += speed_x
    rect_y += speed_y

  • Third : Change the drawing command using the defined variables:

    pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50,50])