How to animate an image II- Wrap around screen

One of the problems with our prior pygame animation is that once the red square goes to far to the left, we don’t do anything. It just seems to be gone forever !

download-bttn
Download Code

What we want to write is the python code that says, “Hey if the image goes all the way to left, off of the screen, then set its x coordinate (ie imageX) back to the total right!)  As shown here;

animation-of-square-looping

The two highlighted lines of code below do exactly that. We discuss those 2 lines at length below.

pygame-code-wrap-image-around

The  get_rect() method is a handy method that pygame supplies us . It allows us to figure out.

 

pygame-get_rect_width-example
get_rect().width

 

pygame-get_rect_x_y_example-diagram
how the get_rect().x and .y works

 

What we want to do is set the imageX value back to width (350) as soon as the image’s  x value is at – get_rect().width

pygame_loop-at-negative-width-example