Basic Game Programming With FreeBASIC - Rev1
Basic Game Programming With FreeBASIC - Rev1
The following sample codes is only used to demonstrate the very basics of Game Programming, which
are:
1. Initializing Graphics library and creating a Screen
2. Loading and Displaying an Image
3. Making a simple Animation
4. Putting it all together
5. Cleaning up the code
Before you start reading, please bear in mind that I am not claiming that these are the best approach to
doing the tasks listed above. Just like any other programming, each problem can be done with different
algorithms. My approach is not a standard design pattern for game programming.
Do
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
Dim x as Integer
Dim y as Integer
x = 100
y = 150
If stand = NULL _
Then
Print "Image creation failed!"
Sleep
End
End If
Do
Put (x,y), stand
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
ImageDestroy stand
Dim x as Integer
Dim y as Integer
x = 100
y = 150
If stand = NULL _
Then
Print "Image creation failed!"
Sleep
End
End If
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
ImageDestroy stand
Dim x as Integer
Dim y as Integer
x = 100
y = 150
Do
Cls
standCtr = standCtr + 1
If standCtr >= 60 Then standCtr = 0
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
ImageDestroy stand1
ImageDestroy stand2
Dim x as Integer
Dim y as Integer
x = 100
y = 150
Do
Cls
standCtr = standCtr + 1
If MultiKey(SC_RIGHT) Then
face = FACE_RIGHT
ElseIf MultiKey(SC_LEFT) Then
face = FACE_LEFT
End If
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
ImageDestroy standR1
ImageDestroy standR2
ImageDestroy standL1
ImageDestroy standL2
Dim x as Integer
Dim y as Integer
x = 100
y = 150
Do
Cls
If state = STATE_STAND Then
standCtr = standCtr + 1
If standCtr >= 60 Then standCtr = 0
If MultiKey(SC_RIGHT) Then
state = STATE_RUN
x = x + 2
face = FACE_RIGHT
ElseIf MultiKey(SC_LEFT) Then
state = STATE_RUN
x = x - 2
face = FACE_LEFT
Else
state = STATE_STAND
End If
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
ImageDestroy standR1
ImageDestroy standR2
ImageDestroy standL1
ImageDestroy standL2
ImageDestroy runR1
ImageDestroy runR2
ImageDestroy runR3
ImageDestroy runR4
ImageDestroy runR5
ImageDestroy runR6
ImageDestroy runL1
ImageDestroy runL2
ImageDestroy runL3
ImageDestroy runL4
ImageDestroy runL5
ImageDestroy runL6
Dim x as Integer
Dim y as Integer
x = 100
y = 150
Do
Cls
If state = STATE_STAND Then
standCtr = standCtr + 1
If standCtr >= 60 Then standCtr = 0
If MultiKey(SC_UP) Then
If state <> STATE_JUMP Then
state = STATE_JUMP
End If
End If
If MultiKey(SC_RIGHT) Then
If state <> STATE_JUMP Then
state = STATE_RUN
End If
x = x + 2
face = FACE_RIGHT
kickCtr = 0
punchCtr = 0
ElseIf MultiKey(SC_LEFT) Then
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
ImageDestroy standR1
ImageDestroy standR2
ImageDestroy standL1
ImageDestroy standL2
ImageDestroy runR1
ImageDestroy runR2
ImageDestroy runR3
ImageDestroy runR4
ImageDestroy runR5
ImageDestroy runR6
ImageDestroy runL1
ImageDestroy runL2
ImageDestroy runL3
ImageDestroy runL4
ImageDestroy runL5
ImageDestroy runL6
ImageDestroy kickR1
ImageDestroy kickR2
ImageDestroy kickR3
ImageDestroy kickR4
ImageDestroy kickL1
ImageDestroy kickL2
ImageDestroy kickL3
ImageDestroy kickL4
ImageDestroy kickhiR1
ImageDestroy kickhiR2
ImageDestroy punchR1
ImageDestroy punchR2
ImageDestroy punchR3
ImageDestroy punchR4
ImageDestroy punchL1
ImageDestroy punchL2
ImageDestroy punchL3
ImageDestroy punchL4
ImageDestroy jumpR1
ImageDestroy jumpR2
ImageDestroy jumpL1
ImageDestroy jumpL2
Return tmp
End Function
Dim x as Integer
Dim y as Integer
x = 100
y = 150
Dim i As Integer
For i = 0 To 2 - 1
standR(i) = Image_Load(47,88,"char1/stand/r/" & (i+1) & ".bmp")
standL(i) = Image_Load(47,88,"char1/stand/l/" & (i+1) & ".bmp")
For i = 0 To 6 - 1
runR(i) = Image_Load(72,77,"char1/run/r/" & (i+1) & ".bmp")
runL(i) = Image_Load(72,77,"char1/run/l/" & (i+1) & ".bmp")
Next
For i = 0 To 4 - 1
kickR(i) = Image_Load(84,82,"char1/kick/r/" & (i+1) & ".bmp")
kickL(i) = Image_Load(84,82,"char1/kick/l/" & (i+1) & ".bmp")
Do
Cls
If state = STATE_STAND Then
standCtr = standCtr + 1
If standCtr >= 60 Then standCtr = 0
If MultiKey(SC_UP) Then
If state <> STATE_JUMP Then
state = STATE_JUMP
End If
End If
If MultiKey(SC_RIGHT) Then
If state <> STATE_JUMP Then
state = STATE_RUN
End If
x = x + 2
face = FACE_RIGHT
kickCtr = 0
punchCtr = 0
ElseIf MultiKey(SC_LEFT) Then
If state <> STATE_JUMP Then
state = STATE_RUN
End If
x = x - 2
face = FACE_LEFT
kickCtr = 0
punchCtr = 0
ElseIf MultiKey(SC_S) Then
state = STATE_KICK
ElseIf MultiKey(SC_A) Then
state = STATE_KICK_HI
ElseIf MultiKey(SC_D) Then
state = STATE_PUNCH
Else
If (state = STATE_KICK Or state = STATE_KICK_HI) And kickCtr <> 0 Then
'DO NOTHING
ElseIf state = STATE_PUNCH And punchCtr <> 0 Then
'DO NOTHING
ElseIf state = STATE_JUMP And jumpCtr <> 0 Then
'DO NOTHING
Else
state = STATE_STAND
End If
Sleep 10
Loop Until MultiKey(SC_ESCAPE)
For i = 0 To 2 - 1
ImageDestroy standR(i)
ImageDestroy standL(i)
ImageDestroy jumpR(i)
ImageDestroy jumpL(i)
Next
For i = 0 To 6 - 1
ImageDestroy runR(i)
ImageDestroy runL(i)
Next
For i = 0 To 4 - 1
ImageDestroy kickR(i)
ImageDestroy kickL(i)
ImageDestroy kickhiR(i)
ImageDestroy kickhiL(i)
ImageDestroy punchR(i)
ImageDestroy punchL(i)
Next
Sleep
Resources
You can get a PDF copy of this article and the images used plus with source code and compiled binary
from the site indicated below.
http://konsolscript.org/~mjmendoza/freebasic/basic-game-programming-with-freebasic/