1 Timing
Tim Young edited this page 2017-10-02 00:30:52 +02:00

The timing system

SpriteLibrary needs to erase and draw the sprites every time a sprite moves or animates. So SpriteLibrary creates a timer that runs code many times a second. It checks to see if anything needs to be erased, moved, or re-drawn, and it does all that for you.

If you have code that takes too long to run, it can have strange effects on your Sprites. They need to be updated semi-regularly to animate properly. And, they need to check for collisions and all sorts of things. In short, try not to run things that pause the program, or take a long time to process. Seriously. Stay away from commands like Thread.Sleep.

There is a lot more about this in the In Depth portion on time.

How the graphics works

The sprite controller keeps track of the individual rectangles that need to be re-drawn on the screen. It draws the sprites on the background image, and then tells the PictureBox to invalidate the areas of the background image which had been changed. Redrawing the entire PictureBox is a time-consuming thing, which is why we try to only re-draw the changed portions.

The process starts by “erasing” the areas where the sprites are. It retains a copy of the background image, and copies the portions of that image to the background, thus overwriting the sprite with the original background. Then the sprites move to their new location, and re-draw themselves on the background. Finally, both the location where the sprite was, and the location where the sprite was moved to, are both invalidated.