Arrays in action

This example demonstrates how the use of arrays can make a program's data far easier to work with. It creates nine sprites and moves them up and down the screen. Information about the sprite, its speed and direction are all stored within arrays. This allows us to store a lot of data within a few variables, which is much better than having lots of unique variables and needing to replicate code to deal with the same data.

This program relies on some external media. The easiest way of running this program is to click here and download the project. Once the file has been downloaded, extract the files and open MyFirstAGKProject.agk within AppGameKit. Now run the program to see what it does.

image-1

The purpose of this program is to give you some kind of idea about how using arrays can be more beneficial in certain circumstances, so don't worry too much about anything you don't totally understand.

Take a look at the code.

SetVirtualResolution ( 1024, 768 )

image as integer sprites as integer [ 9 ] speed as integer [ 9 ] direction as integer [ 9 ]
image = LoadImage ( "penguin.png" )
for i = 1 to 9 sprites [ i ] = CreateSprite ( image ) SetSpritePosition ( sprites [ i ], GetImageWidth ( image ) * ( i - 1 ), 768 - GetImageHeight ( image ) )
direction [ i ] = 0 speed [ i ] = -i next i
do for i = 1 to 9 x = GetSpriteX ( sprites [ i ] ) y = GetSpriteY ( sprites [ i ] )
if ( direction [ i ] = 0 and y < 0 ) or ( direction [ i ] = 1 and y > 768 - GetImageHeight ( image ) ) speed [ i ] = -speed [ i ] direction [ i ] = not direction [ i ] endif
y = y + speed [ i ]
SetSpritePosition ( sprites [ i ], x, y ) next i
sync ( ) loop

We are able to control nine sprites on screen without using a load of variables, instead just three arrays contain all the data we need.

Let's take a brief overview of what the code is doing.

The two main parts that need explaining in more detail are the for loop that creates the sprites and the for loop inside the do loop. Let's begin with the first for loop.

for i = 1 to 9
	sprites [ i ] = CreateSprite ( image )
	SetSpritePosition ( sprites [ i ], GetImageWidth ( image ) * ( i - 1 ), 768 - GetImageHeight ( image ) )
	
direction [ i ] = 0 speed [ i ] = -i next i

This loop will cycle through nine times, performing the following tasks.

The main loop of the program controls the movement of the sprites.

for i = 1 to 9
	x = GetSpriteX ( sprites [ i ] )
	y = GetSpriteY ( sprites [ i ] )
	
if ( direction [ i ] = 0 and y < 0 ) or ( direction [ i ] = 1 and y > 768 - GetImageHeight ( image ) ) speed [ i ] = -speed [ i ] direction [ i ] = not direction [ i ] endif
y = y + speed [ i ]
SetSpritePosition ( sprites [ i ], x, y ) next i

The for loop cycles through all of our nine sprites. It does the following.

Imagine if this program wasn't using arrays. We would need 27 separate variables to store all this information, which would be very messy and unpleasant to work with. Instead we have a much more elegant solution by using arrays.