An Introduction to using Code

Scrolling Backgrounds


First we will create a background image that looks like simple stars.

  • Create a new background.
  • Call it “bgr_Stars1”.
  • Click the “Edit Background” button.
  • Select the menu choice “Transform -> Resize Canvas”.
  • Make the background image 200 x 200 pixels.
  • Fill with black.
  • Select the white color and click on the pencil.
  • Use it to click a few stars scattered randomly around the image.
  • Do not draw too many, 5 or so looks good.

Open the Room Window

  • Click the “Backgrounds” tab
  • Select “Background 0” to “bgr_Stars1”.
  • “Visible” “Tile Hor” and “Tile Vert” should be checked.
  • Uncheck “Draw background color”.
  • OK. We are done here for now. Click OK, save the game and test it.
  • If everything is as it should, there should now be some stars as a background
  • to the game. It looks rather boring though.

Let’s add some movement to the stars.

  • Open the Room window and click on the “Background” tab again.
  • Far down in the tab you will find a textbox marked “Vert. speed”.
  • It is currently set to 0. Set it to 3. Click OK, save and test the game again.

Adding Parallax

Time to add some depth to the background.


  • Create a new background image, call it “bgr_Stars2”
  • Resize it to 200 x 200 pixels.
  • Paint it black and add some stars to it. But this time the color of the stars should be light gray, and not completely white. Add a few less stars to this background. Three, maybe.
  • Then open the room window again and click the Background tab. Select
  • “Background 1” and select the background image “bgr_Stars2” for that background.
  •  “Vert. speed” of “2”.
  • Close the window and try the game. It still looks like a flat starfield. That is because we have forgotten to make the new starfield transparent.
  • Edit “bgr_Stars2”, and click "Edit Background",
    Select "Fill an area", make sure "Opacity on the right is set to 0 and "Replace" is
    selected instead of "Blend", now click on the black colour on the background to fill it
    Then save and try the game again. Alright! Now there are two starfields
    moving at different speeds, creating a small illusion of depth.
  • To make it even better, we will add a third starfield layer, just like the others called “bgr_Stars3”, use a darker gray colour for the stars in it. You can add a few more stars too, maybe 10 or so. Then
    make it Transparent and open up the room. Add the new starfield background as "Background 2” and set the “Vert. speed” to 1.
    Test the game.


Create the Player and the Bullet Objects






Moving the Player using Code


A review of co-ordinates



Moving to the Right - X increases in value
Moving to the Left - X decreases in value
Moving Up - Y decreases in value
Moving Down - Y increases in value






On Create set the speed of the Player




On Keyboard Left



Repeat for other Keys


Right x+=

Up y-=

Down y+=


Now use if statements to stop the plane from leaving the room




Repeat for other directions



Let's Shoot



On Create of the Player set the Speed of the Bullet



On Spacebar of the Player




If your bullet does not shoot from the right spot remember to center your sprites.


Fixing Rapid Fire




The “If” Statement

if (Some condition)


< one or more statements>




< one or more statements>



You absolutely must indent if statements correctly!  I will not look at any code that is not indented.


Note:  You do not always need an else

Use the ideas from the previous assignment to complete the steps below using code

On Player Create

// Create a variable can_shoot and set it to true

canShoot = true;



On Space

// Add a variable check (using code) before the bullet is created

if (canShoot= true)


// Put the create bullet code here

// Set shooting to false

canShoot = false;

// Set the alarm to the time between bullets

alarm[0] = 25;


On Alarm

// Set shooting back to true

canShoot = true;


On the bullet Outside of the Room

// Destroy the bullet



Create your Enemy Object and add it to the Room



Note: Rotate the image to make it come down from the top


Add an Enemy Plane to the top of the screen.

On Create
//Using a variable set the speed of the Enemy Plane

enemySpeed = 7;

// Set the vspeed of the Enemy Plane
obj_Enemy.vspeed = enemySpeed;

Spawn the Enemy back at the top when it leaves the room

On a Step Event

Adding an Explosion when the Enemy is Hit

On obj_bullet add a collision event with obj_Enemy1

// Destroy the bullet
on the collision event


// Destroy the plane
on the collision event

Note: This is New use: with(other)instance_destroy();

Create an Explosion sprite and object make sure to center it.
My strip is out of order so re-order it

// Create an instance of the Explosion on the collision event

Add an Animation End Event on the Explosion

// Destroy the instance




Adding Score when the Enemy is Hit



Create a controller to initialize global variables


On Create Event


// Initialize the score to zero


//we use global if we are using the variable in more than one object

global.playerScore = 0;




On the Player add a Collision Event with the Enemy Bullet

// Give the player score every time they shoot an enemy
//To add relative we use +=

global.playerScore += 10;




Create a Controller to Draw the score


On the Draw Event


draw_set_color(c_white); // Set Text Colour
draw_text(100, 100, "Score"); // Print Title
draw_text_color(100, 200, string(global.playerScore), c_lime, c_lime, c_green, c_green, 1);

// The above code will draw two sections of text with the first text being drawn white (as that is the base drawing colour) and the second text being drawn with a lime green to normal green gradient.



Now change the values to modify colours and place it in the correct position





Spawning Enemies


Create obj_spawnEnemies


On Create of obj_spawnEnemies


//Set an alarm


On Alarm
of obj_spawnEnemies


// Calculate rndx using the random formula


//Create an instance of the enemy at the random location



//reset the alarm

If you have too many enemies, remove the respawn on the exit room

Creating Enemy Bullets

Create a new Bullet. Call it obj_enemyBullet

// On create set the speed of the bullet
// Outside the Room destroy the bullet

On the Enemy Plane

//On Create create an instance of obj_enemyBullet
//On Create set an alarm

On the alarm

//Create another instance of obj_enemyBullet and reset the alarm

On obj_enemyBullet

//add a collision with the player. Destroy the bullet and create an explosion.




EnemyBullet Destroys the Player


You should be able to do this now. Follow the same steps as when the playerBullet destroyed the Enemy



Give the Player Lives

On obj_setvariables

On Create


On the Draw


// Draw the Lives

(this code is a little confusing, don't worry if you don't completely understand it)


for (i=0; i<global.playerLives; i+=1)


draw_sprite(spr_life, 0, 140 +(30*i), 25) // Note: 140 is the x value, 25 is the y values, 30 is the spacing






On Collision with the bullet

// Subtract one life when the Player is Hit

global.playerLives -=1;


Check for no more lives in a step


if (global.playerLives = 0)


// Show the High Score Table

// End the game



Enemy Fire towards the Plane


On obj_Enemy Create

Have the Enemy Bullet travel Towards the Player


Make Enemy Fire at Random Times

On Create of the Enemy

//Generate a random number between 1 and 60 to control firing time

rndFireTime = floor(random(60) + 1// Change values to make it work for you

//Set the timer

alarm[0] = rndFireTime


On Alarm


Move the code that creates and fires the Bullet to alarm[0]


//Reset alarm[0] to a new rndFireTime


Make the speed of the bullets random

//Use a rndBulletSpeed Variable


Don’t Forget to destroy the bullets when they leave the room!!!

If you get an error trying to create bullets when your player is dead try adding the following to your bullet create code so that a bullet is only created when the player has lives


Destroy Player and Enemy when they Collide


Ok …….. This you should be able to do!


Collision Event


//Destroy Enemy

//Destroy Player

// Respawn the player at the original location



Adding Sound using Code

// To play a sound



// To play a background song




Giving the Player Health to Survive Collisions




On obj_setvariables Create


// Set the players health

On Collision with Enemy Bullet

// Subtract health relative




Only destroy the player and subtract a life if the playerHealth is < 0


if (global.playerHealth < 0)



// subtract a life
//Destroy player

// Reset the health to 100




Now ……….. remove health when the player is hit by the enemy.  You decide how much.



Drawing the Health Bar using Code



First we draw the gray background rectangle, and then we draw the blue

health rectangle on top of it.

On Draw Function in the Player

Set the colour to draw with using RGB values (



// set draw color to grey




// draw the grey rectangle draw_rectangle (x1,y1,x2,y2, false)


draw_rectangle(5,400,105,410, false) // difference between 1st number and third number is 100


// set the color for the energy bar this is blue




//draw the energy bar

draw_rectangle(5,400,global.playerHealth + 5 ,410, false); 



//Now draw the green box using draw_set_color and draw_rectangle



// ************Only add this line if you get a drawing error or your plane disappears ********

draw_sprite(sprite_index, image_index, x,y) ;



Post the following to your website:

.gm81 folder
.exe folder
obj and sprites screenshot
gameplay video

self evaluation (embedded)