Good Programming Techniques/Habits

Show Index

You are free to program in any style you wish, but here on this page are listed some techniques and habits that many programmers have found to be worth putting in the effort to do.

 

Code Indentation

Typical game code structure

modularize your code - todo...

simplify (don't make redundant code) - todo...

collecting user input - todo...

 

Code Indentation

Many programmers have found that indenting/structuring their code helps them immensely when trying to follow their programs flow.

Typically, conditional statements (If, Select)  or loops (While, Repeat, For) are indented to clarify which lines of code are contained within their boundaries.

Take for example the two code snippets on the right.  The upper snippet is not indented, and the lower snippet is.  They both do the exact same thing but the indented version makes it much easier to understand what's happening within that code, doesn't it?

With the indented version (lower snippet) you can quickly see which sections of code are executed or not executed depending on the outcome of the If statements.  For example, if the If move_box = True statement evaluated to False, you can quickly see that none of the code listed would be executed. 

However, with the non-indented version (upper snippet), if the If move_box = True statement evaluated to False, you must really study the entire snippet and read it through thoroughly to determine which parts will or won't be executed.

Clearly the lower snippet is easier to read and understand and is more efficient.

If move_box = True
If KeyDown(30) TurnEntity box, 0, 1, 0
If KeyDown(32) TurnEntity box, 0, -1, 0
If KeyDown(31) MoveEntity box, 0, 0, -.01
If KeyDown(17) MoveEntity box, 0, 0, .01
If EntityCollided(box, terrain)
If KeyDown(17)
speed = speed + .001
If speed > .5 Then speed = .5
Else If KeyDown(31)
speed = speed - .02
If speed < -.5 Then speed = -.5
Else
speed = speed * .99
EndIf
MoveEntity box, 0, 0, speed
TranslateEntity box, 0, -.01, 0
Else
TranslateEntity box, x_vel, y_vel, z_vel
EndIf
EndIf
If move_box = True
  If KeyDown(30) TurnEntity box, 0, 1, 0
  If KeyDown(32) TurnEntity box, 0, -1, 0
  If KeyDown(31) MoveEntity box, 0, 0, -.01
  If KeyDown(17) MoveEntity box, 0, 0, .01
  If EntityCollided(box, terrain)
    If KeyDown(17)
      speed = speed + .001
      If speed > .5 Then speed = .5
    Else If KeyDown(31)
      speed = speed - .02
      If speed < -.5 Then speed = -.5
    Else
      speed = speed * .99
    EndIf
    MoveEntity box, 0, 0, speed
    TranslateEntity box, 0, -.01, 0
  Else
    TranslateEntity box, x_vel, y_vel, z_vel
  EndIf
EndIf
Typical Game Code Structure

Many games can use the same basic code structure.  Most games perform their initializations at the beginning, have their game loop in the middle, and include their additional functions near the end.

You don't have to follow this structure exactly, but it's shown here to give you an idea of how a typical game could be set up.

Note, of course that the code shown is only pseudo-code.

;Name of my game
;Made by Me
;Copyright 2008

;Type definitions
Type coordinate
  Field x
  Field y
End Type

;array declarations
Dim board(40, 30)

;Variable/Constant initializations
Const red = $FF0000
Global level = 1

;graphics initialization
Graphics 800, 600
SetBuffer BackBuffer()
SeedRnd MilliSecs()

;media loading routines
Player = LoadAnimImage("playerimage.png", 16, 16, 1, 8)
Music = LoadSound("bgmusic.ogg")

;start of game loop
While Not KeyHit(Escape_Key)
  ;collect player input
  leftkey = KeyDown(203)

  ;process game logic
  If somethinghappened Then DoSomething()

  ;draw screen
  RenderWorld()
  Flip

;end of game loop
Wend

;Function definitions
Function DoSomething()
  ;useful function code here
End Function

;Data statements
Data 23, 17 ;start position
Data 4 ;number of gold bars

End
Up and coming tip...

Nothing here yet

Good habits are better than no habits...
Up and coming tip...

Nothing here yet

Up and coming tip...

Nothing here yet

 

 

If you've reached this page and there's no index on the left, Click here to show the Index Page