DarkBASIC Programming/2D Game Programming Pt 1

Day 15
The half way point, finally after all that coding and reading and days. This chapter we'll be making a text adventure using more text commands you'll now learn. Plus you'll learn how to clear the screen in various colors, and oh boy you're gonna enjoy this lesson.

You probably thought, my God does the text always have to be white!

`NOPE ink rgb(0,255,0),1 Print "OMG FINALLY COLOR!"

There you are! I'm a Canadian but I still spell color as color instead of colour. What's the RGB for? It stands for Red, Green, Blue, look at your monitor with a lens you'll see lots of those pixels in those colors. The rgb must be separated by brackets and then a comma for the transparency value. The transparentcy ranges from 0 to 255. RGB values range from 0 to 255 also.

Here's Some Colors to Remember

255,0,0 - red 0,255,0 - green 255,255,0 - yellow 0,0,255 - blue 0,255,255 - cyan 255,255,255 - white 0,0,0 - black 255,0,255 - pink 192,192,192 - light grey

To see more have a look in paint, view the rgbs values and fool around that's the way I found out, so you should find out too. Using these above values you should be able to make nice colors. The lower the value the darker, the higher the lighter.

Now after that you want a better way to Print right?

text x,y,string$

When using text, you need to use + instead of a semi-colon to separate variables. All values must be a string, remember to convert a value to a string use the str$ command as seen in Lesson 6.

The Right Way

a = 1 text 1,100,"This is no syntax error "+str$(a)

The Wrong Way

a = 1 text 1,100,"This is a syntax error you won't see this text until you fix "+a

We can change the font easily

set text font fontnameasstring$

We can make our text opaque easily too or allow us to write over other text values

set text opaque

Here's style changes

set text bold set text italic set text bolditalic

Here's how to change the size

set text size integernumber

Okay here's all those in a program

set text font "Times New Roman" set text size 30 set text bold text 1,100,"Hey"

You assignment is to fool around with text values and colors, come back tomorrow

Day 16
Okay, now we've learned lots about changing text values it's time to write your own text adventure. We'll call it "Dungeons of Doom", it'll have the player to escape the dank dungeons of a castle using numerous data commands and whatnot.

First let's do an outline:



Now we have an outline let's write a simple story:

Evil Elves have captured you in their prison, now you must escape the Dungeon of Doom. Beware some turns lead to doom, others lead to a way out.

There is only two ways to die in our game, since it's more like a maze anyways.

Now Let's Do Some Data For It:

`Dungeons of Doom `By Dbtutor

`Room dim room1$(10) dim room2$(10) dim room3$(10) dim room4$(10) dim room5$(10) dim room6$(10) dim room7$(10) dim room8$(10) dim room9$(10) dim room10$(10) dim room11$(10) dim room12$(10)

rem - gosub startgame

startgame: cls gosub start for a = 1 to 10 Print room1$(a) next a

do if inkey$ = "l" then gosub room1_b if inkey$ = "r" then gosub room2_b if inkey$ = "u" then gosub room3_b loop

room1_b: cls gosub room1 for a = 1 to 10 Print room2$(a) next a wait key end

room2_b: cls gosub room2 for a = 1 to 10 Print room3$(a) next a wait key end

room3_b: cls gosub room3 for a = 1 to 10 Print room4$(a) next a

do if inkey$ = "l" then gosub room4_b if inkey$ = "r" then gosub room5_b if inkey$ = "d" then gosub startgame if inkey$ = "u" then gosub room6_b loop

room4_b: cls gosub room4 for a = 1 to 10 Print room5$(a) next a wait key end

room5_b: cls gosub room5 for a = 1 to 10 Print room6$(a) next a

do if inkey$ = "b" then gosub room3_b loop

room6_b: cls gosub room6 for a = 1 to 10 Print room7$(a) next a

do if inkey$ = "l" then gosub room7_b if inkey$ = "r" then gosub room8_b if inkey$ = "d" then gosub room3_b if inkey$ = "u" then gosub wingame loop

room7_b: cls gosub room7 for a = 1 to 10 print room8$(a) next a do  if inkey$ = "b" then gosub room6_b loop

room8_b: cls gosub room8 for a = 1 to 10 print room9$(a) next a

do if inkey$ = "b" then gosub room6_b loop

wingame: cls gosub win for a = 1 to 10 print room10$(a) next a wait key end

`Data Statements start: data "Dungeons of Doom - Starting Area" data "The cell door swings open, the guard lies dead in a pool of blood" data "A large gash does through his neck, clutching your dagger you" data "Have a decision to make." Data "You can Go - L>eft, R>ight, or U>p"

for a = 1 to 5 read room1$(a) next a

return

room1: data "Dungeons of Doom - Room 1" data "You look around the corner and leap out at a guard" data "Sticking a sharp spear in the air your gored by it" data "Through the heart you die instantly and never escape" data "The Dungeons of Doom....." data "Press Any Key"

for a = 1 to 6 read room2$(a) next a

return

room2: `Right of start data "Dungeons of Doom - Room 2" data "As you reach the room, you notice the walls close in!" data "You are squished between the walls" data "The End - Press Any Key" for a = 1 to 4 read room3$(a) next a

return

room3: `North of the start point data "Dungeons of Doom - Room 3" data "This room is a room that leads all ways..." data "U>p, L>eft, R>ight or D>own" for a = 1 to 3 read room4$(a) next a

return

room4: `Left of Up one from start point data "Dungeons of Doom - Room 4" data "You run into a monstrous troll, who proceeds to bash you" data "Over the head with a club, waking up inside a pie" data "You are instantly eaten by a horde of trolls" data "The End - Press Any Key"

for a = 1 to 5 read room5$(a) next a

return

room5: `Right of the room4 data "Dungeons of Doom - Room 5" data "You run into a dead end, you must go out the way you came" data "B>ack"

for a = 1 to 3 read room6$(a) next a

return

room6: `North of Room data "Dungeons of Doom - Room 6" data "This room is a dank one, to the north you feel cold air a way out?" data "U>p, L>eft, R>ight, or D>own"

for a = 1 to 3 read room7$(a) next a

return

room7: `Left of the room 6 data "Dungeons of Doom - Room 7" data "Dead End Go Back" data "B>ack"

for a = 1 to 3 read room8$(a) next a

return

room8: `Right of Room 6 Data "Dungeons of Doom - Room 8" data "Dead End Go Back" data "B>ack" for a = 1 to 3 read room9$(a) next a

return

win: Data "You Won" Data "Escaping to the Forest, now you need to find a town!" data "Press Any Key"

for a = 1 to 3 read room10$(a) next a

return

This game is complete, all things should work if not feel free to email me at: thenerd2468@hotmail.com

Now then your assignment to show you know what's going on is to go back and add in comments describing what's happening in each. The final part in your assignment is to edit all the code and data statements to make a sequel, add rooms, and more. No this game is not a text role playing game, we'll make a text role playing game in the next lesson. I think tomorrow your gonna have a break-out of game programming.

Day 17
Oh boy, oh boy, oh boy! We're going to make a Pong game today, a BreakOut game today, and your going to learn about images, sprites and more! Okay, so how can we load multimedia in DarkBASIC? Remember in the file manager program we had some confusing formats of files in there. One thing I didn't add was animations unfortunately.

Multimedia Example:

`Image `Supports bmps and jpgs only id = 1 string$ = "myimage.bmp" load image string$,id x = 1 y = 1 paste image id,x,y

`Music `Sound works the same way, only instead the word music is replaced with the word `sound `Supports are: wav, mp3, and midi musid = 1 musstring$ = "mymusic.mid" load music musstring$,musid play music musid stop music musid loop music musid

`Animation `Supports gif, avi, may support other formats animid = 1 animstring$ = "myanimation.gif" load animation animstring$,animid play animation animid

`Bitmap `Works same as image only supports bitmaps, you can load only 32 bmpid = 1 bmp$ = "mybmp.bmp" load bitmap bmp$ `This pastes the bitmap, but the id can be used to create bitmaps at certain `locations `see bmps under the command reference for those details

end

Go look at some more examples of multimedia in DarkBASIC, some more are 3dsound, and you can play music off your cd too. We'll be using some more examples of multimedia in DarkBASIC in later chapters for now we'll be focusing on the said above in the example.

Your main question may be what is a sprite? A sprite is a 2d character that is used in games. Basically a sprite is a image that does more, in DarkBASIC sprites are the lifeblood of the system.

Sprite Works Like

sprite spriteid,x,y,imageid

You cannot use load bitmap and use that as an image for your sprite, you can only use images loaded by the load image command.

Before you did not manipulate the x,y cords of a image in DarkBASIC well that's about to change. Before we start, if we leave the computer to take care of our game's framerate then our game will run slow and differently on different paced computers. Use sync on, to turn the framerate over too you and away from the computer. Every time the sync command is used the screen is updated constantly. Use the sync rate command, to set the rate the frame refreshes, mostly 30 is good for 3d games in DarkBASIC, and 40 for 2d games is also good too, but use 30 to make sure. There are a few commands in darkbasic that draws crude graphics, the commands we'll use are: line(line startingx,toendingx,startingy,toendingy), circle(x,y,radius), and dot(dot x,y). They draw these graphics in the default ink, if the ink is not set it'll use cyan as a default. There are more commands that draw different basic 2d shapes, but we won't be using them. If your curious go check them out under BASIC2d under the DarkBASIC command reference.

Here's an example of you being able to move a circle `Moving Circle `By Dbtutor

`Hide the mouse hide mouse

`Setup sync rate sync on sync rate 30

`Make the screen grey cls rgb(192,192,192)

`Select an ink color for us, make it all transparent ink rgb(0,155,0),0

`Draw the circle at coordinates provided xpos = 100 ypos = 200 circle xpos,ypos,10

`Start the main loop do `Clean up mess left over, and re-draw circle cls rgb(192,192,192) circle xpos,ypos,10

`User control if upkey = 1 then ypos = ypos - 7 if downkey = 1 then ypos = ypos + 7 if rightkey = 1 then xpos = xpos + 7 if leftkey = 1 then xpos = xpos - 7 `Allow no leaving the screen if xpos > 600 then xpos = xpos - 7 if xpos < 1 then xpos = xpos + 7 if ypos > 400 then ypos = ypos - 7 if ypos < 1 then ypos = ypos + 7 `Refresh the screen sync loop

Wow that worked out great eh? Let's get some sleep for now.

Day 18
Before we start I'd just like to thank everybody for visiting this wikibook, taking time to read it and for knowing when I google "DarkBASIC Programming" I see my book on the first page. That's a great feeling! I hope you learn something, I'll try and get this book done very soon but I've been very busy with learning new things and my schoolwork. I've been learning Assembly, C++, Pascal, & Visual Basic I'm also considering buying a console building kit "The XGameStation" by Mr Lamothe or as you may know him Andre' Lamothe or Lord Necron. He's one my biggest inspirations for programming besides Lee Bamber(the visionary behind DarkBASIC). Okay let's start with the pong game, I must say it's been a challenge writing this pong game, I've had lots of bugs with it. You must use your own images, only the source code is included.

Understanding the Material Before I go ahead with the Pong game there is some things that I must, nay, need to explain to you. First, of all you need to understand what a "sprite" is, yes I know I mentioned it but not has in depth as I need to now, again that was just a basic example. Basically, put a sprite is nothing but an active image that allows more manipulation then standard bitmaps. In our terms, speaking a sprite is in most cases nothing but a 2d character. We can do more with sprites in DarkBASIC then we can simply using just plain images.

You already know how to setup a sprite. But you do not know how to delete sprites, how to detect collision with sprites, and have not used them very much yet. The syntax of deleting a sprite is simple: delete sprite spriteid. The sprite obviously must exist if not you'll get a runtime error stating that the "bob" or sprite does not exist. The same works for collisions with sprites: the precise and best way is by using this command sprite collision(spriteid1,spriteid2). Again they must exist has I stated, and you must have two, again I'm stating the obvious but there are pretty dumb people out there.

The other way works the same thing except it's not has precise and goes like this: sprite hit(spriteid1,spriteid2). Easy! What may not be so easy after reading the code below is what the random numbers are for. These random numbers simply put are so that the computer generates a number from 0 to 1 in which direction the ball should go. They are for both X axis and Y axis and you should know what those are already.

The Pong Game -

if you don't understand the material review previous lessons or consult the DarkBASIC help files.

`Pong example code `By Dbtutor `You must use your own multimedia!

sync on sync rate 40 hide mouse

`Load the images load image "paddle1.bmp",1 load image "paddle2.bmp",2 load image "ball.bmp",3

` Load the music load music "Dodge This.mid",1

` Load the sound load sound "thump.wav",1

` Play the music play music 1

` Start the main game loop ` Setup some variables first oneypos = 1 onexpos = 1 twoypos = 1 twoxpos = 620 bally = 150 ballx = 150 movey = rnd(1) movex = rnd(1) onegoal = 0 twogoal = 0

do

`Display Scores & Whatnot set text opaque text 1,1,"Goals: "+str$(onegoal) text 585,1,"Goals: "+str$(twogoal)

`Player Control for 1 if upkey = 1 then oneypos = oneypos - 7 if downkey = 1 then oneypos = oneypos + 7 if oneypos < 1 then oneypos = oneypos + 7 if oneypos > 432 then oneypos = oneypos - 7

`The paddle sprite sprite 1,onexpos,oneypos,1

`Player 2 Paddle if inkey$ = "w" then twoypos = twoypos - 7 if inkey$ = "s" then twoypos = twoypos + 7 sprite 2,twoxpos,twoypos,2 if twoypos < 1 then twoypos = twoypos + 7 if twoypos > 432 then twoypos = twoypos - 7

`The Ball if movey = 1 then bally = bally + 5 if movey = 0 then bally = bally - 5 if movex = 1 then ballx = ballx + 5 if movex = 0 then ballx = ballx - 5 sprite 3,ballx,bally,3

`Collision Detection if sprite hit(1,3) = 1 then movex = 1:play sound 1 if sprite hit(2,3) = 1 then movex = 0:play sound 1 if ballx < 1 then movex = 1:twogoal = twogoal + 1:play sound 1 if ballx > 620 then movex = 0:onegoal = onegoal + 1:play sound 1 if bally < 1 then movey = 1:play sound 1 if bally > 432 then movey = 0:play sound 1

`Wins if onegoal = 10 text 100,150,"Player 1 Has Won!" wait 300 end endif

if twogoal = 10 text 100,150,"Player 2 Has Won!" wait 300 end endif

`Refresh the screen sync

loop

Okay there you are Pong in all it's glory. Now it's time for a prolonged Quiz.

Quiz
1 In Game Design What Is A 2d Character Called?
 * A) Mario
 * B) Chara
 * C) Clip Art
 * D) Sprite

2 True or False: 30 is a good sync rate for games on any computer
 * T)rue
 * F)alse

3 Which is the best way to manually setup framerate in a computer game
 * A)Set the sync on, set it to an average rate such as 30, & use sync to refresh when need
 * B)Let the computer handle it all
 * C)Set the sync rate to 0
 * D)Simply turn on sync

4 You can declare data statements how?
 * A)Data datatype
 * B)dat datatype
 * C)godarkbasic data datatype
 * D)data.db datatype

5 What kinds of data does data statements hold?
 * A)Strings Only
 * B)Arrays
 * C)Strings, Integers, & Reals(floating point decimals or floats)
 * D)Integers

6 In our text adventure you had to escape:
 * A)Castles
 * B)Prison
 * C)Forest
 * D)A Dungeon

Okay now, for the answers, if you cheat your on your own after you have this book completed so don't be a wimp and take the coward's way out.

Answers:
 * D
 * 1) True
 * A
 * A
 * C
 * D

Finally we need to end this long hard-coding day, for another awaits with many new coding properties.