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

pygame.init()

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

pygame.draw.rect(screen,

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

90], 0)

x = x + 5

r

screen.blit(my ball, [x,

y])

pygame.display.flip()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

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

100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook


Post a comment