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

 

obj_player

 

obj_bullet

 


 

Moving the Player using Code

 

Each object has an x and y variable that stores the coordinates of its position. X is the distance across from the left hand side to the right hand side, and y is the distance down from the top. You can use or change these variables to place objects in certain positions on the screen.

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

 

Obj_Player

 

 

On Create set the speed of the Player

 

 

 

On Keyboard Left

 

Repeat for

Right x+=

Up y-=

Down y+=

 

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

 

 

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>

}

Else

{

< 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

 

 

On Space

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

if (can_shoot = true)

{

// Put the create bullet code here

// Set shooting to false

// Set the alarm to the time between bullets

}

On Alarm

// Set shooting back to true

 

On the bullet Outside of the Room

// Destroy the bullet

instance_destroy();


Adding an Explosion

On obj_bullet add a collision event with obj_Enemy1


Destroy the bullet


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

Create an instance of the Explosion (my explosion sprite is out of order, re-order it)

Add an animation end event to the explosion and destroy the instance.

 

// DON'T FORGET TO COMMENT YOU CODE

 


Adding Score

Using code give the player some score every time they shoot an enemy (Collision Event)

//To add relative we use +=

score += 10;

 

if you use the variable score it will print the score on the top bar.

 

 

 

If you use a different variable for the score then you will need to draw it's value

 

To display the value of a different variable

 

 

Enemy Plane

obj_Enemy

 

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

 

Add an Enemy Plane to the top of the screen.
Using a variable set the speed of the Enemy Plane on Create
Set the vspeed of the Enemy Plane
Add an outside room Event and destroy_instance()

 

Spawning the Enemy

 

Create a obj_controllerEnemy

On Create Set an alarm
On the alarm:

- spawn an enemy at a random location: instance_create(random(room_width), 0,obj_enemy1);

reset the alarm

 

Enemy Bullet

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 Create an instance of obj_enemyBullet
Set an alarm
On the alarm create another instance of obj_enemyBullet and reset the alarm

On the obj_enemyBullet add a collision with the player. Destroy the bullet and create an explosion. We will look at lives and health later.

// DON'T FORGET TO COMMENT YOUR CODE

 

 

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

Create a controller to set global variables.

On the create set lives.

 

Create a controller to display the Lives . On the Draw of the Controller

 

 

 

 

Change values to position sprites where you want them.

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

 

On Collision Subtract a Life and Check if Lives =0

 

 

 

 

Note: If you destroy your player you will have to respawn (you will have to move set lives to a controller as well)

 

if (global.playerLives >0)

{

instance_create(x,y,obj_Player) }

 

 

 

Enemy Fire towards the Plane

 

On obj_Enemy Create

Have the Enemy Bullet travel Towards the Player

 

Now Let's Make Enemy Fire at Random Times

On Create of the Enemy

Generate a random number between 0 and 60 to control firing time

rndFireTime = ________________  // You should know this!

Set the timer

alarm[0] = ___________________  // You again

 

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

Reset alarm[0] to a new rndFireTime

 

Now 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

Explosion

Destroy Enemy

Destroy Player

 

Adding Sound using Code

Add in shooting, explosion sounds to enhance the game

 

sound_play(a_sound)

Add in background sounds

sound_loop(a_sound)

 

 

Giving the Player Health to Survive Collisions

 

 

1.    We want to allow the player to run into a number of enemies before he dies.

 

Start the player with an health level of 100 (or whatever you want) on Create

 

global.playerHealth=100;

 

Every time there is a collision with an enemyBullet subtract from the playerHealth.

 

global.playerHealth-=30;

 

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 (http://cloford.com/resources/colours/500col.htm)

 

 

// set draw color to grey

 draw_set_color(make_color_rgb(150,150,150));

 

 

// 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_set_color(make_color_rgb(0,0,250));

 

 

//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********

draw_sprite(sprite_index, image_index, x,y) ;

 

 

 

Post the following to your website:
.gm81
.exe (zipped)

There is no Rubric for this assignment. It will be evaluated for completeness only.