Drawing sprites with for loops

Essentially a for loop is used to repeat a series of instructions for a certain amount of times. This makes it incredibly useful for all kinds of tasks. The next example shows how a for loop can be used to draw lots of sprites on the screen with a few instructions.

SetVirtualResolution ( 1024, 768 )
SetSyncRate ( 0, 0 )
EnableClearColor ( 0 )

CreateImageColor ( 1, 255, 255, 255, 255 ) CreateSprite ( 1, 1 ) SetSpriteSize ( 1, 1, 1 )
do for i = 1 to 2500 SetSpritePosition ( 1, random ( 0, 1024 ), random ( 0, 768 ) ) SetSpriteColor ( 1, random ( 0, 255 ), random ( 0, 255 ), random ( 0, 255 ), 255 ) DrawSprite ( 1 ) next i
sync ( ) loop

Before we figure out what's happening in the code try running the program to see what it does.

image-1

By repeating a series of small instructions over and over we're able to create some interesting results on screen using a small amount of code. The for loop being used declares a variable i, assigns it a value of 1 and sets the end point to be 2500, meaning our loop is going to perform the tasks inside of it 2500 times before returning control to the next part of our program. The instructions within the for loop are used to control the position of a sprite, give it a random colour and then draw it. The end result is that we have 2500 sprites being drawn on screen.