Creating Your First RPG Tutorial
This tutorial is made for DarkBASIC Professional and may not work in DarkBASIC Classic.
In This tutorial I hope to cover various aspects of creating an RPG.
= Complete
= Coming Later
Setting Variables
Setting Types
Creating Arrays
Creating Functions
Creating a Game Menu
Creating my First 3D object
Making my object move
Creating The 3D World....
And once this list is done i may continue to add to this tutorial hopefully
Part I: Setting Variables
Open up DBpro click File>New Project name it RPG, once you have done that carry on.
Almost EVERY game needs to set variables. They are good for RPG’s to be the stats, FPS’s for Ammo etc and a lot more.
For our stats we’ll start with setting simply the Health Points, the Magic Points and the Power.
HealthPoints = 100
MagicPoints = 100
Power = 10
This will set our variables to 100, 100 and 10. But what if we want to use decimals in a variable? We set it to a float variable. The most common way is like this, using a # to make it a float.
HealthPoints# = 100.50910
MagicPoints# = 100.147625
Power# = 10.124725721
But what if you just want to type HealthPoints instead of HealthPoints# every time you want to use the health? Easy! Just set it to a float at the start and the variable type is a float.
HealthPoints as float
MagicPoints as float
Power as float
HealthPoints = 100.50910
MagicPoints = 100.147625
Power = 10.124725721
What if we wanted to make the variable store words? We make it a string
First way, use a $ at the end second way use as string at the end
Or
MyWords as string
MyWords = “Hello World”
Now what if we want to display the amount of the variable on screen? We use the print command.
But that just doesn’t work! So we use the command Suspend for key so that the program waits for you to press a key before it ends.
Print MyWords
Suspend for key
Now we want to print HealthPoints so we use
Print HealthPoints
Suspend for key
What if we want to have it display Health Points: 100.50910 then we have to use a sring and a float. Either we use a variable to store Health Points: or we simply write it.
Print “Health Points: ” + HealthPoints
Suspend for key
That didn’t exactly work. Because the variable HealthPoints is after a string and it is not a string itself we need to put it inside a str$() so it reads the number as a string
Print “Health Points: ” + str$(HealthPoints)
Suspend for key
Using decimals for Stats is kind of strange in my opinion so lets change to using integers (Numbers Without Decimals) two ways
Or
MyInteger as integer
MyInteger = 100
Well that’s about it for variables your code should show
HealthPoints as integer
MagicPoints as integer
Power as integer
HealthPoints = 100
MagicPoints = 100
Power = 10
Print "Health Points: " + str$(HealthPoints)
Print “Magic Points: ” + str$(MagicPoints)
Print “Power: ” + str$(Power)
Suspend for key
Hope you enjoyed part one
Part II: Creating Types
Creating types in DBpro can be very useful for things. For example if we wanted the player to have Health, Magic Points, Stamina and Power then we could make a type for this. To create a type we need to define a name like so
So we have created a type. Now we need to store some variables in this type so we will setup the stats I mentioned above as integers, which means they will be numbers, with no decimals.
Type Stats
Health as integer
MagicPoints as integer
Stamina as integer
Power as integer
Endtype
You will notice that I have added Endtype to the end of the code so that it closes the type and doesn’t store anything else inside the type.
With this type we can re-use the health variable because it is only part of this type. For example we could have our player type as above, and then copy and paste that type and rename it Enemy, then you will have two different variables.
Now to show you how to assign this type to a name. To do this we use this.
Type Stats
Health as integer
MagicPoints as integer
Stamina as integer
Power as integer
Endtype
Global Player as Stats
Now we can use the Health variable and all the other variables like so
Player.Health = 100
Player.MagicPoints = 75
Player.Stamina = 10
Player.Power = 20
This is exactly the same as variable usage except our variables are a bit different
Well that’s all for Types.
Part III: Arrays
Arrays are another useful way to store variables. A use in an RPG would be to use it for a monster spawn.
To create an array first we use Dim to open the array, and then assign a name, followed by the number of pockets in the array.
So say we used this as an array for 5 spawns. This array could store data for 5 monster spawns with 10 monsters in each spawn. So lets start by creating a monster called Fire Bird in spawn 1. Also we will add some other monsters. Lets Create spawn 1 now.
Dim Monsters(5,10) as string
Monsters(1,1) = “Fire Bird”
Monsters(1,2) = “Ice Bird”
Monsters(1,3) = “Lightning Bird”
Monsters(1,4) = “Fire Cat”
Monsters(1,5) = “Ice Cat”
Monsters(1,6) = “Lightning Cat”
Monsters(1,7) = “Fire Dog”
Monsters(1,8) = “Ice Dog”
Monsters(1,9) = “Lightning Dog”
Monsters(1,10) = “Lava Boss”
This will create an array with 10 monsters all in spawn 1 named as shown above. This can be useful in some areas, however we will get to that later.
That’s all for Arrays.
Part IV: Creating Functions
First of all click on the Files tab in the DBpro Editor then click Add New then call it functions.dba then carry on
Function is a command that lets us make our own commands. We can make a function to set all our variables for us. Lets do that! We use Function then the functions name we will use Set_Variables() the () on the end means there are no parameters. If there were parameters there would be something between ( and ) then we insert the code that we want the function to use underneath the first line and once that’s there we close it with Endfunction so lets make the lesson from part one into a command.
Function Set_Variables()
HealthPoints as integer
MagicPoints as integer
Power as integer
HealthPoints = 100
MagicPoints = 100
Power = 10
Print "Health Points: " + str$(HealthPoints)
Print “Magic Points: ” + str$(MagicPoints)
Print “Power: ” + str$(Power)
Suspend for key
Endfunction
But now if we want to use the variables HealthPoints and all the other ones outside of the function we can’t because its local to the variable. To fix that we make it a global variable which means you can use it throughout the code because its global to the whole code.
Function Set_Variables()
Global HealthPoints as integer
Global MagicPoints as integer
Global Power as integer
HealthPoints = 100
MagicPoints = 100
Power = 10
Print "Health Points: " + str$(HealthPoints)
Print “Magic Points: ” + str$(MagicPoints)
Print “Power: ” + str$(Power)
Suspend for key
Endfunction
Not to difficult? We will put our functions in Functions.dba and our Main code in RPG.dba. Now to use it we put Set_Variables() at the top of the page your code should be:
Set_Variables()
Function Set_Variables()
Global HealthPoints as integer
Global MagicPoints as integer
Global Power as integer
HealthPoints = 100
MagicPoints = 100
Power = 10
Print "Health Points: " + str$(HealthPoints)
Print "Magic Points: " + str$(MagicPoints)
Print "Power: " + str$(Power)
Suspend for key
Endfunction
Hold up! When we press a key there is an error saying “You Have hit a FUNCTION declaration in mid-program!” so we make it end the program before it gets there.
Set_Variables()
Function Set_Variables()
Global HealthPoints as integer
Global MagicPoints as integer
Global Power as integer
HealthPoints = 100
MagicPoints = 100
Power = 10
Print "Health Points: " + str$(HealthPoints)
Print "Magic Points: " + str$(MagicPoints)
Print "Power: " + str$(Power)
Suspend for key
End
Endfunction
You will notice that after Suspend for key I have put End which ends the program. Well before after Suspend for key it was carrying on but there was no code so it ended but this time it carried on to the Function which hits an error. Now when it carries on it will hit the End Command and end the program.
Functions are a good way to organise code nicely. Instead of a whole list of coding it just has a couple of function names as the code and the rest at the bottom which can fold up.
We will be using functions in the rest of the tutorial so make sure you understand them before you continue.
Well that’s the end of part two your code in RPG.dba should be:
Rem Project: RPG
Rem Created: Date.
Rem ***** Main Source File *****
Set_Variables()
And in Functions.dba should be:
Rem *** Include File: Functions.dba ***
Rem Created: Date.
Rem Included in Project: C:Program FilesThe Game CreatorsDark Basic ProfessionalProjectsRPGRPG.dbpro
Function Set_Variables()
Global HealthPoints as integer
Global MagicPoints as integer
Global Power as integer
HealthPoints = 100
MagicPoints = 100
Power = 10
Print "Health Points: " + str$(HealthPoints)
Print "Magic Points: " + str$(MagicPoints)
Print "Power: " + str$(Power)
Suspend for key
End
Endfunction
Hope it helps
Part V: Creating a Game Menu
In this part we will be using a Function to create a menu to start our game. We will create it using a Function we will make called Make_Button(x1,y1,words$)
Lets start. Make a Function called Make_Button(x1,y1,words$) in functions.dba
Function Make_Button(x1,y1,words$)
Endfunction
To make a button we will use the command Box to create a box around the button and the command Text to create the words on the button
Function Make_Button(x1,y1,words$)
X2 = text width(words$)+4
Y2 = text height(words$)+4
Box x1,y1,x1+x2,y1+y2
Text x1,y1,words$
Endfunction
This function uses the parameters x1,y1 and words$ to get the position for the button and the writing to put on the button, then it sets x2 and y2 to those values which get the text width and text height of the buttons text and add 4 so it makes the button bigger than the text. Then it uses a box which is made with x1,y1,x1+x2 and y1+y2. x1+x2 will make the box bigger than the button likewise with y1+y2. Finally it uses the command text x1,y1,words$ to position a piece of text to x1,y1 and prints the variable words$.
Now to test our button we do this
In RPG.dba under set_variables() we put a loop which will keep repeating the commands inside the loop
Do
Make_Button(200,200,“Start Game”)
Loop
This makes the button at position 200,200 with the text Start Game on it.
Now we want to do this, Check if the user clicks on the button, If so then go into the game.
For this we make a variable inside this function called Clicked, which will determine whether the button is clicked. To check if the button was clicked we use an If statement and the command Mouseclick() as well as a few other commands.
Ok so we need to create this now. Inside the function Make_Button() we will put this code
Function Make_Button(x1,y1,words$)
X2 = text width(words$)+4
Y2 = text height(words$)+4
Box x1,y1,x1+x2,y1+y2
Text x1,y1,words$
If mousex()>x1 and mousex()<x1+x2 and mousey()>y1 and mousey()<y1+y2
If Mouseclick()=1
Clicked=1
Endif
Endif
Endfunction Clicked
This will check if the mouse is in the button. If it is in the button and then you click it will set Clicked to 1. Also you will notice I have added Endfunction Clicked well this is because if an If statement is called then we want it to return the value of clicked.
So now we can tell when our button is clicked. Lets get the If statement that asks if the button is clicked, and then tell it to exit the loop and enter the main program loop.
REM ***** Menu Loop *****
Do
If Make_Button(200,200,“Start Game”)=1
Exit
Endif
Loop
REM ***** Main Loop *****
Cls
Do
`Our code will go here
Loop
You will notice that I added Cls just before the main loop. What this does is clears the screen of all the currently displayed 2D images. So when we click the button, it will clear the menu, and go into the main game loop.
Now when you click on a button it should go to a blank screen.
That is the end of this part your final code should show:
REM ***** Set Up *****
Type Stats
Health as integer
MagicPoints as integer
Stamina as integer
Power as integer
Endtype
Global Player as Stats
Player.Health = 100
Player.MagicPoints = 75
Player.Stamina = 10
Player.Power = 20
REM ***** Menu Loop *****
Do
If Make_Button(270,100,"Start Game")=1
Exit
Endif
Loop
REM ***** Main Loop *****
Do
Cls
`Our code will go here
Loop
REM ***** Functions *****
Function Make_Button(x1,y1,words$)
X2 = text width(words$)+4
Y2 = text height(words$)+4
Ink Rgb(255,0,0),0
Box x1,y1,x1+x2,y1+y2
Ink Rgb(0,0,0),0
Text x1+2,y1+2,words$
If mousex()>x1 and mousex()<x1+x2 and mousey()>y1 and mousey()<y1+y2
If Mouseclick()=1
Clicked=1
Else
Clicked=0
Endif
Endif
Endfunction Clicked
This is all we will need, we won’t need our other variables anymore so we will create our type up the top like so.
That’s the end of this part on creating your menu. If you want to add more buttons just use that same code except make the effect that they have a little different for example lets just add a Quit button for fun
REM ***** Set Up *****
Type Stats
Health as integer
MagicPoints as integer
Stamina as integer
Power as integer
Endtype
Global Player as Stats
Player.Health = 100
Player.MagicPoints = 75
Player.Stamina = 10
Player.Power = 20
REM ***** Menu Loop *****
Do
If Make_Button(270,100,"Start Game")=1
Exit
Endif
If Make_Button(275,150,"Quit Game")=1
End
Endif
Loop
REM ***** Main Loop *****
Do
Cls
`Our code will go here
Loop
REM ***** Functions *****
Function Make_Button(x1,y1,words$)
X2 = text width(words$)+4
Y2 = text height(words$)+4
Ink Rgb(255,0,0),0
Box x1,y1,x1+x2,y1+y2
Ink Rgb(0,0,0),0
Text x1+2,y1+2,words$
If mousex()>x1 and mousex()<x1+x2 and mousey()>y1 and mousey()<y1+y2
If Mouseclick()=1
Clicked=1
Else
Clicked=0
Endif
Endif
Endfunction Clicked
So now when we click on the Quit Game button it ends the program. That’s the real end of this part.
Enjoy
RPG Engine Work in progress!