************************************
********** World Tutorial Part I **********
************************************
NOTE: This tutorial was designed for DarkBASIC pro and may not work in DarkBASIC but I don’t discourage you to try it in DarkBASIC classic.
In Part one we will cover the following aspects:
How to create a Menu
How to use Functions
Please open up DarkBASIC pro.
Ok first we will set up our menu using a function. Functions are always made at the bottom of the page in DarkBASIC. To create a function we use the commands “Function” and “Endfunction” to make a function for our menu we must first create the menu to put inside the function. Lets start with the menu’s buttons
Using “Function” to open the function, we will call the function Menu_Buttons and we will have the parameters x1,y1 and text$ for it. Text$ is a variable which will store the text you want for the button.
Function Menu_Buttons(x1,y1,text$)
Endfunction
We will also need to put something in the function, we will set x2 to the width of the text and y2 to the height of the text so we can use it to find the width and height of the text when we need to determine where the mouse needs to click to activate the button.
Function Menu_Buttons(x1,y1,text$)
x2 = text width(text$)
y2 = text height(text$)
Endfunction
Now we need to get it to display the text that we want the button to have so we use the command “Text” which we will use the parameters for our function to determine where to position it and what text to use. (x1,y1,text$)
Function Menu_Buttons(x1,y1,text$)
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
Now we need to determine “if the mouse is over the button and the mouse is clicked, load the button” to do so we use the mousex() and mousey() commands to get the co-ordinants of the cursor and we use the variables x1,y1,x2 and y2 to determine where the button is.
Function Menu_Buttons(x1,y1,text$)
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
if mousex()>x1 and mousex() <x1+x2 and mousey()>y1-y2 and mousey()<y1+y2 and mouseclick()=1
`activate button
endif
endfunction
The part in the code that says “`activate button” has a ` at the start which makes it a comment and won’t be executed as a piece of code.
Now we have our button function, lets use it to use it we type the name of the function and then the parameters, so lets say we want a new game button at position 100,200
Menu_Buttons(100,200,”New Game”)
Its as simple as that! Now to finish our menu lets make another function to load the menu called “Load_Menu” this will have no parameters, it will set the foreground color to red and the background color to black using the “Ink” command, then it will make the new game button, also we will use a repeat, until loop. In this one we will make it loop until the player presses escape, which to determine when they press it we use “Repeat, Until Escapekey()=1”
Function Load_Menu()
Ink RGB(255,0,0),RGB(0,0,0)
Repeat
Menu_Buttons(100,200,"New Game")
Until escapekey()=1
Endfunction
You will notice that after the “ink” function it has “RGB(255,0,0),RGB(0,0,0)” RGB is what we use for colors, the numbers are the red value, the green value, and the blue value. But why is there two RGB’s in the code? Simple its because the first RGB(255,0,0) sets the red value of the foreground color to 255 (max) and the other colors to 0 (min) then the second RGB(0,0,0) sets all the values of the background color to 0 which is black.
Now We will call our Menu function to load the menu.
I would just like to say I like to keep code clean so I can find stuff easily this is what I use to separate my functions from the code itself
REM *******************************
REM ********** FUNCTIONS **********
REM *******************************
All it does is show as a comment because the world “REM” is beside it which stands for REMARK.
Well that’s the end of
Part I your code should show
`Load the Menu
Load_Menu()
REM *******************************
REM ********** FUNCTIONS **********
REM *******************************
Function Menu_Buttons(x1,y1,text$)
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
if mousex()>x1 and mousex() <x1+x2 and mousey()>y1-y2 and mousey()<y1+y2 and mouseclick()=1
`activate button
endif
endfunction
Function Load_Menu()
Ink RGB(255,0,0),RGB(0,0,0)
Repeat
Menu_Buttons(200,100,"New Game")
Until escapekey()=1
Endfunction
*************************************
********** World Tutorial Part II **********
*************************************
This part of the tutorial requires some media, get it here
http://forumfiles.thegamecreators.com/?i=1102210
In
Part II we will cover the following aspects:
More Tidy code organisation
How to create our first 3D objects
Now I want to start this by telling you how important it is to keep tidy code. A while ago I used to not code this way and it would take me like 5 minutes to find anything. Now I organised it using functions and splitters.
Here I have used splitters (as I call them) to separate the code into 3 parts:
-The Setup
-The Main Loop
-The Functions
REM *******************************
REM ************ SETUP ************
REM *******************************
Load_Menu()
REM *******************************
REM ********** MAIN LOOP **********
REM *******************************
REM *******************************
REM ********** FUNCTIONS **********
REM *******************************
Function Menu_Buttons(x1,y1,text$)
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
if mousex()>x1 and mousex() <x1+x2 and mousey()>y1-y2 and mousey()<y1+y2 and mouseclick()=1
`activate button
endif
endfunction
Function Load_Menu()
Ink RGB(255,0,0),RGB(0,0,0)
Repeat
Menu_Buttons(200,100,"New Game")
Until escapekey()=1
Endfunction
This way there are easy-to-notice signs telling you which part of the code you are at.
Now I have cleared that up lets get down to business.
First of all we need to exit out of the menu and into the main program loop once we have clicked on the “New Game” button. To do so we change the “Repeat, Until” loop into a “Do, Loop” loop. A Do loop works with two commands “Do” and “Loop”
Do starts the loop then it executes all the code below Do until it gets to Loop then it goes Back to where do starts and executes all the code again until the “Exit” Command is used.
Now lets go to our Load_Menu() Function and change the type of loop.
Function Load_Menu()
Ink RGB(255,0,0),RGB(0,0,0)
Do
Menu_Buttons(200,100,"New Game")
Loop
Endfunction
Now we need a way to exit the loop so we change the remarked bit of the Function Menu_Buttons(x1,y1,text$), to set a variable to 1 when clicked on. We will use the variable “Pressed” to determine if the mouse was pressed. We need to set the variable to a Global Variable otherwise it wont work outside the function Menu_Buttons because it will be local only to the function. We will set it to global when the function is called so we change the function Menu_Buttons to include this.
Function Menu_Buttons(x1,y1,text$)
Global Pressed
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
if mousex()>x1 and mousex() <x1+x2 and mousey()>y1-y2 and mousey()<y1+y2 and mouseclick()=1
Pressed = 1
endif
endfunction
Now when the button is clicked it sets Pressed to 1. now we need an If statement in the Function Load_Menu() that says “If Pressed = 1 then exit the loop” which is what we do, use the function exit.
Function Load_Menu()
Ink RGB(255,0,0),RGB(0,0,0)
Do
Menu_Buttons(200,100,"New Game")
If Pressed = 1
Exit
Endif
Loop
Endfunction
You must end the if statement with endif otherwise it will not compile properly and will crash. Now when it exits the menu loop it will go to the game itself (the main loop.)
Now lets go to the main loop and create a sphere to be our player.
Your code so far should be
REM *******************************
REM ************ SETUP ************
REM *******************************
Load_Menu()
REM *******************************
REM ********** MAIN LOOP **********
REM *******************************
REM *******************************
REM ********** FUNCTIONS **********
REM *******************************
Function Menu_Buttons(x1,y1,text$)
Global Pressed
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
if mousex()>x1 and mousex() <x1+x2 and mousey()>y1-y2 and mousey() <y1+y2 and mouseclick()=1
Pressed = 1
endif
endfunction
Function Load_Menu()
Ink RGB(255,0,0),RGB(0,0,0)
Do
Menu_Buttons(200,100,"New Game")
If Pressed = 1
Exit
Endif
Loop
Endfunction
So between MAIN LOOP and FUNCTIONS we put the main loop, which is a do loop. We will use the command “Make object sphere” to create a sphere. We will use object number 1 and size 50 for it so it will be “Make object sphere 1,50” We should put the media loading into a function so lets create the function Load_Media()
Function Load_Media()
Make object sphere 1,50
Endfunction
But that’s just a stupid grey sphere! So lets add some color! In the media pack there is a texture named “spheretexture.jpg” we will load it and texture our sphere with it. So inside the folder with the code saved in it create another folder called media and load the media from mediapack1 into it.
Now we use the commands “Load image” and “texture object” to make the player pretty
we want to load the image as image number 1 so we use “Load image "Media/spheretexture.jpg",1” we want to texture the player (object number 1) with the sphere texture (image number 1) so we use “texture object 1,1”
Function Load_Media()
Make object sphere 1,50
Load image "Media/spheretexture.jpg",1
Texture object 1,1
Endfunction
Now we call the load media outside of a loop so it only loads the media once. Put it directly underneath the Load_Menu() function so that when it exits it loads media then goes to the main loop.
Now in the main program loop lets just put a remark and get it to end the program after a key is pressed using “suspend for key” which stops the code from continuing until a button is pressed then put “end” which ends the program.
That’s the end of
Part II
Your Final code should show:
REM *******************************
REM ************ SETUP ************
REM *******************************
Load_Menu()
Load_Media()
REM *******************************
REM ********** MAIN LOOP **********
REM *******************************
Do
`Main Loop Here
suspend for key
end
Loop
REM *******************************
REM ********** FUNCTIONS **********
REM *******************************
Function Menu_Buttons(x1,y1,text$)
Global Pressed
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
if mousex()>x1 and mousex() <x1+x2 and mousey()>y1-y2 and mousey()<y1+y2 and mouseclick()=1
Pressed = 1
endif
endfunction
Function Load_Menu()
Ink RGB(255,0,0),RGB(0,0,0)
Do
Menu_Buttons(200,100,"New Game")
If Pressed = 1
Exit
Endif
Loop
Endfunction
Function Load_Media()
Make object sphere 1,50
Load image "Media/spheretexture.jpg",1
Texture object 1,1
Endfunction
**************************************
********** World Tutorial Part III **********
**************************************
Now in
Part III we will cover the following aspects:
How to move the player
Using Sync
First we need to set the refresh rate if we want our player to be able to move and have the screen updated in the loop. We use the command “sync on” to turn the sync on and “sync rate” to set the refresh rate. Lets set it to 60 frames per second so in the function Load_Menu we will add this.
Function Load_Menu()
Sync on
Sync rate 60
Global a# = 0
Ink RGB(255,0,0),RGB(0,0,0)
Do
Menu_Buttons(200,100,"New Game")
If Pressed = 1
Exit
Endif
Sync
Loop
Endfunction
You may have noticed that I also added another piece of code, Global a# = 0. It is a variable we will use later to turn the object.
We also must use “Sync” at the end of every loop so here is the loop we need to change
Sync on : Sync rate 60
Global a# = 0
Ink RGB(255,0,0),RGB(0,0,0)
Do
Menu_Buttons(200,100,"New Game")
If Pressed = 1
Exit
Endif
Sync
Loop
Endfunction
Notice how it Refreshed(syncs) at the end of every time it loops? That’s so it updates every time on the screen, if we didn’t do this while sync was on it would show a black screen.
Ok lets use another function to move our player around because a still player is just lame. We will call the function “Move_Player_With_Arrowkeys(ObjectNumber,Speed).”
Now we will use “Move Player” to make the player move forward and backward and “Yrotate object” to turn it.
The command “Move Player” ends with an object number and the speed. We use the parameter ObjectNumber from the function to get the number of the object we want to move. To move the object forward at the parameter Speed we use “Move Object ObjectNumber,Speed” which will move the player forward at the speed. We will also use an If statement so if the player pressed up then it can move forward.
Function Move_Player_With_Arrowkeys(ObjectNumber,Speed)
If upkey()=1 then Move Object ObjectNumber,Speed
Endfunction
You will notice that I said always to use an endif after functions but I used a “THEN” so that I won’t have too. Endifs are easier if you want to have the if statement doing multiple commands. I also used “If Upkey()=1” because if the upkey is pressed it returns a 1.
But that only moves forward so lets make a move backward piece of code. We do the same thing as the forward except we use a negative number so instead of moving forward, or not moving at speed 0 we use a negative. We will use “Move Object ObjectNumber,-Speed” which will have a negative number. Also we use downkey() instead of upkey()
Function Move_Player_With_Arrowkeys(ObjectNumber,Speed)
If upkey()=1 then Move Object ObjectNumber,-Speed
If downkey()=1 then Move Object ObjectNumber,-Speed
Endfunction
Now we want the player to be able to turn left and right. To do this we could use the command “turn object left” and “turn object right” but we need to use the variable a# for the camera later on so we will use the command “Yrotate object” which will rotate the object on the Y axis, which will turn the sphere around.
To do this we use “INC” and “DEC” the command INC will increase the variable given by the amount given. We use “Inc a#,Speed” which increases the variable a# by the speed that we set. That will turn us right. To turn left we use “DEC” to decrease a# by the speed “Dec a#,Speed”
Function Move_Player_With_Arrowkeys(ObjectNumber,Speed)
If upkey()=1 then Move Object ObjectNumber,-Speed
If downkey()=1 then Move Object ObjectNumber,-Speed
If leftkey()=1 then Dec a#,Speed
If rightkey()=1 then Inc a#,Speed
Endfunction
Now we have to Yrotate the Player by the variable a#. when it is increased the amount it rotates gets greater and as it decreases the amount it rotates the other way gets greater. So we use “Yrotate object ObjectNumber,a#”
Function Move_Player_With_Arrowkeys(ObjectNumber,Speed)
If upkey()=1 then Move Object ObjectNumber,-Speed
If downkey()=1 then Move Object ObjectNumber,-Speed
If leftkey()=1 then Dec a#,Speed
If rightkey()=1 then Inc a#,Speed
Yrotate object ObjectNumber,a#
Endfunction
Now lets change our main program loop. It should now be
Do
Move_Player_With_Arrowkeys(1,2)
Sync
Loop
The parameters for Move_Player_With_Arrowkeys are 1 and 2, which means its using object number 1 (the player) and the speed 2. That’s the end of
Part III your code should now be
REM *******************************
REM ************ SETUP ************
REM *******************************
Load_Menu()
Load_Media()
REM *******************************
REM ********** MAIN LOOP **********
REM *******************************
Do
Move_Player_With_Arrowkeys(1,2)
Sync
Loop
REM *******************************
REM ********** FUNCTIONS **********
REM *******************************
Function Menu_Buttons(x1,y1,text$)
Global Pressed
x2 = text width(text$)
y2 = text height(text$)
text x1,y1,text$
if mousex()>x1 and mousex() <x1+x2 and mousey()>y1-y2 and mousey()<y1+y2 and mouseclick()=1
Pressed = 1
endif
endfunction
Function Load_Menu()
Sync on : Sync rate 60
Global a# = 0
Ink RGB(255,0,0),RGB(0,0,0)
Do
Menu_Buttons(200,100,"New Game")
If Pressed = 1
Exit
Endif
Sync
Loop
Endfunction
Function Load_Media()
Make object sphere 1,50
Load image "Media/spheretexture.jpg",1
Texture object 1,1
Endfunction
Function Move_Player_With_Arrowkeys(ObjectNumber,Speed)
If upkey()=1 then move object ObjectNumber,Speed
If downkey()=1 then move object ObjectNumber,-Speed
If Leftkey()=1 then dec a#,Speed
If rightkey()=1 then inc a#,Speed
Yrotate object ObjectNumber,a#
Endfunction
The camera does not move yet which we will cover in the next tutorial.
if i get more feedback im happy to add to this