Page 1 of 1

Basic 2D Game Template

Posted: Wed Nov 03, 2010 3:11 am
by J. Baker
Here's a basic template that should help on creating a 2D game. It's has a grid that can be enabled by pressing the "G" key. I found this a pretty simple way of setting up graphics without using a separate editor. But if this method is not for you, you can always remove the grid code. I commented on as much as I could, so it should be easy to follow. ;)

Code: Select all

;Screen settings which can be also useful for sprites.
#ScreenWidth = 1024
#ScreenHeight = 768
#ScreenDepth = 32
#GameTitle = "New Game"

;Temporary grid settings for editing.
#GridSize = 512
#GridTile = 32
#GridMarker = 128
#LevelWidth = 2048 ;Can also make #LevelWidth1, #LevelWidth2, and so on for different level lengths. Although the grid is set for #LevelWidth but could be edited.

;Initializing the game requirements and checking for errors.
If InitSprite() = 0
    MessageRequester("Error!", "Sprite system can not be initialized.", 0)
   End
EndIf

If InitSprite3D() = 0
    MessageRequester("Error!", "Sprite3D system can not be initialized.", 0)
   End
EndIf

If InitKeyboard() = 0
    MessageRequester("Error!", "Keyboard can not be initialized.", 0)
   End
EndIf
 
If InitMouse() = 0
    MessageRequester("Error!", "Mouse can not be initialized.", 0)
   End
EndIf
 
If InitJoystick() = 0 ;Generic auto joystick initializing.
    Gamepad = 0
  Else
    Gamepad = 1
EndIf
 
UsePNGImageDecoder() ;For nice looking graphics, 32bit png images with alpha channel. But not required.

If OpenScreen(#ScreenWidth, #ScreenHeight, #ScreenDepth, #GameTitle, #PB_Screen_SmartSynchronization)
  
;-Draw Grid
  ;Temporary grid for editing. Remove grid code when done editing the game. 
        
       CreateSprite(99999, #GridSize, #ScreenHeight, #PB_Sprite_AlphaBlending | #PB_Sprite_Texture)
        StartDrawing(SpriteOutput(99999))
         DrawingMode(#PB_2DDrawing_Default)
          LW = 4
           For GridWidth = 0 To #GridSize Step #GridTile
             If LW = 4
               Line(GridWidth, 0, 1, #ScreenHeight , RGBA(255, 0, 0, 0))
               LW = 1
              Else
               Line(GridWidth, 0, 1, #ScreenHeight , RGBA(255, 255, 255, 0))
               LW +1
             EndIf
           Next GridWidth
           For GridHeight = 0 To #ScreenHeight Step #GridTile
             If LH = 4
               Line(0, GridHeight, #ScreenHeight, 1 , RGB(255, 0, 0))
               LH = 1
              Else
               Line(0, GridHeight, #ScreenHeight, 1 , RGB(255, 255, 255))
               LH +1
             EndIf
           Next GridHeight
         StopDrawing()
        TransparentSpriteColor(99999, RGB(0, 0, 0))
  
   Else
    ;Displays an error if the users pc can not use the resolution and/or color depth requirement.    
    MessageRequester("Error!", "Unable to open screen at " + Str(#ScreenWidth) + "x" + Str(#ScreenHeight) + " with " + Str(#ScreenDepth) + "bit color.", 0)
  End
      
EndIf

  Grid = 0 ;Editing grid is disabled until the "G" key is pushed/released.
  GridX = 0 ;Starting position of our scrolling grid.
  Level = 0 ;Game starts at the "Start Screen".
  LoadGraphics = 1 ;Loads needed graphics.

Repeat
  
  ExamineKeyboard()
   FlipBuffers()
    ClearScreen(0)
    
;-Start Screen    
    
    If Level = 0 ;The "Start Screen".
      If LoadGraphics = 1
        ;CatchSprite(0, ?Pic0, #PB_Sprite_AlphaBlending | #PB_Sprite_Texture) ;Load some graphics.
        LoadGraphics = 0
      EndIf
      
        ;DisplayTransparentSprite(1, 10, 10) ;Display our "Start Screen" graphics.
        
      If KeyboardPushed(#PB_Key_Return) ;Or you mouse click on the "Start" button.
         ;FreeImage() ;Free the graphics. May need a For/Next loop to free multiple images.
         Level = 1 ;While a new game would start at level 1, it would be even better if you create a saved game setting and loaded it.
         LoadGraphics = 1
      EndIf
       
      If KeyboardReleased(#PB_Key_Escape) ;Closes the game.
         ;FreeImage() ;Free the graphics. May need a For/Next loop to free multiple images.
         End
      EndIf
    EndIf
    
;-Level 1    
    
    If Level = 1
      
      ;Same as the "Start Screen" pretty much.
      
      If KeyboardReleased(#PB_Key_Escape) ;Goes back to the "Start Screen".
         ;FreeImage() ;Free the graphics. May need a For/Next loop to free multiple images.
         Level = 0
      EndIf
      
    EndIf
    
;-Display Grid
  ;Temporary grid for editing. Should be drawn last or over sprites. Remove grid code when done editing the game.
           
      If KeyboardReleased(#PB_Key_G)   
        If Grid = 1
          Grid = 0
         Else 
          Grid = 1
        EndIf
      EndIf
      
      If Grid = 1     
          For GridTile = 0 To #LevelWidth Step #GridSize
           DisplayTransparentSprite(99999, GridX +GridTile, 0)
          Next GridTile
      
         StartDrawing(ScreenOutput())
          DrawingMode(#PB_2DDrawing_Default)
           For GridText = 0 To #LevelWidth Step #GridMarker
             DrawText(GridX +GridText, 0, Str(GridText), RGB(255, 255, 255))
           Next GridText
         StopDrawing()
           
         StartDrawing(ScreenOutput())
          DrawingMode(#PB_2DDrawing_Default)
           For GridText = #GridMarker To #ScreenHeight Step #GridMarker
             DrawText(0, GridText, Str(GridText), RGB(255, 255, 255))
           Next GridText
         StopDrawing() 
       EndIf  
 
;-Keyboard

      If Not KeyboardPushed(#PB_Key_Left) ;Make sure the "Left Cursor" key isn't still being held down.
       If KeyboardPushed(#PB_Key_Right)
        If Grid = 1
         If GridX > -#LevelWidth + #ScreenWidth
           GridX -5
         EndIf
        EndIf
       
        ;More scrolling variables go here.
       
       EndIf
      EndIf
      
      If Not KeyboardPushed(#PB_Key_Right) ;Make sure the "Right Cursor" key isn't still being held down.
       If KeyboardPushed(#PB_Key_Left)
        If Grid = 1
         If GridX < 0
           GridX +5
         EndIf
        EndIf
       
        ;More scrolling variables go here.
       
       EndIf
      EndIf
      
;-Joystick      
         
     If Gamepad = 1 ;If a joystick is detected it will be available.
       
       ExamineJoystick(0)
       
        If JoystickAxisX(0) = 1 ;Right
          If Grid = 1
           If GridX > -#LevelWidth + #ScreenWidth
             GridX -5
           EndIf
          EndIf
         
         ;More scrolling variables go here.
         
        EndIf
        
        If JoystickAxisX(0) = -1 ;Left
          If Grid = 1
           If GridX < 0
             GridX +5
           EndIf
          EndIf
         
         ;More scrolling variables go here.
         
        EndIf      
        
      EndIf      
      
   Delay(1) ;Save the cpu from maxing out.
   
ForEver

;Include the graphics in the executable.
DataSection
 ;Pic0: IncludeBinary "image 0.png"
 ;Pic1: IncludeBinary "image 1.png"
 ;Pic2: IncludeBinary "image 2.png"
EndDataSection

Re: Basic 2D Game Template

Posted: Tue Dec 14, 2010 4:35 pm
by epidemicz
Hey this is cool, thanks! A friend of mine is interested in learning PB, gonna use this to help him.