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 / TUTORIAL: Simple Dodging Game (No Media Required)

Author
Message
Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:35 Edited at: 29th Jun 2009 23:49
EDIT: Ok. I've posted the entire tutorial, and just realized that I spelled Dodging wrong in the thread title. Whatever, I hope this helps people.

Introduction:

When you get Dark Basic Pro, the first thing you want to do is get your first game up and running to show to other people; but this can be a daunting task. This tutorial will help you get a simple, but functional game running in a very short time span, and hopefully teach you some basic coding in Dark Basic Pro. This game can be created using only the commands that come with Dark Basic Pro, and no external media. No official or third-party plugins are necessary, and you do not need to download anything.

Game Concept:

The basic idea behind the game is that you control a box at one end of a platform, and you must avoid other boxes moving towards you from the other side of the platform by moving left or right. You’ll start out with 100 health, and every time you get hit, you lose 20 health, allowing for 5 hits before a game over. The boxes coming at you will have varying sizes and speeds. Every box you successfully dodge will add one to your score.

--------------------The Coding-----------------------------------------------------------------------------------------

NOTE:
You do not have to write your code exactly the way I write mine, but I find that code looks neater and easier to read if it is in all capital letters, and it also sets it apart from normal text. I always right my commands in all-caps, and variable names selectively capitalized. You are free to write things in ALL CAPS, all lowercase, Or AnY StUPid ComBInATiOn YOu PLeAsE.

Part 1: Set Up

This part of the code will just set up the screen options and get us ready for coding the actual game. Open up the Dark Basic Pro IDE and type in:



The SET WINDOW TITLE command sets what text appears on the tab in the taskbar and program window.

SET WINDOW OFF makes the game run in fullscreen mode.

SET DISPLAY MODE sets the screen resolution.

The SYNC on and SYNC RATE commands force the program to run at a constant frame rate, 60 in this case.

HIDE MOUSE makes the mouse invisible while in the bounds of our program, and AUTOCAM OFF stops DBP from positioning the camera to point at a new object when it is created.

The DO ... LOOP command is the games main loop. All code inside the loop is executed infinitely, so a lot of the games code goes inside these to commands. The SYNC line directly before LOOP tells the program to redraw the scene to the screen.

The POSITION MOUSE command inside the loop continuously positions the mouse in the center of the screen. This is really only important in fullscreen applications if you have more than one monitor and don’t want the mouse going over to the other screen while the application is running.

Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:37
Part 2: The Landscape and Player

In this part, we will put the playing field on the screen, as well as putting in the player character and allowing the player to move him around.

Go down a line or two under the AUTOCAM OFF command and type in



The first MAKE OBJECT BOX command creates the box that will serve as the ground/playing field. I have given it such an unreasonably high object number (9999) so it will not get confused with any object that is mentioned in other parts of the code. The ground will be 75 units on the x-axis, 5 units on the y-axis, and 200 units on the z-axis. The COLOR OBJECT command will give it a kind of tannish color. The RGB part of that command is where you specify the Red, Green, and Blue (RGB) values for the color.

The second MAKE OBJECT BOX command will give us our player character, which will be number one for convenience. The POSITION OBJECT command raises out character out of the ground, and places him at one end of the platform. The YROTATE OBJECT command rotates object one on the y-axis 180 degrees, so it is facing the right direction.
Go inside the DO LOOP, under the POSITION MOUSE command and enter this:



The POSITION CAMERA and POINT CAMERA commands do just that, they place the camera where you specify, and point it where you specify. In this case, the make the camera float above and behind the player, and point it down towards the player.

The IF LEFTKEY()=1 command checks to see if the right arrow key is being pressed, the AND part also makes the program check for the next qualifier, which is, OBJECT POSITION X(1)<34.5. This put together makes the program check IF the LEFTKEY is being pressed AND object 1 less than 34.5 units on the x-axis. If those qualifiers are true, the program will move on to the next line, if one or more is not true, it will skip to the ENDIF line. Anything inside the IF… ENDIF statement will not happen if one or more of the qualifiers in not true.

In this case, MOVE OBJECT LEFT is inside the IF…ENDIF statement. This command moves object one to the left a value of -.2 units, but only IF the left arrow key is pressed, and object 1 is less than 34.5 units on the x-axis. This means it will only move the character left if the left arrow is pressed and it not too far on the x-axis; this stops the character from being able to fall off the ground.
The next IF…ENDIF statement is exactly the same, only it is for moving the character right.

You can now move your little blue-ish box left and right on your tannish box.

This should be your code so far:



Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:39
Part 3: The Blocks to Dodge

In this section, we’re going to add the blocks that the player has to dodge.

Add the following code under the code that created the player.



The only code that you might not know in this section is the X value in the position object command; RND(37.5). The RND command generates a random number, up to the value in the parenthesis, 37.5 in this case. A number generated by the RND command will always be positive.
Go down under the player movement controls and type the following:



This code shouldn’t be too hard to understand. It moves the object, and if the object is a distance behind the player, it repositions it randomly. An issue that we are running into now is that this object will only respawn on the left side of the field. The reason for this, which I mentioned earlier, is that the RND command will always give us a positive number, so it will never generate a negative X value for the position. Since our playing field is right on 0,0,0; one half of the field will never have an object positioned on it. We fix that like this. Underneath the code that created object 2, add this:



This creates an identical object as object 2, but you’ll notice that the positioning code now reads RND(37.5)*-1. This multiples the value by negative 1, making it negative. Now go back under the movement code for object 2, and add:



Now object 3 will act like object 2, but on the other side of the map, and just a bit slower so they seem different. Now go ahead and add as many objects as you want. I had a total of 5, and had them all move at different speeds. You can choose the speed and shape of the objects, or you can just use the code that I used:


*NOTE: object 6 is a wide block that comes straight down the middle




Your code so far should looks something like this (It will differ if you used your own code for the boxes):



Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:41
Part 4: Consequences
As you’re probably aware if you tested the code, nothing happens when the red blocks hit you, which gives the game no challenge. In this section, we will give the player some health, and make the blocks take away health when they hit you.
Go directly above the DO command, and type the following:




This declares a variable called PlyrHlth, and sets it equal to 100. Now go below the DO command and type the following:



The INK command specifies the current ink color. We have the foreground set to red and the background set to black. The SET TEXT SIZE command should be obvious. The TEXT command is actually typing to the screen. 10,20 is the X and Y locations for the text. We are typing to the screen, the text in parenthesis, and the string value of our variable for the players health.

Now there needs to be a way for the health to be lowered. For this, we will need to use collision detection to see if the boxes hit the player. Go to the code for the player object, and every box you need to dodge, and add the following underneath them all, with X being the number of the object in question:



Now that the collision detection is on, we need to specify what happens if the objects collide. Underneath the code that moves the boxes we want to dodge, type the following:



This checks to see if there is a collision between the two object numbers in the parenthesis, in this case 1 and 2. If this collision is true, then our player health variable is lowered by 20, and the box is repositioned. Notice that the repositioning code is the same code I used earlier for this object. Go ahead and do the same thing for all your objects. My code looks like this:



You should notice that when you get hit, the number next to the text PLAYER HEALTH: decreases by 20. It will continue to decrease by 20 every time you get hit, even going into negative numbers. We will address this in the next section.
Your code so far should look like this:



Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:43
Part 5: Death

This section will stop the player health counter from going into the negative numbers, and kill the player when the health reaches zero.

Go directly above the SYNC command at the end of the main loop and type the following:



This will stop the player health variable from ever getting below zero; because if it is ever less than zero, it is automatically reset to zero.

Now go directly above what we just typed and enter the following code:



This code makes it so that when the players health it equal to zero, the player object is hidden, which makes it invisible. The player object is then sent down in the y direction so that it is out of the way of the boxes. We then type out a game over message. This alone would work, but there’s some other things we can do as well.
Modify the code that makes boxes move to look something like this:



This makes it so the boxes will stop respawning if the player object dies. Your code should now look like this:



Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:45
Part 6: The Score

What’s a game without a method to keep track of how well you’re doing? In this section, we’ll add a score counter that keeps track of the number of boxes you’ve been able to dodge.
Go ahead and add this line of code under the PlyrHlth variable:



That gives us another variable for the players score. It starts out at zero, because the player builds up a score, he doesn’t lose it. Right under the line where we type the players health to the screen, add the following:



You should already know what this does. We have to once again edit the code that makes the boxes move, it should now look like this:



This makes it so that every time a box goes past the player and resets, the players score goes up by one. We have to take this into account with our death code, edit the code that displays a game over message to look like this:



Now Your final score is stated when you die. Your code should now look like this:



Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:46
Part 7: Optimization

While the game is now functional, it could use a little bit of optimizing. You want to minimize the lines of code your program has, so we’re going to condense it a little. Go ahead and delete all the lines that look like this:



Instead, after the last object is created, type this:



Replace 6 with whatever is the highest object number you have (not counting the playing field). However, this time, the X should stay as X. This will make the program use the collision commands for every object number in the range of X that you specify. Before, we had 2 lines for every object we wanted, which amounted to 12 lines in my code. Now it’s only 4 lines; that’s a third of the original line count. The more objects you have, the more lines this will cut out. Your code should looks like this now:



Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 29th Jun 2009 23:48
Part 8: Making It Look Good

While you could argue that we now have a finished product, we still need to give it that extra little bit that makes it pretty. Because this tutorial doesn’t use any external media, and requires no files, we cannot use textures, we cannot use shaders, but we can add shadows.

Right below that AUTOCAM OFF command, enter:



This will activate the shadows, but we’re still not done. We have to specify the light position and range. Go underneath our optimized code that activates collision detection and enter the following:



The SET SHADOW LIGHT command creates a light for the sole purpose of casting shadows. The different parameters; 100,100,100,1000 are the X,Y,Z positions and the range. Light number 0 is the default light that is created automatically. We position this light where our shadow light is, and give it the same range.

Now that the light is set up, we have to tell it which objects to effect. Directly under the SET LIGHT RANGE line, add the following:



Now our objects cast shadows, and I declare our game done. I hope you learned something through all this, and had a good time as you learned. If you need any help on a bit more of a one to one level, go ahead and send me an email, or you can contact me through AIM. Both contacts should be available underneath my bright annoying flashy signature in the bar with the link to my profile. I’m not guaranteeing I can help you, but I’ll do my best.

Our final code, at 141 lines:



Deego
10
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 30th Jun 2009 06:29
Excellent tutorial! Simple, easy to follow, and touches a lot of bases. There are plenty of jumping off points for further additions; making a menu system, a high score saving system, firing projectiles at the blocks, adding effects when a block collides, adding a jump... the framework is there for people to begin exploring coding ideas.
Zotoaster
13
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 30th Jun 2009 10:24
Cool. I'll add this to the Tutorials Thread.
lucifer 1101
9
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 30th Jun 2009 12:16
awesome tutorial i got 114 on my first try, weirdly it seemed to jump alot, maybe it was the shadows....

Come and have a look at my hobby site once your ready..
http://darkdstudio.webs.com/
Note: its still really bare..
Thraxas
Moderator
12
Years of Service
User Offline
Joined: 8th Feb 2006
Location: The Avenging Axe, Turai
Posted: 30th Jun 2009 13:08
Fixed the title for you.

Nice tutorial

Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 30th Jun 2009 19:37 Edited at: 30th Jun 2009 19:38
Thanks for fixing that Thraxas. I had this entire thing typed out into a MS Word document before I posted it so it would be easier to spot any formatting mistakes; and of course the only thing I needed to type out I managed to get wrong .

Thank you for the comments everyone.

Potassium
9
Years of Service
User Offline
Joined: 12th Jul 2009
Location:
Posted: 24th Jul 2009 20:18
The code works when I comment out the lines:



I'm using the DBPro Trial, but I mostly use Dark GDK, only use DBPro to test out code snippets and such.

But one thing is that your code is all capitalized. I'm not sure if thats a good habit to teach beginners, but if you use a case-sensitive language, all caps-lock is not going to help. So I would make code all lower-case or something.

But nice tutorial, I might port this to Dark GDK, but that's not a definite promise.
Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 27th Jul 2009 09:03
Quote: "The code works when I comment out the lines:

I'm using the DBPro Trial, but I mostly use Dark GDK, only use DBPro to test out code snippets and such."


It might need one of the upgrades to work properly, I'm not sure.

Quote: "But one thing is that your code is all capitalized. I'm not sure if thats a good habit to teach beginners, but if you use a case-sensitive language, all caps-lock is not going to help. So I would make code all lower-case or something."


I say at the beginning of the tutorial that they do not need to type in all caps, I just think it looks a little neater. I think it's harder to read in lowercase and looks a little sloppy.

Quote: "But nice tutorial, I might port this to Dark GDK, but that's not a definite promise."


Thank you. If you, or anyone for that matter, wants to port it to whatever, go ahead, I don't mind. On that note, if anyone wants to build off it and explain how to add any other features, that's fine too.

Cyborg ART
11
Years of Service
User Offline
Joined: 14th Jan 2007
Location: Sweden - Sthlm
Posted: 21st Aug 2009 14:23
Sorry for the bump, but this is a great tutorial, thanks!

Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 25th Aug 2009 05:08
Quote: "Sorry for the bump, but this is a great tutorial, thanks!"


Thank you for the comment, if I ever get finished with the FPS game I'm making, I might turn that into a tutorial as well.

Glenash
8
Years of Service
User Offline
Joined: 9th Nov 2009
Location:
Posted: 9th Nov 2009 18:00
Thanks the character you play as (standing up rectangle) was abit slow so i changed the speed up to 9

OrzeL
8
Years of Service
User Offline
Joined: 20th Oct 2009
Location:
Posted: 10th Nov 2009 03:40
Wow Zaibatsu thanks for the tutorial. It helped me alot and looks amazing especially once you add in the lights.

Quote: " if I ever get finished with the FPS game I'm making, I might turn that into a tutorial as well."


You did such a great job with this tutorial, Can't wait for any future tutorials you decide to do.
Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 19th Nov 2009 02:08
Quote: "Wow Zaibatsu thanks for the tutorial. It helped me alot and looks amazing especially once you add in the lights."


Thanks for the feedback. It's nice to know it helped someone.

Quote: "You did such a great job with this tutorial, Can't wait for any future tutorials you decide to do. "


I can say with certainty that the FPS game will not become a tutorial, in its current stage, it requires several plugins that cost money, and would be very hard to adequately explain. I might try to write some other tutorials if I get any other ideas for simple games.

bg38
8
Years of Service
User Offline
Joined: 28th Oct 2009
Location:
Posted: 20th Nov 2009 02:54
Hey,Zaibatsu. Thanks for taking the time to make this tutorial.
I went thru it a few times and eventually replaced the blocks with some of the dark matter models, and added some music and sound effects to it

Great way to learn coding
Burning Feet Man
10
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 25th Nov 2009 05:52
Wonderful tutorial! I wish more experienced coders had the time and patience to write up similar examples! Fantastic stuff.
Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 4th Dec 2009 00:28
Quote: "Hey,Zaibatsu. Thanks for taking the time to make this tutorial.
I went thru it a few times and eventually replaced the blocks with some of the dark matter models, and added some music and sound effects to it

Great way to learn coding"


Thanks for the comment.

Quote: "Wonderful tutorial! I wish more experienced coders had the time and patience to write up similar examples! Fantastic stuff."


Thanks, but I'm really not an experienced coder. This tutorial was a learning experience for me to write.

nATEdAWGG
8
Years of Service
User Offline
Joined: 30th Dec 2009
Location:
Posted: 30th Dec 2009 08:05
i modified this quite a bit ... i set it to take only 10 hp each time but there is a glitch in that :S cant find it...i also set the blocks to start at certain speeds and the speed grows each time they respawn... and the one you have going right down the middle constantly, i made it look like the rest and set it to act like the rest also but switch from the left side to the right each time... and i think thats it... OOOH and on yours ... the parts about the shadow i had to remove and put

after AUTOCAM OFF and:

after the collision setup for the blocks...
heres the full code:

... pretty good for a slight beginner eh? ... all the programming i've had is a little bit of PBASIC at school but its at a big time beginner level
Zaibatsu
12
Years of Service
User Offline
Joined: 1st May 2006
Location: Lost in Thought
Posted: 2nd Jan 2010 04:50
Quote: "i modified this quite a bit ... i set it to take only 10 hp each time but there is a glitch in that :S cant find it"


It's nice to see that you took the effort to not just copy what I did, buy expand on it. If you explain more what the glitch is, I can try to hunt it down for you.

Nokiaqd
8
Years of Service
User Offline
Joined: 31st Aug 2009
Location:
Posted: 20th Jan 2010 11:55
Awesome. Very clean and not bulky with lots of explanations

Login to post a reply

Server time is: 2018-07-19 12:46:00
Your offset time is: 2018-07-19 12:46:00