Chapter 2: Light Cycle
This is the code for the light cycle program listed in Chapter 2. I have preserved the C comments for reference into the C source. My changes are marked with RDC.
'Light.bas
'Example program from Chapter 2 of Black Art of 3D Game Programming by Andre LaMothe
'Converted from C to FreeBasic by Richard Clark
'I have included the C comments as a reference point for the C source.
'Copyright The Waite Group (c) 1995
'==================================================================================
'// LIGHT.C - An example of real-time event loops
'// I N C L U D E S ////////////////////////////////////////////////////////////
'RDC: None needed
'// D E F I N E S /////////////////////////////////////////////////////////////
'// directions
#Define NORTH 0
#Define EAST 1
#Define SOUTH 2
#Define WEST 3
'Color const (not in C source)
Const fbWhite = Rgb(255, 255, 255)
Const fbBlue = Rgb(0, 0, 255)
'Key Const (Ont in C source) used in inkey
Const xk = Chr(255)
Const key_right = xk + "M"
Const key_left = xk + "K"
Const key_close = xk + "k"
Const key_esc = Chr(27)
'// F U N C T I O N S /////////////////////////////////////////////////////////
Sub Delay(clicks As Integer)
'// this function uses the internal timer to delay a number of clock ticks
Dim dnow As Double
'// get current time
dnow = Timer
'// Wait for number of click to pass
Do
Sleep 1 'Need to do this so system can run other tasks
Loop Until Timer > (dnow + clicks)
End Sub '// end Delay
'//////////////////////////////////////////////////////////////////////////////
Sub Draw_Game_Grid
'// This function draws a game grid out of horizontal and vertical lines
Dim As Integer x, y
'// the the line color to white
'Will set color in line statement (see below)
' RDC: I am going to use 640x480 in these examples with 32bit color.
'// draw the verical lines
ScreenLock
Cls
For x = 0 To 640 Step 20
'// position the pen and draw the line
Line (x, 0) - (x, 479), fbWhite
Next '// end for x
'// draw the horizontal lines
For y = 0 To 479 Step 20
'/ position the pen and draw the line
Line (0, y) - (639, y), fbWhite
Next '// end for y
'RDC: make a nice border around screen
Line (0, 0)-(639, 479), fbWhite, B 'B = make a Box
ScreenUnLock
End Sub 'Draw_Game_Grid
'// M A I N ////////////////////////////////////////////////////////////////////
Dim As Integer done = 0 '// main event loop exit flag
Dim As Integer player_x = 160 '// starting position and direction of player
Dim As Integer player_y = 150
Dim As Integer player_direction = NORTH
Dim As String ch 'RDC: use inkey for this since Multikey is too fast.
'// SECTION 1 //////////////////////////////////////////////////////////////////
'// set the graphics mode to (rdc: screen 18, 540x480, 32bit color)
Screen 18, 32
'//draw the game grid
Draw_Game_Grid
'// SECTION 2 //////////////////////////////////////////////////////////////////
'// begin real time event loop
Do
'// SECTION 3 //////////////////////////////////////////////////////////////////
'// is the player steering his light cycle or trying to exit?
'// is the player steering his light cycle or trying to exit?
ch = InKey
'// test for Left, Right or Esc/Close
If ch <> "" Then
'// turn 90 to the left
If ch = key_left Then
player_direction -= 1
If player_direction < NORTH Then player_direction = WEST
EndIf
'// turn 90 To the right
If ch = key_right Then
player_direction += 1
If player_direction > WEST Then player_direction = NORTH
EndIf
'// set exit flag to true
If (ch = key_esc) Or (ch = key_close) Then
done = 1
EndIf
End If
'// SECTION 4 //////////////////////////////////////////////////////////////////
'// at this point we need to move the light cycle in the direction its
'// currently pointing
Select Case player_direction
Case NORTH
player_y -= 1
If player_y < 0 Then player_y = 479
Case SOUTH
player_y += 1
If player_y > 479 Then player_y = 0
Case EAST
player_x += 1
If player_x > 639 Then player_x = 0
Case WEST
player_x -= 1
If player_x < 0 Then player_x = 639
End Select '// end switch direction
'// SECTION 5 //////////////////////////////////////////////////////////////////
'// render the lightcyle
Pset (player_x, player_y), fbBlue
'// wait a moment and lock the game to 18 fps
Delay .18
Loop Until done = 1 '// End loop
'// end main