The First Program: Jumping Cone

;demo01-01.bb - Jumping Cone, your first program!
;------------------------------------------------

;Set up the graphics
Graphics3D 640, 480
SetBuffer BackBuffer()
;Define constants
Const ESC_KEY    = 1
Const UP_KEY     = 200
Const DOWN_KEY   = 208

;Define variables
camdistance = 10
type_player = 1
type_ground = 2

;track the position and jump of the cone
velocity#    = 0

This is the end of the declaration section of the code. This part sets up the important variables for the program as well as some constants. (Don’t worry; you will be introduced to all of this as the book progresses.)

After the declaration, we begin the initialization. Initialization is the process of setting up everything that will be used in the program—in this section, the initialization section sets up the shapes and objects on the screen.

;Create the camera, which is what the user sees
camera=CreateCamera()
RotateEntity camera, 45, 0, 0

;Create the light, so the user can see!
light=CreateLight()

;Create a plane and texture, the actual objects on the screen
;Create texture
grid_text = CreateTexture(32, 32, 8)
ScaleTexture(grid_text, 10, 10)

;Draw the texture, and reset the drawing back to the main screen
SetBuffer TextureBuffer(grid_text)

;Create two rectangles
Color 0,0,64
Rect 0,0,32,32
Color 0,0,255
Rect 0,0,32,32
SetBuffer BackBuffer()

;Create the plane
grid_plane = CreatePlane()


CreateMirror()

HandleEntities(grid_plane, grid_text)

;Create a cone
cone = CreateCone(64)
EntityType cone, type_player
PositionEntity cone, 0, 1, 5
ScaleEntity cone, 0.4, 0.4, 0.4
EntityRadius cone, 0.5

;Create texture for the cone
texture = CreateTexture(16,16):SetBuffer TextureBuffer(texture)
ClsColor 256, 6, 56
Cls
;assign a color for the texture
Color 12, 126, 256
Rect 0, 0, 8, 8, 1
Rect 8, 8, 8, 8, 1
ScaleTexture texture, .2, .2
;put the texture on the cone
EntityTexture cone, texture

The initialization section sets up some important variables for the game, such as the score and the player variables. These variables keep track of where the cone is located and how fast it’s jumping.

After initialization, the actual loop begins:

;MAIN LOOP
While Not KeyDown(ESC_KEY)

     ;Handle User Input
     If KeyDown(UP_KEY)
          velocity = velocity - .001
     EndIf
     If KeyDown(DOWN_KEY)
          velocity = velocity + 0.001
     EndIf
     ;Move the cone and camera around to see everything
     MoveEntity cone, 0, velocity, 0
     TranslateEntity cone, 0, -0.03, 0
     PositionEntity camera, EntityX(cone), 0, EntityZ(cone)
     MoveEntity camera, 0, 0, -5

     ;Test for collisions so things can't go through walls
     Collisions type_player, type_ground, 2, 2
     Collisions type_ground, type_player, 2, 2

     UpdateWorld
     RenderWorld
     Flip

     Wend ;END MAIN LOOPS

End ;End of the program

What Is a Frame?

I am about to reference the word frame a bunch of times in a few moments, and you should know what it means. A frame is the screen at any given moment. A game can be compared to an animated film---both are made up of a bunch of different pictures that, when put together, create animation. The frames blend together so quickly that the objects on the screen appear to be moving. An average game runs at 30 frames per second, which means 30 pictures on the screen are blended together each and every second.


This is the end of the main loop. To put it bluntly, the main loop is the entire game. Every frame of a game is a single iteration of the main loop. By the way, a loop causes some code to be repeated over and over until some condition becomes false. Here, the condition is that the Esc key has not been pressed. Usually, the main loop is a while loop, shown here in the line

While Not KeyDown(ESC_KEY)

At this point, the actual game loop has been completed, so we must now define the functions. A function is called with its name followed by parentheses; for example, HandleEntities(). Functions are like little helpers that perform specific activities that we want to do over and over. If you look at the main loop, you will see that most of these functions are called from there, and some others are called from within other functions.

;FUNCTIONS

;Function HandleEntities() - this function assigns the texture to the grid plane
and deals with Entities
Function HandleEntities(grid_plane, grid_text)
     ;Make texture and plane appear on screen
     EntityTexture grid_plane, grid_text
     EntityBlend grid_plane, 1
     EntityAlpha grid_plane, .6
     EntityFX grid_plane, 1
     EntityType grid_plane, type_ground

End Function

This function simply makes the ground become the ground—it sets the texture color and makes it look pretty.

Figure 1.7 shows the Jumping Cone program!

Figure 1.7. The Jumping Cone!


Compiling the Code

Compiling the code is a very simple procedure. Just open the file (demo01-01.bb) off the CD in Blitz3D (or type it into the workspace), save the file (File > Save) onto your computer, and select Program > Run Program.

Well, that isn’t what you would call a full game. I did not add any special effects or sounds because they aren’t very important at this point. The idea is to get a feel for what code looks like and how it is written. You will notice that the meanings of most of the functions are easy to understand because of the function names. This helps in understanding the program.

Let me summarize the main parts of a game. The game consists of:

  • The initialization section

  • The main loop

  • The shutdown

Initialization sets up variables and functions that are used throughout the game. Declaration is part of initialization and is used to set up variables that will be used later in the program. The game loop is what you see on the screen. Each iteration (an iteration is each time the program runs through the loop) of the loop is one frame of the game. Usually, there are at least 30 frames, or iterations, per second.

The shutdown sequence is the final part of the game, and it runs just before and during the end of the game. It closes all open files, deletes any running variables, and quits the game.

Of course, there are a few other important parts to any game, but I will go over them with you when learning about them is necessary. For now, read over the commented code (on the CD) and try to understand what in heck is going on. If you follow the functions, it shouldn’t be too hard.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.39.133