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.
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.
A few variables are declared at the start of the program image, sprites, speed and direction.
image will be used to store an ID number for an image that will get loaded and used with the sprites moving up and down the screen.
sprites is an array containing 9 integers. It will store ID numbers for the sprites that we create.
speed is also an array containing 9 integers. This array will store values that control how fast the sprites move up and down the screen.
Finally direction is declared and is also an array storing 9 integers. This array stores a value that determines which direction the sprites move in.
After the variable declarations an image named "penguin.png" is loaded. This will be used by all of the sprites.
The next part is a for loop that creates 9 sprites.
Next we're onto the main loop of the program that controls how our sprites are moved.
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.
A sprite gets created. Its identifier or ID number is stored within the array sprites. This number is required by many other commands. The sprite is told to use the image loaded earlier (penguin.png).
The position of the sprite is set by calling the command SetSpritePosition. The ID number of the sprite we're working with is passed in, followed by the X and Y positions. This will initially position all of the sprites in a row.
All the values inside direction are set to 0. Later on inside the main loop if this value is 0 the sprites move up the screen. When it is set to 1 the sprites move down the screen.
speed is set to the negative value of i. When this for loop has finished the values this array contains will be -1, -2, -3, -4, -5, -6, -7, -8 and -9.
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.
Initially the X and Y positions of our sprites are stored in the variables x and y. This data is needed multiple times throughout the loop, so instead of repeatedly calling the functions to extract the data, it is simply assigned to these variables, resulting in our program having less work to do.
The if statement checks whether either of the conditions are valid. If the direction is set to 0 and the Y position is less than 0 or whether the direction is set to 1 and the Y position is greater than 768 minus the size of the image. In order words this is checking whether the sprite is at the top or bottom of the screen.
If either of the conditions are true then the speed for the sprite is set to the negative value of its speed. If the speed was set to 5, then it would switch to -5. The direction is set using the not operator, allowing us to alternate between 0 and 1. If the direction is 0 it becomes 1. If it's 1 it gets set to 0. All this has the effect of letting us continually tick tock our values letting us move the sprites up and down.
Next there's a line that takes the current Y position and add the current speed to it. The speed is either negative or positive, allowing us to move the sprite up or down.
The final line updates the position of the sprite based on our alterations to its position. Nothing actually happens with the X position, but we are moving the Y position.
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.