Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / World Tutorial

Author
Message
Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 22nd Jan 2007 20:43 Edited at: 4th Mar 2007 09:50
************************************
********** 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.



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.



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$)



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.



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



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”



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



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



*************************************
********** 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



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.



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.



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.



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



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()



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”



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:



**************************************
********** 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.



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.



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()



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”



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#”



Now lets change our main program loop. It should now be



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



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

Big Man
19
Years of Service
User Offline
Joined: 4th Feb 2005
Location: BEHIND YOU!!!! (but I live in England)
Posted: 22nd Jan 2007 22:27
nice...very detailed and you do a good job of explaining things I think this could be useful for newbs

bm

Our aim is to keep the loo's clean, your aim can help.

Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 22nd Jan 2007 23:24 Edited at: 4th Mar 2007 09:50
ty

Aaron Miller
18
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 23rd Jan 2007 01:54 Edited at: 23rd Jan 2007 01:55
Nice. Very good idea, and good minded. Oh yes, only kieran will get this:
lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol

Ha!

-db

lol

Your signature has been erased by a mod because it's larger than 600x120
Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 23rd Jan 2007 01:57 Edited at: 4th Mar 2007 09:50
lol DB user 2006+ keeps crashing my computer with LOL's

EDIT: thanks for the feedback and lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol lol

OneShot
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location:
Posted: 23rd Jan 2007 07:45
O-m-g...finally someone who explains a menu in detail besides that RPG one...I think this should be a sticky...imo...im noob though

Just started! Help!!!!
Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 23rd Jan 2007 10:36 Edited at: 4th Mar 2007 09:51
thanks oneshot if you want I'll post the next tutorial soon

Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 23rd Jan 2007 11:25 Edited at: 4th Mar 2007 09:51
World Tutorial Part II

Moved to top post...

EDIT: the attachment is the media for the tutorial so it has to stay

Attachments

Login to view attachments
Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 24th Jan 2007 00:19 Edited at: 4th Mar 2007 09:51
Part III is done now. check first post for it

Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 31st Jan 2007 04:19 Edited at: 4th Mar 2007 09:51
anyone else using it?

Kieran
18
Years of Service
User Offline
Joined: 6th Aug 2006
Location: Hamilton, New Zealand
Posted: 9th Feb 2007 21:24 Edited at: 4th Mar 2007 09:49
[moved to first post]

Login to post a reply

Server time is: 2024-11-26 21:30:49
Your offset time is: 2024-11-26 21:30:49