Tag Archive for 'Tutorial'

How to make a bullet hell shmup in Game Maker Chapter 2: Player Movement

>> Download the project used in this tutorial here <<

Introduction

Though this tutorial is mainly for beginner, you are expected to have some basic knowledge of Game Maker.  I’d recommend you to read Derek Yu’s Game Maker tutorial for beginner first. Follow his great tutorial up to chapter 2 or 3 should get you ready.

Making your character

Last time we manage to able to create our main game GUI. Now, it’s time that we put our character that represents our player into the game field.

First of all, we need to find a graphic to represent the player character. This can be anything, really. But in this tutorial, I’ll use this sprite as our player representation.

Pixel Ship by Squirrelsquid

Though this is a gif file and don’t really need to add it externally to make the transparency work, we’ll just do that to keep the consistency here. Feel free to do whatever you like though: D

Ok, first of all go to your game folder again. Inside the dat/img folder, create a new folder call ‘player’. This will be used to store any graphics related to the player ship. Put a graphic that you’d like to represent the player inside that folder.

Then, go to our load_graphics script, and add these lines.

//LOAD PLAYER

global.spr_player = sprite_add_alpha(“dat/img/player/paxship_01.gif”,4,false,false,16,16);

Alternatively, if you don’t want to type all those global prefix every time you refer to a sprite, you can also do the following:

  • Create a new sprite in GM, make it blank and untick the transparent option.
  • Name it sprPlayer
  • Then use this script instead of the above one
//LOAD PLAYER

sprite_replace_alpha(sprPlayer,”dat/img/player/paxship_01.gif”,4,false,false,16,16);

The result of both methods is the same, loading a graphic and stores it to some variable. The first method store it in global.spr_player while the second method replace sprPlayer sprite’s graphic with the one you load. If you want a auto highlight in the script editor, the second one should be a better way to go. In this tutorial, I’ll go with the second one anyway: P

Next step is to create a player object. We’ll call this object objPlayer.  Now, assign a sprite to this object.  You can do this by clicking on the icon on the right in the sprite panel.  Once clicked, you should see a dropdown list and you can choose sprPlayer from there.

If you assign the sprite to global.sprPlayer, apparently this is not going to work. You can assign the sprite by adding a creation event. And inside that event you can add an action Execute A Piece Of Code which is in the control tab and is the first icon in the Code group. Drag it and drop it into your action box, double click on it to add some script.

//Assign sprite to objPlayer

sprite_index = global.sprPlayer

Of course, even if you use the second method, you can still do this and no need to mess with the dropdown box. Just replace global.sprPlayer with sprPlayer and it will work the same way. Basically what we are doing here is telling this object which sprite it’s going to use. Easy, eh? :D

Now, go to the shmup_room and place the objPlayer somewhere on the screen. Then you can run the game and see how our lovely sprite works.

If you’re using the sprite I’m using, you’ll see that the sprite animated WAYYY too fast. Go back to your creation event and add these lines to your script.

//Change the animation speed

image_speed = 0.4

Of course, you can adjust this to fix your need. Negative number will reverse your animation, however.

Move your character

The player character won’t do any good if we cannot control them. Now, it’s time to work on more codings!

First of all, we need to initialize all the variables that the player need to have for the movement part. In order to do this, we go back to objPlayer’s creation event and add these lines to the script ( or add a new piece of script if you don’t already have one)

//Player parameter initialization

normal_speed = 5

focus_speed  = 2

is_focus     = false

is_move_vert = false

is_move_hori = false

move_speed   = normal_speed

Here are some explanations of each variable.

  • Normal_speed and focus _speed should be self-explanatory. The speed that we will be using will be move_speed, but move_speed will refer to normal_speed or focus_speed, depending on the player’s state.
  • Is_focus is used to determine whether the player is now in the focus state or not. By focusing, I mean the state of holding SHIFT button in Touhou to slow down the movement.
  • Is_move_vert and is_move_hori is used to tell the program whether we are moving horizontally or vertically. This will also be used to calculate the movement later on.

Next step is to code the movement control part. Since this code will be called every frame, it is only logical to put it somewhere that will be called every frame, and that somewhere is inside our Step event.

So, create a step event in objPlayer and add Execute A Piece of Code action to it. Add these lines of script to let your ship movable!

//Check and control player movement

is_move_vert = false

is_move_hori = false

dx = 0

dy = 0

divider = 1.0;

//Check which speed to use

if(keyboard_check(vk_shift))

{ is_focus = true;

}else

{ is_focus = false;

}

if(is_focus == true)

{ move_speed = focus_speed

}

else

{

move_speed = normal_speed

}

//Check differences in x and y coordinate according to key pressed.

//Check differences in x and y coordinate according to key pressed.

if (keyboard_check(vk_up))

{

dy = -move_speed;

}

if (keyboard_check(vk_down))

{

dy = move_speed;

}

if (keyboard_check(vk_left))

{

dx= -move_speed;

}

if (keyboard_check(vk_right))

{

dx = move_speed;

}

//Check movement orientation

if (keyboard_check(vk_up) or keyboard_check(vk_down))

{

is_move_vert = true;

}

if (keyboard_check(vk_left) or keyboard_check(vk_right))

{

is_move_hori = true;

}

//If move diagonally, then we need to divide the distance in x and y by square root of 2.

if( is_move_vert == true and is_move_hori == true )

{

divider = sqrt(2);

}

x+=dx/divider

y+=dy/divider

Ok, that was the longest code we have so far, but it isn’t as scary as it looks like. I’ll go over each section of the code step by step to explain

//Check and control player movement

is_move_vert = false

is_move_hori = false

dx = 0

dy = 0

divider = 1.0;

There is nothing much in this part. Basically we reinitialize all the required variables every frame. This is because the movement can change any moment. The dx and dy variables refer to the distant of the ongoing movement.  That mean, if dx equals to 5, we will move 5 pixels to the right. Divider is there to normalize the diagonal movement, which you will see later on.

//Check which speed to use

if(keyboard_check(vk_shift))

{ is_focus = true;

}else

{ is_focus = false;

}

if(is_focus == true)

{ move_speed = focus_speed

}

else

{

move_speed = normal_speed

}

This code is pretty self-explanatory. If you press shift, then you’re in focus mode. And if you’re focus, then the move_speed will refer to focus_speed, otherwise it will refer to the normal one.

//Check differences in x and y coordinate according to key pressed.

if (keyboard_check(vk_up))

{

dy = -move_speed;

}

if (keyboard_check(vk_down))

{

dy = move_speed;

}

if (keyboard_check(vk_left))

{

dx= -move_speed;

}

if (keyboard_check(vk_right))

{

dx = move_speed;

}

And this is the part that calculates the distant that your character will be moving. If you press up, then you’ll be moving upward with the distant of whatever move_speed is currently refer to.

However, if you do just this and not the next section of the code, there will be a mistake in diagonal movement. Why, you may ask?  You can see the reason from this link here ->http://www.analyzemath.com/Trigonometry_problems/special_triangles.html

As we can see, moving upward a pixel and moving rightward a pixel, does not mean you are moving diagonally with the same number of pixels. It turns out that you are actually moving diagonally pixels, which is more than you actually need to move!  So what do we do? Simple. We’ll check that if there’s a diagonal movement, we will divide the distance by before moving. And this is exactly what the next section of code does.

//Check movement orientation

if (keyboard_check(vk_up) or keyboard_check(vk_down))

{

is_move_vert = true;

}

if (keyboard_check(vk_left) or keyboard_check(vk_right))

{

is_move_hori = true;

}

//If move diagonally, then we need to divide the distance in x and y by square root of 2.

if( is_move_vert == true and is_move_hori == true )

{

divider = sqrt(2);

}

x+=dx/divider

y+=dy/divider

As we can see, we first check to see whether both is_move_hori and is_move_vert are true true. If they are, that means we are moving both horizontally and vertically, which is moving diagonally. Then we change the value of divider from 1 to sqrt(2)

The last two lines are very easy. We then add the displacement to the current x and y position, by divided them by the divider first in order to normalize the diagonal movement.

Now run the game and you should be able to control your ship with the keyboard arrow. Holding shift while moving should slow down your ship :D

Adjust the boundary and depth

You should notice something funny’s here. You can go over the GUI! And that is not good :O Now, let’s do something about that, shall we?

First of all, the reason our ship is drawn over our GUI is because it is at the same depth as the GUI. If the instances have the same depth value, the newer ones will be drawn over the older ones. However, we want to GUI to always be above the player, and we can do this by adjust the depth value of the objGUIFrame object. Double click the object and you should see Depth value on the left. For safety sake, I’ll suggest you to use something like -100, in case there are other objects that need to be drawn over the player ship but should be covered by the GUI as well ( namely enemy bullets ;) )

Run the game again and you should see that once we move out of the play field, we will be covered by the GUI and thus cannot see the ship. Still, this is not desirable! We don’t want the player to move out of the playing field at all.

In order to do this, we have to determine the boundary of the playing field. This is an easy task, as we can see from the picture below.

As we can see, the left most that our ship can go is 32 and the right most is 32 + 385 = 417, and you can do the similar calculation with the topmost and the bottommost. However, it isn’t really wise to hard code these numbers into the game, since we might change it later. However, these kind of variables aren’t really changing during the play, hence it would be better if we make them constant.

And yes Game Maker has this feature for us!  Double click on Global Game Setting and click on the Constant tab. Once you see the constant variable editor, enter each field like the picture below.

And then when you type FIELD_X or FIELD_Y in the script, Game Maker will automatically highlight those words for us so we know that they constant.

Now it’s time to put these constants into good use. Go to our objPlayer’s step event, and add this to the bottom.

//Keep player inside the playing field

if( x > FIELD_X + FIELD_WIDTH) x = FIELD_X + FIELD_WIDTH

if( x < FIELD_X) x = FIELD_X

if( y < FIELD_Y) y = FIELD_Y

if( y > FIELD_Y + FIELD_HEIGHT) y = FIELD_Y + FIELD_HEIGHT}

This code will make sure that if the player ever out of field, their position will be reset to the boundary position. Now, run the game and you should be able to move your character freely but limited to inside the game play field.

And that’s it for this chapter. Until next time.

How to make a bullet hell shmup in Game Maker” Chapter 1: Getting Start and the main game GUI

“How to make a bullet hell shmup in Game Maker”

Chapter 1: Getting Start and the main game GUI.

>> Download the project used in this tutorial here <<

Introduction

Though this tutorial is mainly for beginner, you are expected to have some basic knowledge of Game Maker. I’d recommend you to read Derek Yu’s Game Maker tutorial for beginner first. Follow his great tutorial up to chapter 2 or 3 should get you ready.

Making your first room

First of all, start Game Maker. I will be using Game Maker 7.0 professional in this tutorial. However, I believe it can be applied to any versions, registered or unregistered.

On the left panel you will see a tree structure. We need to first create a room for our game. This is where our game is going to take place. A room is like a stage or a scene where our game objects will interact with each other and create the actual game.

To create a room, simply right click on ‘Rooms’ and choose ‘Create Room’

Right click on the new room you just created and choose rename because any name is better than room0. Let’s name it ‘init_room’. This will be the very first room that the game enters. Hence its main task is to initialize anything before the game title screen start.

Now click on the setting tab. In here, we can set the room properties. Choose the size to be 640 x 480 and set the room speed to be 60. This is basically your frame per second, and 30 is too low in my opinion.

Save your game and click on run the game in debug mode. You should see a blank window with gray background. On the debug window, your fps should say 60 fps.

Folder Structure

If there’s no problem let’s go to where you save your source file. In order to make this tutorial easier to follow, I’d suggest you to name the folder the same as I did for now. After you got all the concepts you can, and I highly encourage you to, remake the game again with your own naming convention.

Create a new folder called ‘dat’. This will be your main resource folder. So any game resources should be in here

Then inside that folder, create the following folders.

- img

- bgm

Once you’re done. Find yourself a saudio.dll. Or you can just download it here. No matter how you got the dll, place it in the same level your source file is.

So basically your game folder structure should look like this

- Your game folder

- your source file

- saudio.dll

- dat

- img

- bgm

Let’s get back to Game Maker interface. Create another room and call it ‘shmup_room’. This will be the room for our main game. Don’t forget to set the room speed to be 60. Also, you might want to change the background color of this room and the init_room as well. Let’s choose black since it’s pretty common to start a game with a black screen before some swooshy title screen show up. You can change the background by clicking on the background tab, and choose the color to be black. Pretty easy, eh?

Making the main game’s GUI

Things will get pretty complicate from now, but nothing you can’t handle. We will be making a game interface. Something likes this.

First of all, create a new folder call ‘gui’ inside our ‘img’ folder. This will be where we store our gui graphic. To make this tutorial easier follow, you can use the graphics that come with the sample project I have for you at the top. (Those images were taken from Danmakufu, by the way)

Now, all four files, which are frame00 – 03 should be inside the gui folder. Then let’s get back to the GM interface.

It’s time for us to write some script. What we are going to do is to load the images externally. This will greatly enhance your game loading speed. Believe me, it is not fun having to wait 5-10 minutes everytime you debug your game! Don’t worry about your resource files being exposed for now; we can deal with that later :)

In order to do this, we have to do it through scripting. So, let’s start with right clicking on ‘script’ in the left panel and choose create script. Name it ‘load_graphics’ so that we know what script we’re dealing with.

Double click on the script that we just created will open up a script editor. This will be where you write your script. I won’t go into details about syntax and all that jazz. You can read them up in GM help file.

Since the gui frame are separated into 4 parts, but they form just one gui when they’re put together. I think an array variable would make it easier to remember. You can create an array variable by simply type

array_name[array_index] = something

Also, since the graphic can be used by basically any objects. Hence, it’s logical to make it globally known. To do this, we put a keyword ‘global’ in front of the variable name.

All in all, to declare a global array would be something like this.

global.gui_frame[0] = 0

The above code means we assign 0 to the first element in the global array named gui_frame. (Array first element start with 0.)

Now, let’s move on to loading resource externally. Since all our four images won’t be using any transparency, we will use sprite_add instead of sprite_add_alpha.

sprite_add(filename, how many subimages(frames), precise collision?, tranparent?,

smooth?,preload?, xorigin, yorigin)

The words inside the parenthesis, separated by commas, are called ‘arguments’. A function will use these arguments to give us an appropriate result. In this case, to load a graphic and add it as a sprite, we need to know the filename, how many frames the sprite has, what kind of collision checking, will it be transparent, will it be smooth, should it be preload, and the anchor point of the image.

So in the end, our code of loading images externally would be:

//Load Pictures externally. Make game load MUCH faster.

//The arguments are like this

//sprite_add(filename, how many subimages(frames), precise collision?, tranparent?, smooth?,preload?, xorigin, yorigin)

//or

//sprite_add_alpha(filename, how many subimages(frames), precise collision?, preload?, xorigin, yorigin)

//LOAD GUI

global.gui_frame[0] = sprite_add(“dat/img/gui/frame00.png”,1,false,false,false,false,0,0)

global.gui_frame[1] = sprite_add(“dat/img/gui/frame01.PNG”,1,false,false,false,false,0,0)

global.gui_frame[2] = sprite_add(“dat/img/gui/frame02.PNG”,1,false,false,false,false,0,0)

global.gui_frame[3] = sprite_add(“dat/img/gui/frame03.png”,1,false,false,false,false,0,0)

According to the above code, we have created a global array called gui_frame. The array contains four elements, running from 0 – 3, and the elements are the graphic that we externally loaded.

However, script will be no used if nobody is using it. Therefore, we need to create an object that is responsible for calling this script. Right click on the object folder in the left panel and choose Create Object. Name it ‘initGame’ to let us know what this object does.

Now, double click on initGame object to open the object properties window. Click on Add Event and choose Create Event. This is because we want to load graphic at the very beginning.

Move your mouse to the right panel and click on the tab that says control. Under the code section, drag the first paper icon and drop it on the action box. This action is called ‘Execute a piece of code’ and will allow that object to perform whatever you tell it to do. In this case, we will tell initGame to call load_graphics so that we can actually make use of what we have written before.

Next, double click on the paper icon in the Actions box. This will open up another script editor with a window called Execute Code like the following picture

To execute the script you already wrote is really easy. Just type the script name, follows by () and viola! It will go look for your script and execute whatever you have written before.

load_graphics()

Now, after we finish loading the graphic, we don’t want the player to stay here any longer. Hence we have to change the room to shmup_room, where our main game will take place. The function that is used for changing room is called room_goto(roomIndex). In this case, the roomIndex is shmup_room. Hence, our code will be

load_graphics()

room_goto(shmup_room)

Click ok to save changes. And we’re done with the object initGame for now.

We have an object that takes care of the graphic loading; however, we still need an object that is taking care of drawing them. There won’t be anything shown on the screen if no one is drawing them, right? Create a new object, name it oGUIFrame. Open up the object properties window but this time add Draw event instead of Create.

Add an action Execute a piece of code to the Draw event, just like what you did to the Create event of initGame. Now, a simple function used to draw a sprite is.

draw_sprite(image, subimage, x, y)

The function is pretty self-explanatory. I’ll give you the code here and you can play around with the number and figure how things work out yourself :D

draw_sprite(global.gui_frame[0],0,0,0);

draw_sprite(global.gui_frame[1],0,32,0);

draw_sprite(global.gui_frame[2],0,32,465);

draw_sprite(global.gui_frame[3],0,417,0);

Now, we have everything ready. The only thing we need to do now is to place all these objects to their appropriate room. Go to init_room and place initGame in that room. This can be done by clicking on the Objects tab and choose initGame from the bottom left box. You can place it anywhere in the room but I prefer to put it at the first square at the top left ( position 0,0 ) If you see something like this then you’re doing it right.

Then, goto shmup_room and place oGUIFrame in that room, like what you did previously. And then run the game. You should see something like this.

And congratulations! You’re another step closer to complete your touhou game! :D

>> Download the project used in this tutorial here <<