`Project: Lander DBPro Challenge
`Coded By: Mike Horton aKa "Skrawl"
`Date Started: 03/02/06
`Date Completed: 03/07/06
 
sync on:sync rate 60:set display mode 1024,768,16:hide mouse:set window off `Setup Basic Screen Settings
randomize timer() `Use System Clock To Get Random Seed, Ensures A Different Game Every Time
 
`VARIABLES!!!
global pX#=510.0                                    `Ship X Position
global pY#=100.0                                    `Ship Y Position
global pT#=0.0                                      `Ship Thrust (Thrust>0 = UP | Thrust<0 = Down)
global pS#=0.0                                      `Ship Strafe (Strafe>0 = Right | Strafe<0 = Left)
global pLives=3                                     `Player Lives - DER!
global level=1                                      `Current Level
global gravity#=0.0                                 `Planet Gravity = Changes Descent And Climb Rate
global winds#=0.0                                   `Planet Solar Winds = Causes Random Side Movement
global fuel=500                                     `Ship Fuel
 
global red=128                                      `Logo Color Changer
global green=128
global blue=128
 
global uX#=0.0
global uY#=300.0
global uO=0
 
type linetype                                      `Type For Each Line Segment Of Ground
   p1 as float                                     `X Position 1
   p2 as float                                     `Y Position 1
   p3 as float                                     `X Position 2
   p4 as float                                     `Y Position 2
endtype
 
type trail                                         `Type For Smoke Circle
   tX as float                                     `x Coord
   tY as float                                     `Y Coord
   life as integer                                 `Life Of "Particle"
   size as float                                   `Size of "Particle"
endtype
 
global dim ground(10) as linetype                `Create Array Of Line Segments To Define Ground
global dim vapor(30) as trail                      `Create Array Of Smoke Trail Circles
 
for n=1 to 30 step 1                               `Fill In Smoke Array With Starter Variables
   vapor(n-1).life=0
   vapor(n-1).size=3
next n
 
createGround()                                     `Fill In The Array For Ground
 
gameHelp()                                         `Display Begining Help Screen
 
`Main Game Start
do
   if uO=0                                         `If We Dont Draw The UFO
      wee=rnd(50000)                               `Get New Random
   endif
   drawShip(pX#,pY#)                               `Draw The Ship
   if wee<=10 or uO=1                              `If We Draw The UFO Or wee=rnd(50000)<=10
      drawUFO()                                    `Update The UFO
   endif
   drawGround()                                   `Draw Ground For Current Level
   drawHUD()                                       `Draw Our Game HUD
   cycleTrail()                                    `Cycle Through Out Smoke Trail And Update As Necessary
   wind=rnd(1000)                                  `Lets See If We Get Some Wind
   if wind<=100                                    `Blow The Ship From Side To Side!
      inc PS#,rnd(winds#*2)-(winds#)
   endif
   if scancode()=0 or fuel=0                       `If No Keypress
       if pT#>(-3-gravity#) then dec pT#,.1        `If thrust going up, decrement it to start gravity descent
       if pS#>0 then dec pS#,.1                    `If Strafe > 0 decrement Strafe back to 0
       if pS#<0 then inc pS#,.1                    `If Strafe < 0 increment Strafe back to 0
       if ps#=0 then ps#=0                         `If Strafe = 0 then keep it that way, had some wierd strafing without this code
   endif
   pY#=(pY#-pT#)+gravity#                          `Assign Ship Y Coord by subtracting Thrust and adding Gravity
   pX#=(pX#+pS#)                                   `Assign Ship X Coord by adding Strafe
   if pX#<0 then pX#=1024                          `If Ship Goes Of Screen Right, Bring It Back On Screen Left
   if pX#>1024 then pX#=0                          `If Ship Goes Of Screen Left, Bring It Back On Screen Right
   if pY#<=-200                                    `If Ship Goes Too Far Off The Top Of The Screen
      ink rgb(255,0,0),0
      center text 512,300,"You Left Orbit, Chicken"   `Let Them Know What They Did Wrong And How They Should Feel About It
      sync
      wait key
      restartLvl()                                    `Restart The Level And Make Them Feel Even Worse
   endif
   if pY#<0                                           `If Ship Leaves The Top Of The Screen
      ink rgb(255,0,0),0
      center text 512,384,"Warning!, Leaving Orbit"   `Let The Player Know Whats About To Happen
   endif
   if upkey()=1 and fuel>0                         `If Player Is Pushing The Up Arrow Key And The Ships Got Gas
         if pT#<5 then inc pT#,.1                  `Increase Thrust
         dec fuel,1                                `Decrease Fuel
         newTrail()                                `Create A New Smoke Trail
   endif
   if leftkey()=1 and pS#>-5 then dec pS#,.1       `Strafe Left
   if rightkey()=1 and pS#<5 then inc pS#,.1       `Strafe Right
   if getCollision(pX#,pY#+10)               `Check Collision
      tmp#=abs(pS#)                          `Get The Absolute Value Of Strafe, For Some Reason A Negative Number Here Was Annoying
      if pT#<=-1.0 or tmp#>=1.0              `See If The Ship is Moving Too Fast In Any Direction
            restartLvl()                     `If It Is, Kill The Ship And Start The Level Over
      else                                   `If Not
         advanceLvl()                        `Send That Player To The Next Level
      endif
   endif
   sync
   cls 0
loop
`End Main Loop
 
function gameHelp()
   set text font "Arial"
   set text size 30
   sync
   ink rgb(0,255,0),0
   center text 512,250,"Lander Remix"
   set text size 20
   ink rgb(200,0,200),0
   center text 512,300,"Welcome To Lander Remix"
   center text 512,320,"Coded By Mike Horton"
   center text 512,340,"Features Unlimited Levels With Increasing Difficulty"
   center text 512,360,"Controls:"
   center text 512,380,"Up - Increase Thrust"
   center text 512,400,"Left/Right - Strafe Ship Left And Right"
   center text 512,420,"Rules:"
   center text 512,440,"Attempt To Land On The Planets Surface"
   center text 512,460,"You May Land Anywhere, But The Flat Spots Are Easier."
   center text 512,480,"Your Fuel Will Decrease As You Thrust"
   center text 512,500,"When You Run Out Of Fuel You Will Crash"
   center text 512,520,"The Planets Gravity Effects How Hard It Is To Climb"
   center text 512,540,"And How Quickly You Go Down"
   center text 512,560,"Solar Winds In The Atmoshphere Will Move Your Ship"
   center text 512,580,"From Side To Side, Making Landing Even Tougher"
   center text 512,600,"That Sums It Up, Enjoy!"
   sync
   wait key
endfunction
 
function newTrail()
   for n=1 to 30 step 1
      if vapor(n-1).life=0          `If Place In Array Is Empty (If The Smoke Circle Isn't Already On Screen)
         vapor(n-1).tX=pX#          `Grab The Smoke X Coord From The Ship
         vapor(n-1).tY=pY#+20       `Grab The Smoke Y Coord From The Ship and Move It Down Some
         vapor(n-1).life=25         `Give The Smoke Some Life (How Many Cycles It Will Stay On Screen)
         vapor(n-1).size=3          `And Set It's Starting Size
         n=30
      endif
   next n
endfunction
 
function cycleTrail()
   for n=1 to 30 step 1
      if vapor(n-1).life>0          `If The Smoke Hasn't Died Yet
         dec vapor(n-1).life,1      `Take A Life Away
         inc vapor(n-1).tY,3        `Move The Smoke Down
         inc vapor(n-1).size,.1     `Make It A Little Bigger
         ink rgb(vapor(n-1).life*8,vapor(n-1).life*8,vapor(n-1).life*8),0 `Make It A Little Darker
         circle vapor(n-1).tX,vapor(n-1).tY,vapor(n-1).size       `Draw That Puppy
      endif
   next n
endfunction
 
function createGround()
   ground(0).p1=0                                        `Set The First X Coord Of The First Line
   ground(0).p2=600                                      `Set The First Y Coord Of The First Line
   for n=1 to 10 step 1
         temp = rnd(200) + ground(n-1).p1                `Get Second X Coord
         do
            change= rnd(300)                             `Get Decision Variable
               if change<100                             `If Decision<100
                  temp2=ground(n-1).p2-rnd(200)          `Get Second Y Coord Above First One
               endif
               if change>=100 and change <200            `If Decision>=100 and Decision<200
                  temp2=ground(n-1).p2                   `Get Second Y Coord At The First One Making A Straight Line
               endif
               if change>=200 and change<=300            `If Decision>=200 and change <=300
                  temp2=ground(n-1).p2+rnd(200)          `Get Second Y Coord Below First One
               endif
               if temp2>=400 and temp2<=650  then exit   `If Second Y Coord Is Within Exceptable Range, Then Kick Us Out Of The Loop
         loop
            if n<10                                      `If We Aren't On Our Last Line
               ground(n-1).p3=temp                       `Set Line's Second X Coord
               ground(n-1).p4=temp2                      `Set Line's Second Y Coord
            else                                         `If We Are On The Last Line
               ground(n-1).p3=1024                       `Set Line's Second X Coord
               ground(n-1).p4=600                        `Set Line's Second Y Coord
            endif
            if n<10                                      `If We Aren't On Our Last Line
               ground(n).p1=temp                         `Set Next Lines First X Coord
               ground(n).p2=temp2                        `Set Next Lines First Y Coord
            endif
   next n
endfunction
 
function drawShip(x1 as float,y1 as float)
   ink rgb(255,255,255),0
   line x1,y1,(x1-5),(y1+15)
   line (x1-5),(y1+15),(x1+5),(y1+15)
   line (x1+5),(y1+15),x1,y1
endfunction
 
function drawUFO()
   if uO=0
      uY#=rnd(400)
      uX#=0.0
      uO=1
   endif
   inc uX#,1
   if uX#=1024 then uO=0
   ink rgb(255,255,255),0
   ellipse uX#,uY#,15,8
   line uX#-15,uY#-1,uX#+15,uY#-1
   line uX#-15,uY#+1,uX#+15,uY#+1
   for n=-15 to 15 step 3
      ink rgb(rnd(255),rnd(255),rnd(255)),0
      circle uX#+n,uY#,1
   next n
 
endfunction
 
function drawGround()
   ink rgb(255,255,255),0
   for n=0 to 9
      line ground(n).p1,ground(n).p2,ground(n).p3,ground(n).p4
   next n
endfunction
 
function getCollision(x1 as float,y1 as float)
   for n=0 to 9 step 1                                         `Step Through Every Line Segment In The Ground Array
      for t#=0.0 to 100.0 step 1.0                             `Used In Getting Each Point Along The Line
         lX#=t#/100                                            `Get Percent
         lX#=lX#*ground(n).p3                          `Get Point At Percent Value Of Line X Coord
         lX#=lX#+ground(n).p1                          `Add First Point To Ensure We're On The Line
         if lX# >= (ground(n).p3) then lX#=ground(n).p3 `Safeguard To Ensure We Dont Go Off The Line
         if (ground(n).p4) = (ground(n).p2) then lY#=ground(n).p4 `If Line Y Coord Doesn't Change, Point Doesn't Either
         if (ground(n).p4) <> (ground(n).p2)   `If Line Y Coord Changes Go Here
            lY#=t#/100                                         `Get Percent
            if (ground(n).p4) > (ground(n).p2) `If Line Y Coords Go Down Do This
               lY#=lY#*ground(n).p4                    `Get Point At Percent Value Of Line Y Coord
               lY#=lY#+ground(n).p2                    `Add First Point To Ensure We're On The Line
               if lY# >= (ground(n).p4) then lY#=ground(n).p4 `Safeguard To Ensure We Don't Go Off The Line
            endif
            if (ground(n).p2) > (ground(n).p4) `If Line Y Coords Go Up Do This
               lY#=lY#*ground(n).p2                    `Get Point At Percent Value Of Line Y Coord
               lY#=ground(n).p2-lY#                    `Subtract First Point From Percent Value To Stay On The Line
               if lY# <= (ground(n).p4) then lY#=ground(n).p4 `Safeguard To Ensure We Don't Go Off The Line
            endif
         endif
         if (x1<=(lX#+5) and x1>=(lX#-5)) and (y1<=(lY#+5) and y1>=(lY#-5)) `See If Collision Has Occured
            exitfunction 1                                                 `Send Out That Collision Has Happened
         endif
      next t#              `Increment T
   next n                  `Goto Next Line Segment
endfunction 0
 
function restartLvl()
      pLives=pLives-1
      pX#=510.0
      pY#=100.0
      pT#=0.0
      pS#=0.0
      fuel=500
      if pLives>=1
         ink rgb(255,0,0),0
         center text 512,384,"You Crashed!!"
      else
         ink rgb(255,0,0),0
         center text 512,380,"You Lose!"
         center text 512,400,"Press Escape To Run Away, Anything Else To Try Again"
         pX#=510.0
         pY#=100.0
         pT#=0.0
         pS#=0.0
         pLives=3
         level=1
         gravity#=0.0
         winds#=0.0
         fuel=500
      endif
      sync
      wait key
endfunction
 
function advanceLvl()
   pX#=510.0
   pY#=100.0
   pT#=0.0
   pS#=0.0
   fuel=500
   level=level+1
   if gravity#<2
      inc gravity#,.1
   endif
   if level=20
      gravity#=0.0
   endif
   if level>=20 and winds#<1
      inc winds#,.1
   endif
   ink rgb(0,255,0),0
   center text 512,384,"You Landed!"
   createGround()
   sync
   wait key
endfunction
 
function drawHUD()
   ink rgb(0,0,255),0
   text 10,740,"Lives: "+str$(pLives)
   text 100,740,"Level: "+str$(level)
   gTemp=gravity#*100
   text 200,740,"Gravity: +"+str$(gTemp)
   wTemp=winds#*100
   text 300,740,"Winds: "+str$(wTemp)
   text 400,740,"Fuel: "+str$(fuel)
   if fuel<=200
      ink rgb(255,0,0),0
      text 500,740,"LOW FUEL!"
   endif
   if pT#<-1.5
      ink rgb(255,0,0),0
      text 600,740,"SLOW!"
   endif
   inc red,rnd(20)-10
   inc green,rnd(20)-10
   inc blue,rnd(20)-10
   if red<0 then red=0
   if red>255 then red=255
   if blue<0 then blue=0
   if blue>255 then blue=255
   if green<0 then green=0
   if green>255 then green=255
   ink rgb(red,green,blue),0
   line 0,700,1024,700
   line 0,710,1024,710
   line 700,710,700,767
   line 700,767,885,767
   line 700,710,885,710
   line 750,767,750,755
   line 750,755,720,755
   line 720,755,720,710
   line 740,755,740,740
   line 760,767,760,710
   line 730,730,750,730
   line 770,767,770,750
   line 780,710,780,727
   line 790,710,790,767
   line 810,767,820,760
   line 820,760,820,717
   line 820,717,810,710
   line 810,730,810,740
   line 850,767,850,710
   line 850,750,840,750
   line 850,730,840,730
   line 885,767,885,710
   line 870,767,865,750
   line 860,730,870,730
   set text to bold
   ink rgb(255,0,0),0
   text 890,750,"R E M I X"
   set text to normal
endfunction