GameMaker Tutorial: Space Rocks (#1)


I’m starting to learn GameMaker, because I want to create my own video games in the future.

The first tutorial is “Space Rocks”, which I will refer to as Asteroid Chaser (don’t know if “Space Rocks” is actually the name of the tutorial).

Links to the tutorials:

I enjoyed this first GameMaker experience. Following the tutorial was simple, and I was also able to easily debug any problems I encountered along the way.

Before I show you the Preview of my work, let me tell you about some changes I’ve made to the tutorial itself.

Customizations

I will now share with you some of my customization to the tutorials, so you can pick and choose your favorites.

Player Moves Backwards

Originally there was only a way to move forward, and not backwards. I wanted the option of doing so by holding the Down button.

You can do the same by inserting the following code in the Step event of obj_player:

if keyboard_check(vk_down) // If Down is held
{
	motion_add(image_angle, -0.1); // Player moves backwards
}

Better Player Wrapping

The tutorial uses player wrapping in a slightly weird way. Originally the player wrapped around the Room (the Game) after 0 pixels outside it. I found it to be slightly fast for my taste, so I changed it to after 32 pixels (half of the player’s horizontal sprite size).

You can do the same by changing the third argument value to 32 in the move_wrap() function:

move_wrap(true, true, 32); // If Player is outside the Room, wrap the object to the opposite side after 32 pixels

More Variables Setup for Future Use

The tutorial only has the “points” variable used for the score, and the “powerup_time” variable used after you destroy rocks for 20 seconds.

I created two more variables:

  • “destroyed”: Every time a rock is destroyed, the destroyed rock counter goes up by one (small rocks included).
  • “bullet”: Every time you shoot, the bullet counter goes up by one (also works for the spread-shot power up).

You can do the same by going to the Create event of obj_game, and adding the following code:

destroyed = 0; // Set amount of rocks destroyed to 0 when starting or restarting the Room
bullet = 0; // Set player's bullet counter to 0 when starting or restarting the Room

Unfortunately it still won’t show up, because it’s not drawn on the screen!

To do that, go into the Draw GUI event for obj_game, and insert the following code:

draw_text(10, 60, destroyed); // Render number of rocks destroyed
draw_text(10, 110, bullet); // Render number of player bullets

The counters will now show up in the Room, however, they will not increment when you destroy a rock.

For the “destroyed” counter, add the following code (Highlighted in Red), in the collision event with obj_bullet of obj_rock:

if sprite_index == spr_rock_big // If a big rock is destroyed
{
	obj_game.points += 50; // Add 50 points
	obj_game.destroyed += 1; // Add 1 to destroyed rock counter
}
else // If a small rock is destroyed
{
	obj_game.destroyed += 1; // Still add 1 to destroyed rock counter
}

Now every time you destroy a rock (big or small), the counter will go up by one.

The “bullet” counter will also show up in the Room, but it will not increment yet even if you shoot.

To do this, add the following code (Highlighted in Red), in the Step event of obj_player:

if mouse_check_button_pressed(mb_left) // If the left mouse-button is pressed
{
	instance_create_layer(x, y, "Instances", obj_bullet); // Create Bullet Object
	audio_play_sound(snd_shoot, 0, false, 1, 0, random_range(0.8, 1.2)); // Play bullet sound effect
	obj_game.bullet += 1; // Add 1 to bullet counter
	if (powerup == 1) // If player's power up state is 1
	{
		var _bullet = instance_create_layer(x, y, "Instances", obj_bullet); // Create a bullet object
		_bullet.direction += 10; // Give that bullet object a +10 degree angle compared to the player
		_bullet = instance_create_layer(x, y, "Instances", obj_bullet); // Create another bullet object
		_bullet.direction -= 10; // Give that bullet object a -10 degree angle compared to the player
		obj_game.bullet += 2 // Add 2 to bullet counter
	}
}

Now if you shoot with the spread-shot power up, the “bullet” counter add 2 + 1 with the left mouse button.

Preview

You can see all of my customizations (and back then, unsolved bugs) in the following video. I will cover more of my changes in a following blog post.

Stay tuned to see more changes.

Let me know in the comments if you have any questions, and if you enjoyed some of my customizations.


2 responses to “GameMaker Tutorial: Space Rocks (#1)”

  1. I’m glad to see you’re enjoying GameMaker so far. I will keep an eye on your blog to read all about your next adventures. Have fun coding!

Leave a Reply

Your email address will not be published. Required fields are marked *