Discs/Ball – Code Club Gideros 1
The first two sections of the program are mainly to do with setting up and creating useful ‘variables’ that we can
access later if needed.
The first of these two sections creates a ‘variable’ called ‘device’. This lets us know what device the program is
running on. We check this variable for different values and set more variables (pc, android, ios) to ‘true’ to make it
easier to check which device we are on later in the program.
The second of these two sections sets the ‘system’ so that:
The device won’t stop turn off after being left idle.
The background colour is set.
A variable called ‘width’ is set to the screen width.
A variable called ‘height’ is set to the screen height.
A variable called ‘orientation’ is set so we know if the screen is landscape or portrait.
After the first two sections, it’s time to load the graphics. I’ve put all of the disc images into one big image. This
makes it faster to draw because all the images will be near each other in the computers graphic memory.
We load a variable called ‘tDiscs’ with a pointer to the image (called a ‘texture’). Notice that the variable has a t at
the beginning, this makes it easy for us to remember that it’s a texture.
Now we setup a ‘table’ called tBalls. The ‘={}’ is the bit that lets the program know we want tBalls to be a table. A
table contains lots of other variables, we are going to load all of these with pointers to the smaller textures (the
discs) that are contained within the larger texture.
So tBalls[1] is section 1 of a table that points to ‘ShinyBlue.png’ – a blue disc, etc.
Page 1
Discs/Ball – Code Club Gideros 1
By doing this we make it easy to pick any of the 8 different colour discs, we just choose the number 1 to 8. Textures
are like the costumes in scratch.
Now we create a variable that points to a new Sprite layer. A sprite is something this is drawn onto the stage
(another name for the screen). Sprites are similar to the sprites in Scratch.
Now we need some objects. We make a new table variable called ‘balls’ (see ‘balls={}’). We loop 100 times to make
100 objects. The variable ‘loop’ will let us know these we are currently up to.
The ‘balls[loop]={}’ line makes a table within another table, we use these to hold information about each of the 100
balls, eg direction, etc.
The .bitmap=Bitmap line creates an image that looks like one of the discs (it picks randomly from 1 to 8). A bitmap
is just a pointer to a texture.
Setting the ‘AnchorPoint’ to 0.5,0.5 will set the image so that it’s position is the middle. Like the crosshair in Scratch
to set the middle of a costume.
Setting the scale make the image bigger or smaller, in this case we set it to 0.5, so it is half its original size.
We now set the image at a random screen position using the width and height variables we setup earlier.
The repeat..until might seem complicated, but all it does it loop around making sure that that a random direction
that to send the balls does not send them straight up /down or straight left/right.
Each ball that is created is then added to the ‘objects’ sprite.
Once all the balls have been added to the ‘objects’ sprite, then the objects sprite can be added to the ‘stage’ – they
will then appear on screen.
At this point we will run the program and see what happens.
Page 2
Discs/Ball – Code Club Gideros 1
You should see 100 coloured discs/balls on the screen. Now let’s make them move…
This is the bit of the program that will tell the computer how to move the objects.
We make it into a ‘function’, I have called this function ‘gameLoop’.
When gameLoop starts it will create loop that will count from 1 to #balls. #balls contains the size of the balls table
that we created earlier.
New ‘local’ variables called x and y are created to contain the current objects x and y position. A local variable is one
that will get ‘deleted’ once the loop has finished.
I set newX and newY to be the new values of x and y once the direction has been added.
If the newX or newY moves off screen then I multiply the direction by -1, what this does is turn the objects direction
around so it then moves in the opposite direction. Notice that the ‘If … then … else … end’ is very similar to what is
used in Scratch.
If the object doesn’t move off screen then I set the X and Y to the new values.
Now in order for the gameLoop function to do anything it must be ‘called’. To make this happen all the time I setup
what is called an ‘Event listener’, I set it so every time there is a ‘frame’ event then the gameLoop will be called – this
should be about 60 times every second.
In your program you will notice ‘--‘ at the beginning of this line (line 77). Remove it then start the program again.
You should see the balls move.
Let’s change the size of the balls:
Change line 41 so that the 0.5 is 0.75, run the program again. Notice they are bigger. Now make them smaller.
Let’s change the number of balls:
Change line 34 so that the 100 is 200, run the program again. Notice there are lots more balls. If you add too many
balls then the program will slow down as it is drawing too much.
Wouldn’t it be good if that instead of drawing different coloured discs/balls we could draw them in one colour and
the computer could re-colour them for us as a different colour? Ok, let’s now do that…
Page 3
Discs/Ball – Code Club Gideros 1
On line 29, insert this line:
tBall=tDiscs:getTextureRegion("bw.png")
make sure that you type this exactly, then press ‘Enter’ (the objects=Sprite.new() line will now be line 31)
Now change line 38 to be this (add the -- to the beginning):
--balls[loop].bitmap=Bitmap.new(tBalls[math.random(1,8)]) -- random coloured ball
Now change line 39 to be this:
balls[loop].bitmap=Bitmap.new(tBall) -- black and white ball
Now run the program again, you will see loads of grey balls on the screen.
Now change line 40 to be this:
balls[loop].bitmap:setColorTransform(math.random(),math.random(),math.random())
Then press ‘Enter’ so the line setting the AnchorPoint is on line 42.
Now run the program again, you will see loads of coloured balls on the screen.
Next code club we will add a listener for the screen being clicked, detecting which ball has been touched and making
that ball disappear.
Page 4
© Copyright 2026 Paperzz