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 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;


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


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




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