*ANNOUNCEMENT*
I have decided that i am going to continue my tutorials in DBPro. Sorry for anyone wanting the tutorials for dbc but you can still follow along and maybe figure some things out along the way.
^ANNOUNCEMENT^
The following tutorial is assuming that you have just purchased Dark Basic, and will go over everything needed to start you off.
This tutorial is WIP, and progress will grow as it goes.
The Complete Basics
So You just bought Dark Basic? I bet your willing to make your game making dreams come true, aren't you? Everyone who started out with Dark Basic does, so dont feel nervous about it.
If your looking to make an RPG, you've come to the right place. I will go over with you, step by step, explaining everything on the way, of how to make an RPG.
So lets get started!
*Tutorial 1 - The Variable
Now, lets first open DB up. You can use ALT TAB to toggle between DB and this tutorial.
First of all, lets learn about variables. In an RPG, you'll need Health, Money, Player Levels, stats, etc. All these things shall be accounted by variables.
So lets get our first variable, shall we?
How about health - to store health, it will be a float. For a variable, all float variables should end with a ' # ' . For example, health would be not be stated as ' health ' in the code, but as ' health# ' . To assign the variable a value, its probobly exactly what you thought. It would look like this:
health# = 100
Now health doesn't have to be 100, but its a reasonable amount. When you compile the program, the program will not say ' 100 ', because we didn't make it do that. And thats exactly what we are gonna do next.
---------------------------------------------------------------------
*Tutorial 2 - Printing to the screen
Now that you know how to store a float variable, lets learn how to print it to the screen. Its easy, all you have to do is:
Print health#
Add it under the ' health#=100 ' and get:
health#=100
Print Health#
Compile, and you can see the program print the variable health#. It does exactly what it says : It
Prints variable
health#, you see?
Now, thats not how you print words - to print words, all you need is to replace ' health# ' with ' "words here" ' and you get the words
' words here ' on the screen.
Now, there is another variable that you can store - Its called a string variable. A string variable is not a value, but words in a sentence. And instead of adding the ' # ' you add a ' $ '.
For example, lets use the string variable ' HealthWords$ ' . To assign that variable words, you must simply do:
HealthWords$="Your Health is "
Print HealthWords$
As you might've guessed, that stores the words "Your Health Is " into the variable and prints it onto the screen.
I know what your thinking - how do you add the two variables we made together to form a sentence? Its in the next tutorial.
---------------------------------------------------------------------
*Tutorial 3 - Joining two different variables to Print
Its really quite simple - we must use the plus sign in order to do this. Also, you will learn of the function called ' str$() ' .
The plus sign is used for joining the two variables, HealthWords$ and health# (Notice the difference of # and $).
The ' str$() ' function is called a function because it has the
' () ' in it. What does that do? It allows you to put parameters into the function. This functions, will change a variable with #, into a variable with $. So inside the ' () ' you will put the variable
' health# ' as the parameter. It will look like this:
str$(health#)
Now this is where the plus sign comes in. so far our code should be:
health#=100
HealthWords$="Your Health Is "
Print HealthWords$
You see the ' Print HealthWords$ ' ? Add a plus ign after it, then add the ' str$(health#) after the plus sign. Compile it, and it should look like:
health#=100
HealthWords$="Your Health Is "
Print HealthWords$+str$(health#)
Hooray! We've done it! If you've come this far, your doing great!
Now I bet your thinking that health can't always be att 100, aren't you. Thats the next tutorial.
---------------------------------------------------------------------
*Tutorial 4 - Decreasing+Increasing the health variable
There is more then way to do it, and we will go over both ways.
In the past tutorials, you learned about two different variables, how to assign values to them, and how to join them together and print them to the screen.
Now, to increase and decrease the health.
Way #1-
health#=health#+1
What you are doing, is adding the same value to itself to increase it. For a deeper explanation, health#+1 equals 101, because health# used to be 100, and you are adding one to it. You are stating that health# now equals 101 by writing the code ' health#=health#+1 '. Do you under stand? It goes the same way with decreasing, you would just change the plus sign to a minus sign and - wala!
Way #2-
Inc health#,1
Inc, is actually a shorter way of sayng Increase. You are increasing health# by 1, by putting the one after health#. The comma is used for seperation between the variable and how much you want to increase by. It must always be between two different parameters. Now you see what this does. For decreasing, its just as simple. You just change the Inc, to Dec, which is short for Decrease.
So lets use both ways. Our code should be:
health#=100
HealthWords$="Your Health Is "
Print HealthWords$+str$(health#)
Add the ' Inc health#,1 ' to the bottom of that. Now, add this to the bottom of the Increase command:
HealthWords$="Your Health Is Now "
Print HealthWords$+str$(health#)
And your whole code should be:
health#=100
HealthWords$="Your Health Is "
Print HealthWords$+str$(health#)
Inc health#,1
HealthWords$="Your Health Is Now "
Print HealthWords$+str$(health#)
Compile, and you should see what happens.
But, I'm assuming you dont want the health to change by itslef so quickly. This is where the next tutorial starts!
---------------------------------------------------------------------
*Tutorial 5 - Making the Program Wait
There are two ways to do this - waiting for a key press or waiting for an amount of time.
Waiting for a key press is easy - its just the command ' Wait Key '.
Add that right before the ' Inc health#,1 ', and your code should be:
health#=100
HealthWords$="Your Health Is "
Print HealthWords$+str$(health#)
wait key
Inc health#,1
HealthWords$="Your Health Is Now "
Print HealthWords$+str$(health#)
Compile that code, and you can see that it waits for you to press space or return to continue.
There is also a way to make it an amount of time to wait for. You just have to change the ' key ' in ' wait key ' to an amount. But let me warn you - putting 1 does not mean one second. Putting 1000 would make it one second, actually.
So try it, you code should look like:
health#=100
HealthWords$="Your Health Is "
Print HealthWords$+str$(health#)
wait 1000
Inc health#,1
HealthWords$="Your Health Is Now "
Print HealthWords$+str$(health#)
Well, there you go! Thats waiting!
---------------------------------------------------------------------
*Tutorial 6 - Making a Program Loop
If you want your program to continue to keep going, and going, people use loops. From what I know, there are 4 kinds of loops. I will only tell you two right now, because its not important to learn all of them right now.
The First Kind of Loop-
It called the Do-Loop. It is called this, because the code for a Do-Loop would be set up as the following:
Do
*Your code here
Loop
What this does, is it goes from do, to loop, and keeps going from do to loop and going and doesn't stop unless you made it stop, which I will explain in another tutorial.
So for an exercise, make the program We had just made about health loop, by putting a do before the first command, and a loop after the last command. Watch what it does.
The Second Kind of Loop-
This is called the for-next loop. This one is also a little more complicated then the Do-Loop, but you will manage. The Code shall be setup like this:
for health=1 to 5
Print str$(health)
next health
Its more of an example then a setup, but it makes more sense like this. Put this after you current code, but right before the ' Loop ' command. Your code should Now look Like this:
Do
health#=100
HealthWords$="Your Health Is "
Print HealthWords$+str$(health#)
wait 1000
Inc health#,1
HealthWords$="Your Health Is Now "
Print HealthWords$+str$(health#)
for health=1 to 5
Print str$(health)
next health
Loop
Are you understanding these loops? I sure hope you are, because they are really important to make an RPG.
----------------------------------------------------------------------
*Tutorial 7 - Text Commands
First off, if you want to position where the text appears when you use the ' Print ' command, you have to use the ' Set Cursor x,y ' command. The ' x ' should be replaced with the X value on the screen (From left to right, higher number will be further right). The ' y ' should be replaced with the Y value on the screen (Up and Down, higher number will be further down). So If you want to position words onto the screen, you will have to do the following:
Set Cursor x,y
Print "words"
Pretty anoyying huh?
Well there is a solution to that. Instead of using Two commands, you only have to use one, and it is: ' text x,y,"Words" '. Its pretty much just the two command Set Cursor and Print combined.
Unlike ' print ', ' text ' can be changed by Font, Size, and can be Italic, and/or bold.
To Size the Text:
Set Text Size *sizevalue (Replace the ' *sizevalue ' with the size of the text)
To Change the Font:
Set Text Font *font (Replace the ' *font ' with the font type
Bold: Set Text to Bold
|-| Italic: Set text to Italic
Bold+Italic: Set Text to BOLDITALIC
|-| Normal: Set text to normal
Also, just plain white text and black background all the time is boring. Thinking what I'm thinking?
Use the command ' Ink rgb(r,g,b)
,rgb(r,g,b) '. It seems a little complicated, but like before, you'll manage. First Of all, the comma I made bold is seperating the parameters. The one to the left of the bold comma is the color of the text in rgb(r,g,b). The r stands for red, b stands for blue, and g stands for green. As you are thinking, changing the r g and b inside the parenthesis change the red green and blue value of the text color. Its the same with the rgb(r,g,b) to the right of the bold comma, except that it changes the background color.
Well, thats about all you need to know about text. Mess around with everything I've taught you so far, its enough to even make a small text game. But Did you ever wonder, how to make a button with this text? Find out next tutorial!
----------------------------------------------------------------------
*Tutorial 8 - The Text Button
To do this tutorial, we will need a better sense of 2D coordinates (x and y) . I've drawn a simple image which I can explain easily:
http://www.solisearch.net/ims/pic.php?u=191602SZkT&i=240289
The red line is the Y axis in this image, the 0 in red at the top of the page indicates you, that this is where the y axis starts. If the y axis should increase, the arrow would move down. Get it?
Its the same with the x axis- If the x axis should increase, the blue arrow would move right. I think its time to actually move onto the button now.
Learning coordinates is what we need to find where you shall click to trigger the button.
Lets refresh our minds and our code, shall we? Erase all your code. Dont worry, I'm sure you can wip it all up in 2 seconds now!
Now, as an exercise, I'm going to ask you to make a do-loop, and make text at the x coordinate of 20, and the y coordinate of 55. Make the text say "Click Me!"
AND USE 'Click me!' WITH A VARIABLE CALLED WORDS$ ! . Can you do this? I'll wait.
If you think you got it, then great job! If not, look back at the tutorials to see how we did it before.
Your code should be:
WORDS$="Click Me!"
Do
text 20,55,WORDS$
loop
Moving on!
Now, for a button we need 2 x corrdinates, and 2 y coordinates. Yes, so you would have x1,y1,x2,y2. Those would be the parameters.
You probobly know what the x2 and y1 are - they are the coordinates where you positioned the text. So x1 is 20, and y1 is 55.
How in the world do you find out x2 and y2? Easy. There is another function for this. Its called text hieght() and text width(). The two, as you assumed, will get the hight and width (x2 and y2) of the text.
We must add the ' /2 ' after Text Height(), which divides it by two to get a more reasonable button.
So lets get the parameters into variables, shall we? We done need a
' # ' or an ' $ ' . We wouldn't use any at all, cause we dont need them here
(Woah, first smiley in the whole tutorial!)
To set the variables:
x1=20
y1=55
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)/2
You see, by using the Text Width() and Text Height() functions, there must be a parameter inside the parenthesis. The parameter, of course, would be the variable of the text! So put that code of the variables, right before the do-loop, and right after the ' WORDS$="Click Me!"
(Now that we have defined the text's corrdinates to be positioned at, we can just replace the 20 and 55 with x1 and y1)
Your code should be:
WORDS$="Click Me!"
x1=20
y1=55
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
Do
text x1,y1,WORDS$
loop
Now, clicking part! As you know, we click with the mouse button. The mouse button is usefull in may things, but we choose clicking here.
We are gonna need the parameters of the mouse. We are gonna need its X and Y. There are more functions for this! Yay functions! They are called Mousex() and simply Mousey(). Now, this is the most challeging and complicated part, so I'll draw an image for it too:
http://www.solisearch.net/ims/pic.php?u=191602SZkT&i=240290
You see, for you to click on the button, the mouse must be greater then x1 and y1, but less then x2 and y2. I think that explains it, does it not? It it were less then x1 and y1 then you could click the button even when the cursor wasn't on the text!
Now, to learn this, there is another thing I must teach you. It is called, the If-endif statement! What you can do is determine ' If ' something happens, whether it is about variables, or even if you clicked the mouse. Although, for every If, you need an Endif. So here is the code. Look at the previous image, and try to figur out with math how its finding the coordinates
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
endif
endif
Understand?
Well, we have where to click the button... now to click it!
We need another If-endif statement. Although, to see if you click the mouse, there is the function ' mouseclick() '. For mouselcick to be one, is to click the left mouse button. But here, lets use a trigger. The trigger is a variable that when equals 1, it starts something. So the code would be:
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
(Notice the two endif's for the two If statements. As you can see, the trigger is called 'Pressed')
For this trigger to work, at the very top of our code, we shall put ' Pressed=0 ' to tell the program that the trigger shouldn't be on yet.
So what do you want to do when you click the button? How about we trigger it to start the game? Thats the next lesson!
Right now, your full code should be:
Pressed=0
WORDS$="Click Me!"
x1=20
y1=55
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
Do
text x1,y1,WORDS$
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
loop
(We had put the Button part inside the loop only because we want you to be able to click it whenever)
----------------------------------------------------------------------
*Tutorial 9 - The Text Button Part 2
So you know how to create the button and find out where to click on it. Now its time to put it to works for the RPG!
We gotta put what happens when the trigger ' Pressed ' is triggered. Put this code underneath the two endif's in the loop:
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
First of all, (the first line) its saying then when Pressed is Triggered, it will change the color from RED to White. You notice that 'then' and the 'else' ? That is a way of shortening an IF statement.
Second of all, its saying ' when Pressed is triggered '. Third of all, its saying that pressed equals clicking the mouse. So right now, the mouse hasn't been clicked at all, there for pressed is once again 0. Once you click, it will be 1 again, and the function will end.
Get the final code from last Tutorial, and change the WORDS$="Click Me!" to WORDS$="New Game" .
Now, what shall happen when you click the button 'New Game' ? We are gonna have to get out of the loop, so this is where GOTO comes in.
Under neath the ' loop ' command, about 2 lines below, write the words
New_Game:
The words ' New_Game: ' are known as a label. If you use a GOTO, you need a label.
So underneath where it says:
Pressed=Mouseclick()
Put ' If mouseclick()=1 then Goto New_Game '
(When using the GOTO command you don't include the ' : ' but you include it in the label)
Your code should now be:
WORDS$="New Game"
Pressed=0
x1=20
y1=55
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
Do
text x1,y1,WORDS$
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
If mouseclick()=1 then Goto New_Game
else
pressed = 0
endif
loop
New_Game:
Are you following me? Before you test this code out, underneath the ' New_Game: ' label, put the command ' CLS '. ' CLS ' actually means
Clear
Screen. It does exactly what it says, it clears the screen!
Now, Underneath that, put the command ' Print "Get Ready to play"; '
You see the ' ; ' ? That means it'll wait, so underneath the Print command, put ' Wait 2000 '.
Test it out, and wala! You have your start game button! Feel Free to position it anywhere you would like, because we'll be adding more buttons.
Your Final Code:
WORDS$="New Game"
Pressed=0
x1=20
y1=55
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
Do
text x1,y1,WORDS$
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
If mouseclick()=1 then Goto New_Game
else
pressed = 0
endif
loop
New_Game:
cls
Text 240,220,"Get Ready to Play";
wait 2000
----------------------------------------------------------------------
*Tutorial 10 - The Text Menu
For this lesson, we will add more then just ' New Game ' to make a complete menu. We will also learn so many other things that are important. This is a well packed lesson.
You might be thinking ' It took so much code for just one button, new we are gonna do more? '. Well, there is a solution. You have learned different functions like mousex(), and str$(). Now, we are going to be able to make our own function! It'll be a button function, of course.
First off, to make a function, you need a function header. It looks like this:
function FUNCTIONname(parameters)
The word ' function ' in the beginning is always the same, stating that it is a function, so never change that. The ' FUNCTIONname ' or course, we will replace with the word ' Button ' because this is a button function. The parameters, we'll get to that later. (Functions should always be at the bottom of all code, so do all this at the bottom)
It should now be:
function Button(parameters)
Now, the only parameters we need are 3 things: The x1,y1,and WORDS$. This is only because its just like the command ' text ', and for a button, we need text
(WOOT second smiley!)
So this is now the code:
function Button(x1,y1,WORDS$)
Since we will have the ' words ' here, in the final code for the last tutorial, remove the ' WORDS$="New Game" ' and the
' text x1,y1,WORDS$ ' . This is because in the function, we will tell what the words are.
Now that you have a header, its time to put the code in the function. First off, take this code from the top, and put it under neath the header:
Pressed=0
x1=20
y1=55
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
I want you now to remove the:
x1=20
y1=55
This is because we are telling what x1 and y1 are in the function.
Now, from last's tutorials final code, remove this code and put it underneath what we just put in:
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
If mouseclick()=1 then Goto New_Game
else
pressed = 0
endif
Also, remove the ' If mouseclick()=1 then Goto New_Game ' because we will take care of that later. You code should now be:
Do
loop
New_Game:
cls
Text 240,220,"Get Ready to Play";
wait 2000
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
Well, thats actually all there is to make the function. Although, we must once again put ' text x1,y1,WORDS$ ' at the end of this. To really end this, add a ' endfunction pressed ' at the bottom. Thats saying that it will end the function when pressed is triggered. Although, you should always have at least a ' endfunction ' at the bottom of your function.
Now, to call the function to make it work. inside the do-loop, put
' Button(x1,y1,WORDS$) '. This is calling the function, by writing the name and the parameters. Now, to change the parameters to make it work. Change x1 to 20, and y1 to 55. Change WORDS$ to ' "New Game" '. It should look like this:
Button(20,55,"New Game")
Now, the use of a function, is to use another button, all you have to do is put it again. Underneath ' Button(20,55,"New Game") ', put that once again, but change y1 to 95. Also, change WORDS$ to ' "Load Game '
and inside the do-loop it should look like:
Button(20,55,"New Game")
Button(20,95,"Load Game")
Although, we want to do what happens when you click the ' New Game ' button, right? Change the ' Button(20,55,"New Game") ' to an If-Then statement by making it like this:
if Button(20,55,"New Game")=1 then
You see the ' =1 '? Thats saying when Pressed is triggered, because we have in the function ' endfunction Pressed '. So now, after ' then ', put ' Goto New_Game ' to once again go to the label. You code should so far be:
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
loop
New_Game:
cls
Text 240,220,"Get Ready to Play";
wait 2000
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
Since Load Game will not work because we didn't make it work yet, lets add another button, how about the ' Exit ' Button
(Its the third, but its starting to get lame to count it). So once again, inside the loop but after the Load Game button, put:
Button(x1,y1,WORDS$)
For an exersize, make that button say "Exit", and make it the same X coordinate, but 40 Y coordinates down. If you get stuck, look back at the tutorials to see how we did it before.
It should look like this:
Button(20,135,"Exit")
To change that to an if, it should look like this:
If Button(20,135,"Exit")=1 then
Understand? Now, to make it actually exit is simple. Just write the command ' End ' after the then. Wala! Your code should be:
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
loop
New_Game:
cls
Text 240,220,"Get Ready to Play";
wait 2000
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
Test it out, and you have a menu!
----------------------------------------------------------------------
*Tutorial 11 - Starting the 3D
Now that we have a menu, I think its time to start the 3D part of the RPG. First of all, you have to know, that whenever you have 3D, you need sync. Sync Is the thing in a game that refreshes the game, to make it run smoother. To set it on, all you have to do is ' Sync On ' at the top of your program! Easy, huh? (NEVER Put ' Sync On ' inside a loop!). Now, here is another command of sync:
' Sync Rate *rate '
All you have to do is replace the ' *rate ' with the sync rate. That means the rate it will refresh the screen at. So, for Maximum performance, we shall set the rate at 0.
So at the top of our final code from last tutorial, put:
Sync On : Sync Rate 0
You see that ' : ' in between those commands? It can be used between any commands, because it is a stacker. A stacker is what is used to stack commands next to each other. It saves room, which is useful for some people.
Now, we need to refresh the game inside of the loop. So in last tutorial's final code, with the Sync added to it, put this command RIGHT before the word ' loop ': ' sync '. This is telling the program to refresh at that period, which is good.
Remember when I told you that there where 4 kinds of loops? We learned two, its time to lean another. Its called : The Repeat-Until. This is very handy, as you can set parameters for when it should stop looping!
Example:
Repeat
*code
Until *parameter
Just replace the ' *parameter ' with the function or variable it will stop looping at. Another example:
Repeat
Print "Yay Third loop!
Until returnkey()=1
The function ' returnkey() ' is a function that responds the the return key (AKA the enter key). ' returnkey()=1 ' is stating that when the return key is pressed. So, remember this code?:
New_Game:
cls
Text 240,220,"Get Ready to Play";
wait 2000
Thats the New Game lable, right? Well now that we have sync, it won't work without a loop and a sync itself! This is where we use the Repeat-Until loop. But First, I must teach you another useful function. Its called ' scancode() ' What this does, is check if one of the keys on the keyboard is pressed. Every key has its own scancode. So ' scancode()>0 ' means that if any key is pressed, even the letters. Lets make ' scandcode()>0 ' the new Repeat-Until Parameter now. To get the New Game thing to work, we must take the code:
cls
Text 240,220,"Get Ready to Play";
wait 2000
and put it into the Repeat-Until in place of the ' *code '. Now, take out the ' cls ' and the ' Wait 2000 '. Your New Game code should look like this:
New_Game:
Repeat
Text 240,220,"Get Ready to Play";
Until scancode()>0
Add a ' sync ' before the ' Until scancode()>0 '. Add a ' cls ' Before the ' Repeat '.
Your code should now look like this in all:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
Now for the REAL 3D. First of all, we want a box for the player just for now. You can put objects like boxes, spheres, and stuff in place of real objects when you dont have a real object yet, like a house, or a player. To make a box, the command is simply:
' Make Object Box *obj,*x,*y,*z '
Confused, right? Well you never heard of the parameter ' *z ' before have you? Well, I've Drawn another image to help us:
http://www.solisearch.net/ims/pic.php?u=191602SZkT&i=244884
Do you understand this? I hope you do, cause if you dont it will be harder to understand. So lets get back to making the box. The command was: ' Make Object Box *obj,*x,*y,*z '. So lets make the x,y, and z size of the box. How about x is 4, y is 3, and z is 5. As for the
' *obj ', you must understand that it stands for the Object Number. Every object you have must have a number so you can operate it. Lets make this object number 1.
The command now looks like: ' Make Object Box 1,4,3,5 '. Put this command after:
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
Now, lets make it so we can once again wait to end the program, but this time by the ' returnkey()=1 ' instead. For an exercise, make the program wait untl you press the return key with a Repeat-Until, and add a sync. If you think you got it, it should look like this.
Repeat
sync
Until returnkey()=1
Add that code under the ' make object box 1,4,3,5 ' command.
And RIGHT before the ' function Button(x1,y1,WORDS$) ', you should put ' end ' You see, what this does, is end the program before it hits a function header.
Run this final code, you'll see you've made your first box!:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
make object box 1,4,3,5
Repeat
sync
Until returnkey()=1
end
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
----------------------------------------------------------------------
*Tutorial 12 - Organizing the Player
Now that you've made your first 3 Dimensional object, it's time to actually make that box, that little box, your main character (for now). Well, with a main character, you can't just stare at it the whole game. How about some movenment? Although we dont want it moving itslef. This is where a little thing called 'Player Input' comes in. Player Input is simple, really, Player Input is what ever the person playing the game inputs into the keyboard, or even the mouse. By that I mean that if you want the game to continue on, you are waiting for the players input of the keyboard ( wait key and scancode() ). Or if you want the main character to enter a building, you press the enter key ( returnkey() ). Understand?
So now we are going to work with player input, for moving the main character.
I am going to give you two choices, show you two ways of how you can setup keys for main character movement. The first way is the original 'up, left, down, and right arrow keys' movement. The second way is the
'W, A, S, D classic rpg' movement. Lets start with arrow keys first.
The arrow keys on your keyboard all have their own little special function for player input. Yay functions! They are very simple to read, and very simple to understand. The up arrow would be 'upkey()', the left arrow would be 'leftkey()', down arrow would be 'downkey()', and right arrow would be 'rightkey()'. Now, to tell if one of these are pressed, you need an IF statment. remember these old things?
So this is what it would look like: 'If Upkey()' . What this means is if the up arrow is pressed. But what if you want to detect if the up arrow is not pressed? Uh-oh, here come the parameters. Instead of
'If Upkey()' we can have it equal a parameter like so: 'If Upkey()=' .
There can be two parameters. 'If Upkey()=1' Is the same thing as
'If Upkey()', it is checking if the player presses the up arrow. Although, 'If Upkey()=0' is checking if the player is
not pressing the up arrow, which could be useful in some cases. So lets just use 'If Upkey()=1' for the sake of parameters.
Although you must remember, at the end of an IF statement without the 'then' you must always have an 'endif'. So here it is folks:
If Upkey()=1
endif
Not so exciting yet, because we didn't make it do anything. Thats the next part.
*Drum Rolls Please!* Dum-dum dum-dum-dum dum-duuum! Ladies and gentlemen, Nerds and Geeks, Losers and Wackos of all ages! WE LEARN A NEW COMMAND! WOOT!
Ok, so yeah. We get to learn a new command. Not a new function, a new command! This command, this precious command, isn't some lame thing like 'Print', Nor is it anything like 'Ink'!
It's 'Move Object *obj, *speed ' !!! yay! This command, makes the object move forward! The object that you move is specified in the parameter '*obj', and the speed it moves at is specified in the parameter '*speed' ! Alright it wasn't that exciting, but I just felt like doing that...
. Ok, so our object number for the main character is 1 so replace '*obj' with 1. The speed can be something as simple as 2, so replace '*speed' with 2. Put that entire new command inside of the IF statement, and it should ook like this:
If Upkey()=1
Move Object 1,2
endif
Now that we've got moving forward covered, we need moving backwords. So copy that entire IF statement and paste another one below it. Change 'If Upkey()=1' to 'If Downkey()=1' and change 'Move Object 1,4'
to 'Move Object 1,-4'. We changed it to down key because this is for moving backwords. We changed the '*speed' of 4 to a speed of -4 because if 4 made us go forward, than -4 will make us go backwrods logically, right? Should look like this:
If Upkey()=1
Move Object 1,2
endif
If Downkey()=1
Move Object 1,-2
endif
Next, since we have arrow key movement, left and right will turn the main character left and right. This is another new command, and its just as simple. It's 'Turn Object Left *obj, *speed' and
'Turn Object Right *obj, *speed'. '*obj' Of course is 1, and '*speed' can be... how about 2 (or 3, whatever suits you). So once again make IF statements and put the 'leftkey()' and 'rightkey()' functions. Put the turning commands for the right functions and your coe should look like this:
If Upkey()=1
Move Object 1,2
endif
If Downkey()=1
Move Object 1,-2
endif
If Leftkey()=1
Turn Object Left 1,2
endif
If Rightkey()=1
Turn Object Right 1,2
endif
Well there is your movement code so far! Your latest RPG code was:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
make object box 1,4,3,5
Repeat
sync
Until returnkey()=1
end
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
-Right? So you see where it says:
Repeat
sync
Until returnkey()=1
Under the 'New_Game' label? This is where we will have the main game code, so now I'm gonna test you. Change that 'Repeat-Until' loop into a 'Do-Loop' loop and it should look like this:
Do
sync
Loop
Now, Put the movement code with the movement input functions and IF statements right above where it says 'sync' because we want to sync the program at the end of each loop. Now put all that code back into your RPG and it should look like this:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
make object box 1,4,3,5
Do
If Upkey()=1
Move Object 1,2
endif
If Downkey()=1
Move Object 1,-2
endif
If Leftkey()=1
Turn Object Left 1,2
endif
If Rightkey()=1
Turn Object Right 1,2
endif
sync
Loop
end
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
Ok, so run that code, and you can move the box! (Start with moving it forward so your can see it if you move it backwards. Great isn't it! But, ouch, it moves out of sight sometimes! We can fix that by making the camera always face the object(for now)! So the command is as easy as 'Point Camera *x, *y, *z' You remeber the 3 parameters for the axis' of 3D space? E Y and Z! These can easily be represented by three cool new functions, 'object position x(*obj)',
'object position y(*obj)', and 'object position z(*obj)'! We can of course replace *obj with 1, the object number of our main character and the command will look like this:
Point Camera object position x(1),object position y(1),object position z(1)
Put that in your main program loop right under the movement and right above the sync. Next, we want something as a ground that we can see that we are moving on. This will be called a 'matrix'. A matrix is pretty much a grid with tiles, and these tiles can be moved around to make the shape of a ground. But this is only a ground for now. The command is 'Make Matrix *matrix, *width, *height, *xSeg, *zSeg'. Pretty complicated huh? Well first of all, matrix' have their own set of numbers because they are not referred to as objects. SO we CAN make this matrix matrix number 1. Replace '*matrix' with 1. Next, lets look at '*width' and '*height'. Width would pretty much be how big you want it on the x axis and Height is how big you want it on the x axis (even though it sounds like the y axis). Lets make the width and the height both 200. So now lets go to '*xSeg' and '*zSeg'. These are how many tiles you want in the matrix, so lets make both of these 5. Since there are 2 fives, there will be 25 tiles in this matrix (5 times 5). Ok, so lets put this entire command under where you make the main character box object and it should look like this:
make matrix 1,200,200,5,5
Now, we want the player to be in the middle of the matrix, so we position the matrix with 'Position Matrix *matrix, *x, *y, *z'. Since we want the player to be in the middle, we are going to move the matrix. The matrix we have is number 1. If the matrix is 200 in width (*x) and 200 in height (*z) the the middle of that would be 100 *x and 100 *z. Although, since the matrix is at coordinates 0 *x and 0 *z we must move it 100 spaces back, so the coordinates for '*x' and '*z' will be -100. Leave '*y' at 0, because we dont want to move it up or down. The entire command will look something along the lines of:
Position Matrix 1,-100,0,-100
Put that under 'Make Matrix' and your entire and final code should look like this:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
make object box 1,4,3,5
make matrix 1,200,200,5,5
Position Matrix 1,-100,0,-100
Do
If Upkey()=1
Move Object 1,2
endif
If Downkey()=1
Move Object 1,-2
endif
If Leftkey()=1
Turn Object Left 1,2
endif
If Rightkey()=1
Turn Object Right 1,2
endif
Point Camera object position x(1),object position y(1),object position z(1)
sync
Loop
end
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
Compile it, and wa-la! It's beautiful!
----------------------------------------------------------------------
*Tutorial 13 - Organizing the Player 2
Well, having the camera in one place is kind of dull for an rpg, dont you think? Otherwise, you would have to go through finding the perfect camera positions and angles for each scene in the game. So how about we just make it 3rd Person? Its pretty easy, so here it goes.
There are some commands I would like to get out of the way before we get started.
I hope you remember :
object position x(1) object position y(1) object position z(1)
Because those are very useful functions. They get the x, y, and z position of the specified object. Very useful, as we will use them alot.
Now, there is :
object angle x(1) object angle y(1) object angle z(1)
And they get the angles of each axis for the specified object. Understand?
Ok, so you know those. There is first, one thing we should do, one thing we should change before we move on. You remember turning left and right? That was interesting to learn, wasn't it? We used 'Turn object right' and 'Turn object left'. Well, we need to change that, if you want 3rd person camera in your RPG. We need to change it to a variable. You see, all we have to do is increase, and decrease this variable. This will be variable 'a#'. Now, all you have to do is replace:
Turn object left 1,2
With:
Dec a#,2
But as you see that is only for the left turning. Here is a little test. How would you change turning on the right?
Ok, if you think you got it, what you had to do was replace:
Turn object right 1,2
With:
Inc a#,2
You might not understand, but see what we do next. After the 'endif' in turning right, add this command:
YRotate Object *objnum,*value
What that command does is rotate the specified object on the Y axis in 3D space, by the specified value. That is, ofcourse, turning. So, replace the '*objnum' with '1' and the '*value' with 'a#'. Is it making more sense? When you click the 'leftkey()', it decreases 'a#', making it so it rotates you less (to the left) on the Y axis. And its vice-versa for the 'rightkey()'. Your turning should look like this:
If Leftkey()=1
Dec a#,2
endif
If Rightkey()=1
Inc a#,2
endif
yrotate object 1,a#
Understand?
Now, all we need to do is math. Math, and camera commands. First of all, after the turning code, we need to declare two variables that use previous variables, functions, and trigonometry. We will declare the variables 'posx#' and 'posz#'. So lets set that up.
posx#=
posz#=
Now, we must add the trigonometry functions.
posx#=cos( )
posz#=sin( )
I'm sure you know what these do? We have to enter an angle in there, between the parenthesis. Those functions find the cosine and sine of the angle we enter. This is what we should enter:
posx#=cos(270-a#)
posz#=sin(270-a#)
Well, you probably understand that we have 'a#' in there for the players Y rotation. But why the 270? Well in third peron's view, we have to look in the characters' line of sight, so we must use that 270 to do that.
Now we have to add the object position x(1), and object position z(1) to the end of the functions with addition. (Note the pos'x' and the pos'z') This is so we know that it will be behind the character, the specified object number 1.
posx#=cos(270-a#) + object position x(1)
posz#=sin(270-a#) + object position z(1)
But if we used that, it would be first person. *Aha, thats something to think about later on!*. But we want a third person rpg... right?
So we just have to add some simple multiplication in between.
Put ' * 30' before the addition sign. That is to move the camera farther back behind the character.
posx#=cos(270-a#) * 30 + object position x(1)
posz#=sin(270-a#) * 30 + object position z(1)
Well, there we are. We have are super duper veriables! (Ok, I wont say that again) Time for two simple camera commands, that you already know! They are 'Position Camera *x,*y,*z' and 'Point Camera *x,*y,*z'!
For position camera, replace the '*x' with 'posx#' and the '*z' with 'posz#'. And replace the '*y' with 'object position y(1)+6' so we are higher than the characters eyes.
On to Point camera, replace the '*y' with 'object position y(1)+6' so we are looking higher than the characters eyes' line of sight. Finally, replace the '*x' with 'object position x(1)' and the '*z' with 'object position z(1)'. Your camera code should look like this:
posx#=cos(270-a#) * 30 + object position x(1)
posz#=sin(270-a#) * 30 + object position z(1)
Position Camera posx#,object position y(1)+6,posz#
Point Camera object position x(1),object position y(1)+6,object position z(1)
*YOU can change the 30 in the two variables to change hor far in and out you zoom. But always keep them the same as each other!*
And here is you final code:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
make object box 1,4,3,5
make matrix 1,200,200,5,5
Position Matrix 1,-100,0,-100
Do
If Upkey()=1
Move Object 1,2
endif
If Downkey()=1
Move Object 1,-2
endif
If Leftkey()=1
Dec a#,2
endif
If Rightkey()=1
Inc a#,2
endif
yrotate object 1,a#
posx#=cos(270-a#) * 30 + object position x(1)
posz#=sin(270-a#) * 30 + object position z(1)
Position Camera posx#,object position y(1)+6,posz#
Point Camera object position x(1),object position y(1)+6,object position z(1)
sync
Loop
end
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
----------------------------------------------------------------------
*Tutorial 14 - Timer Based Movements
I bet you're thinking "What the heck is this" ? Well, skipping this tutorial is a bad idea- this tutorial is focusing on the performance of your games.
The 'timer()' function that is in DBPro is a function that gets the time in DBPro's time format. Its always changing depending on the time.
Timer Based Movements is a system that uses the 'timer()' function in DBPro to get speeds for your players to move. You're thinking, "Well whats the point? I can just put any number!" But you're wrong. Say you move your player around the world at a speed of 5. Its perfect on your computer. But on someone else's better computer computer, a speed of 5 could be like 20. That would be bad. Everything would be too hard to control.
Now, performance is different depending on your computer. But there is always one thing that you can rely on to be the same. And yeah, its the time. Thats why people use Timer Based Movements. It'll be about the same speed on every computer. Now let's start.
Here is the full code from the last tutorial:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
make object box 1,4,3,5
make matrix 1,200,200,5,5
Position Matrix 1,-100,0,-100
Do
If Upkey()=1
Move Object 1,2
endif
If Downkey()=1
Move Object 1,-2
endif
If Leftkey()=1
Turn Object Left 1,2
endif
If Rightkey()=1
Turn Object Right 1,2
endif
Point Camera object position x(1),object position y(1),object position z(1)
sync
Loop
end
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
I'm going to use an example from my good friend Cash Curtis.
Now, we have to use variables to contain the speed. This way, it can change. Variables change, get it... the root word is 'vary' which means change
So above the 'Do' command in your 'Do-Loop', post this:
movespeed = 75
turnspeed = 87.5
time=timer()
'Movespeed' is the speed your player will move at.
'Turnpeed' is the speed your player will turn at.
'time' is the variable containing the number received from the 'timer()' function I talked about. Ok, we are almost done.
After the 'Do', put this code:
gameTime=timer()-time
MSframe#=gameTime*(movespeed/1000.0)
TSframe#=gameTime*(turnspeed/1000.0)
Now, we are defining a new variable; 'gametime'. Thats the elapsed time between 'before we started the game' and 'the current time'.
The next two are the actual speeds we find out by doing a little math. 'MSframe#' stands for 'move speed frame'. The same goes for 'TSframe#', except its 'turn speed frame'.
Ok, only 2 more steps.
The first one, is changing your speed values to the variables we declared. Replace your movement code with the following:
If Upkey()=1
Move Object 1,MSframe#
endif
If Downkey()=1
Move Object 1,-MSframe#
endif
If Leftkey()=1
Dec a#,TSframe#
endif
If Rightkey()=1
Inc a#,TSframe#
endif
yrotate object 1,a#
As you can see, all I did is replace the speeds with the variables. Easy stuff
The last step (finally), is re-defining the timer variable. Put this right
before the 'sync' in your do-loop:
time=timer()
Wa-la... there you have it. You have Timer Based Movements. Adjust the speeds in the variables we declared before the 'DO' to change speed.
Your final code should be:
Sync On : Sync Rate 0
Do
if Button(20,55,"New Game")=1 then Goto New_Game
Button(20,95,"Load Game")
If Button(20,135,"Exit")=1 then End
sync
loop
New_Game:
cls
Repeat
Text 240,220,"Get Ready to Play";
sync
Until scancode()>0
make object box 1,4,3,5
make matrix 1,200,200,5,5
Position Matrix 1,-100,0,-100
movespeed = 75
turnspeed = 87.5
time=timer()
Do
gameTime=timer()-time
MSframe#=gameTime*(movespeed/1000.0)
TSframe#=gameTime*(turnspeed/1000.0)
If Upkey()=1
Move Object 1,MSframe#
endif
If Downkey()=1
Move Object 1,-MSframe#
endif
If Leftkey()=1
Dec a#,TSframe#
endif
If Rightkey()=1
Inc a#,TSframe#
endif
yrotate object 1,a#
posx#=cos(270-a#) * 30 + object position x(1)
posz#=sin(270-a#) * 30 + object position z(1)
Position Camera posx#,object position y(1)+6,posz#
Point Camera object position x(1),object position y(1)+6,object position z(1)
time=timer()
sync
Loop
end
function Button(x1,y1,WORDS$)
Pressed=0
x2=Text Width(WORDS$)
y2=Text Height(WORDS$)
if mousex()>x1 and mousex()<x1+x2
if mousey()>y1-y2 and mousey()<y1+y2
Pressed=1
endif
endif
if pressed=1 then ink rgb(255,0,0),0 else ink rgb(255,255,255),0
if pressed=1
Pressed=Mouseclick()
else
pressed = 0
endif
text x1,y1,WORDS$
endfunction pressed
This is all of this tutorial so far.. what do you think about my progress?