DarkBASIC Programming/2D Game Programming Pt 2

Day 19 edit

Okay, so we've made Pong the next step is to make a Break-Out game. Just giving you, the source code of the game would prove you didn't code the pong game by yourself. So what I'm going to do is let you program your own break-out game, and I will give some guided examples. After this quick break-out game exercise I will be discussing multimedia!

Collisions

if sprite collision(paddle,ball) = 1 then bouncball = 1

Possible Solutions to Make the Ball Bounce :S

-Setup a variable called ballmovex, set it's value to random 1 or 0. -if the ball strikes 0 make it move left, and in the case of 1 right -do the same thing with a variable called ballmovey, except in this -case moving up or down.

Possible Solutions of Setting Up Blocks

-Setup an array which holds the x positions of your bricks, and the y positions of your bricks. Then one for if the brick has been struck, so if the brick has been struck it will equal 1 or true and then have conditional statements(if and thens) for the action to break the brick.

if if sprite collision(paddle,brickhit(bricknum)) = 1 then destroybrick = 1


if destroybrick = 1

 brickx(bricknum) = -1
 bricky(bricknum) = -1

endif


Something to Remember:

  Remember you need a do loop to accept advanced input such as inkey$(), returnkey(), etc.


I've given lots of suggestions as to how to complete the Break-Out game, hopefully this will go down nice and easy without heartburn. If you have a case of "what thes" then I suggest you visit the codebase on the official DarkBASIC home page: http://www.thegamecreators.com, it has everything a DarkBASIC coder shall need.

Multimedia In DarkBASIC - Audio

Not many people can say that they hate music, in fact most people love hearing ambient music in games, I know I do! Above, I used some strange commands, play sound, play music, load music, play music. Well, DarkBASIC has three audio supports, MP3, Wave, and Midi. Loading music is very simple, and sound works the same way. So I'm going to give you a code example, you bring the sounds, I'll bring the code!



Code Example for Audio:

Rem Audio Example
`Load audio
load sound "mysound.wav",1
load music "mymusic.mid",1
`Play audio once
play sound 1
`Loop music
loop music 1
`wait key
wait key
stop music 1
loop sound 1


So what are the numbers at the end? Remember in images, well this number is a numeric ID given. Remember the location of your audio, and extension!

Multimedia in DarkBASIC - Animations

This will probably be the last multimedia entry, unless someone does one on dlls, I won't be, since I have an older version of DarkBASIC.

Animations at this point in time do not include those such as mario walking, however they do include those such as animated GIFs(Graphic Interchange Format), and AVIs(Audio Video Interleave). So far I believe there are two formats for animation .gif, and .avi please tell me so if otherwise. Animations can be useful for game over screens and maybe even some Mortal Kombat style finishers!!




Code Sample

load animation "animation1.avi",1
play animation 1
wait key
stop animation 1
load animation "animation2.gif",2
play animation 2
loop animation 2


-PLEASE HELP FINISH THIS BOOK, CONTRIBUTE IN ANY WAY POSSIBLE PLEASE!-

________________________________________________________________

This is about 2D Games. This Wiki helped me become the programmer I am today. No, I am not DBTutor, but I will contribute. I am IRON PROGRAMMER!! Talk to me on the DarkBASIC Forums if you have a question.

I challenge YOU to create a 2D Melee Game. These are simple. Here is an example:

''cls rgb(150,150,150)

load image "Sumostein.bmp", 1
sprite 1, sprite1xpos, sprite1ypos, 1

sprite1xpos = 300
sprite1ypos = 300

load image "Sumocula.bmp", 2
sprite 2, sprite2xpos, sprite2ypos, 2

load image "Sumocula Punch.bmp", 3
sprite 3, sprite2xpos, sprite2ypos, 3
hide sprite 3

load image "Sumostein Punch.bmp", 4
sprite 4, sprite1xpos, sprite1ypos, 4
hide sprite 4

sprite2xpos = 300
sprite2ypos = 300

playeronelives = 3
playertwolives = 3

mainloop:
do
  cls rgb(150,150,150)
  sprite 1, sprite1xpos, sprite1ypos, 1
  sprite 2, sprite2xpos, sprite2ypos, 2
  sprite 3, sprite2xpos, sprite2ypos, 3
  sprite 4, sprite1xpos, sprite1ypos, 4
  hide sprite 3 
  if upkey() = 1 then sprite1ypos = sprite1ypos -5
  if downkey() = 1 then sprite1ypos = sprite1ypos +5
  if leftkey() = 1 then sprite1xpos = sprite1xpos -5
  if rightkey() = 1 then sprite1xpos = sprite1xpos +5
  if controlkey() = 1 then gosub sumosteinpunch
  if inkey$() = "w" then sprite2ypos = sprite2ypos -5
  if inkey$() = "s" then sprite2ypos = sprite2ypos +5
  if inkey$() = "a" then sprite2xpos = sprite2xpos -5
  if inkey$() = "d" then sprite2xpos = sprite2xpos +5
  if inkey$() = "x" then gosub sumoculapunch
  if spacekey() = 1 then end
  if sprite1xpos =<0 then sprite1xpos = sprite1xpos +5
  if sprite1xpos =>550 then sprite1xpos = sprite1xpos -5
  if sprite1ypos =<0 then sprite1ypos = sprite1ypos +5
  if sprite1ypos =>350 then sprite1ypos = sprite1ypos -5
  if sprite2xpos =<0 then sprite2xpos = sprite2xpos +5
  if sprite2xpos =>550 then sprite2xpos = sprite2xpos -5
  if sprite2ypos =<0 then sprite2ypos = sprite2ypos +5
  if sprite2ypos =>350 then sprite2ypos = sprite2ypos -5
loop

sumoculapunch:
hide sprite 2
show sprite 3
wait 250
if sprite collision(3, 1) = 1
  playeronelives = playeronelives -1
  if playeronelives = 0
  delete sprite 1
  end
endif
endif
hide sprite 3
show sprite 2
gosub mainloop

sumosteinpunch:
hide sprite 1
show sprite 4
wait 250
if sprite collision(4, 2) = 1
  playertwolives = playertwolives -1
  if playertwolives = 0
  delete sprite 2 
  end
endif
endif
hide sprite 4
show sprite 1
gosub mainloop''

That is a simple format for a multiplayer melee game in 2D. Notice I have cleverly not supplied the media! Hahahahahaaaa! Now get programming!

Day 20
edit

->>3d Programming<<- Tadadada! (I'm not DBtutor, nor IRONPROGRAMMER, i'm gluon, you can find me on the DexOS forums)
First Example
A controlable cube:

SYNC RATE 60

MAKE OBJECT CUBE 1,1
DO
    if leftkey()=1 then YROTATE OBJECT 1,OBJECT ANGLE Y(1)-2
    if rightkey()=1 then YROTATE OBJECT 1,OBJECT ANGLE Y(1)+2
    if upkey()=1 then XROTATE OBJECT 1,OBJECT ANGLE X(1)-2
    if downkey()=1 then XROTATE OBJECT 1,OBJECT ANGLE X(1)+2
    SYNC
LOOP
END

Instead of MAKE OBJECT CUBE we can say SPHERE,etc.
A textured object:

sync on
hide mouse
make object cube 1,20
load image "car.jpg",1
texture object 1,1,1
do
sync
loop

Moving an object

Sync on
sync rate 40
hide mouse  
make object cube 1,10  
do
   if rightkey()=1 then y=wrapvalue(y+1)
   if leftkey()=1 then y=wrapvalue(y-1)
   if upkey()=1 then move object 1,1
   if downkey()=1 then move object 1,-1
   yrotate object 1,y
   sync
loop