Smoother animation

So far, we have made our ball move once! Let's see if we can get it moving in a more realistic way. When animating things on the screen, it's usually good to move them in small steps, so the motion appears smooth. Let's try moving our ball in smaller steps.

We're not just going to make the steps smaller—we're going to add a loop to move the ball (because we want to make many small steps). Starting with listing 16.13, edit the code so it looks like listing 16.14.

Listing 16.14 Moving a beach ball image smoothly

import pygame, sys


screen = pygame.display.set

mode( [640,480])

screen.fill([255, 255, 255])

my ball = pygame.image.load(

'beach ball.png')

screen.blit(my ball,[x, y])

| Add these lines

-a- ' ^

Uses x and y - (instead of numbers)

pygame.display.flip() for looper in range (1, 100) pygame.time.delay(2 0)

:__value from 2000 to 20


[255,255,255], [x, y, 90,

90], 0)

x = x + 5


screen.blit(my ball, [x,



while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:


If you run this program, you should see the ball moving from its original position over to the right side of the window.

0 0

Post a comment