UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on February 02, 2007, 05:00:57 PM

Title: A Simple Game Frame Work
Post by: kevin on February 02, 2007, 05:00:57 PM


[pbcode]


; PROJECT : Project1
; AUTHOR  : Kevin Picone
; CREATED : 3/02/2007
; ---------------------------------------------------------------------



;  Upon startup we see the main settings of the program

      Setfps 75
      LoadFont "Arial",1,32,0




   ; Use the AUto counter to define some meaningful constants we can use.
   ; This allow us to change the order, values of the following flags in
   ; the future without breaking the program,
   ; Set AC to Zero
   Acset =0
   ; define some constants
   constant MenuOption_NoAction       =ac(1)
   constant MenuOption_PlayGame       =ac(1)
   constant MenuOption_Menu         =ac(1)
   constant MenuOption_EndProgram    =ac(1)



   ; Start Game on the menu
   Option=MenuOption_Menu   


   ; ===========================================================
   Repeat  ; MAIN LP of the APP
   ; ===========================================================
   
      ; Choose what section, the proram shoudl run
      select Option
         
         ; option ='s the menu tag, then we call the MENU
         case    MenuOption_Menu   
               option=Menu()   

         ; if option ='s the play Game tad, we call the game   
         case MenuOption_PlayGame
               option=PlayGame()   
   
      endselect
   
   
   Until Option=MenuOption_EndProgram      ; user seelect to exit program    



; delete media, save settigns  etc etc

end



; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------MENU SECTION  ------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------



Function Menu()

      Option=MenuOption_Nothing

      repeat

         Cls 0
         
            y=getscreenheight()/3
            x=getscreenwidth()/2

            centerText x,y, ">> MY COOL GAME MENU<<"
            centertext x,y+70,"P = Play Game"      
            centertext x,y+100,"Q = Quit Program"
         ; dectect what optins the user has selected and          


            ; Check if the user selected a menu option         
            Key$=lower$(inkey$())   
            select Key$
               case "p" : Option=MenuOption_PlayGame
               case "q" : Option=MenuOption_EndProgram
            Endselect

         sync         
      until Option<>MenuOption_Nothing

      Flushkeys
         
EndFunction    Option
   



; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; -------------------------GAME SECTION -------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------


   Constant Alive=true
   Constant Dead=false


Function PlayGame()

       ShowMessageTime=timer()+2000

      AlienStatus=Alive
      AlienRadius=20
      AlienX#= AlienRAdius
      AlienY#=100
      ALienSpeed#=5
            


      PlayerStatus      =Alive
      PlayerSpeed         =5
      PlayerWidth         =30
      PlayerHeight      =10
      PlayerY            =getScreenHeight()-PlayerHeight*3
      PlayerX            =getScreenWidth()/2
      PlayerScore         =0

   
      
      repeat
         Cls 0

               ALienX#=AlienX#+AlienSpeed#                  
               if (AlienX#-AlienRadius)<0
                  ; Flip it's speed (direction)
                  AlienSpeed#=-AlienSpeed#
                  AlienX#=AlienRadius
                  AlienY#=AlienY#+abs(AlienSpeed#)*3
                  if AlienSpeed#<10
                     ALienSpeed#=ALienSpeed#+0.25
                  endif
               endif

               if (AlienX#+AlienWidth)>GetScreenWidth()
                  ; Flip it's speed (direction)
                  AlienSpeed#=-AlienSpeed#
                  AlienX#=GetScreenWidth()-AlienRadius
                  AlienY#=AlienY#+abs(AlienSpeed#)*3
               endif


               CircleC AlienX#,ALienY#,AlienRadius,true,rgb(200,100,50)
               

               ; Random Fire a lazer (erm LINE :) from the alien
               
               if Rnd(1000)>960
                     Line alienX#,alienY#,alienX#,GetScreenHeight()

                     ; Check if the Lazer his the player
                     
                                                    
                     
                     if true=LineIntersectRect(AlienX#,AlienY#,AlienX#,getScreenHeight(),_
                                          PLayerX,PLayerY, PLayerX+PLayerWIdth,PLayerY+PlayerHeight)

                           ; if the lazer hit the player, then the player is DEAD
                              PlayerStatus=DEAD               

                     endif

               endif
               



               ; Handle player controls
               if LeftKey()       
                  PlayerX=PLayerX-PLayerSpeed                                    
                  if PlayerX<0 then PLayerX=0
               endif         
         
               if RightKey()       
                  PlayerX=PLayerX+PLayerSpeed                                    
                  if (PlayerX+PLayerWidth)>GetScreenWidth() then PLayerX=getScreenWidth()-PLayerWidth
               endif         
               ; draw the line   
               Box PLayerX,PLayerY,PLayerX+PLayerWidth,PLayerY+PLayerHeight,true
               inc PLayerScore


               CenterText getSCreenWidth()/2,20,"Score:"+digits$(PlayerScore,5)



               ; show the message for a period of time
               if ShowMessageTime>timer()
                  CenterText getscreenwidth()/2,GetScreenHeight()/2-30,"Look out!"         
               endif
         
            sync         
         until PlayerStatus=DEAD

      
         Flushkeys

         ShowGameOver(PlayerScore)


      ; set the return so to return the menu,
      ; we could also set it to end the  Program, or whatever other options
      ; you drea up      
      option=MenuOption_Menu
   
EndFunction   option
   


; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; -------------------------GAME OVER SCREEN ---------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------

   
Function ShowGameOver(Score)
         
         ShowTime=Timer()+5000   ; Game screen will show for 5 seconds
         AllowKeysTime=Timer()+1000   ; allow press a key to exit in 1 second

         ; Display the Game over INFO
         Repeat
            Cls 0
            ypos=GetScreenHeight()/3
            CenterText getSCreenWidth()/2,ypos,"Game Over"
            CenterText getSCreenWidth()/2,ypos+50,"You Scored:"+digits$(Score,5)
            if Timer()>AllowKeysTime
               if ScanCOde() then exit               
            endif            
            Sync
         Until Timer()>ShowTime
      
EndFunction

[/pbcode]