This topic was published by Jarret Buse and viewed 1531 times since "". The last page revision was "".
- Topics - 25
Within game programming the basic components and structure are different than a standard program. The basic skeleton of the program needs to be understood to aid in proper game programming.
A standard program is "user driven", which means that the user selects objects on the screen things occur. The program will sit and wait for an object to be selected by either the mouse, keyboard or other input device. Some applications are set to perform specific tasks, such as to be opened and the program will open a specific file and optimize it or some other type of maintenance. Programs like this require no user interaction since they are designed to perform a single task.
Games are not usually "user-driven". As with all things, there are exceptions to the rules. Some games which are not time based or have no "enemies" trying to kill the player is one example of an exception. Say for instance you have a one player card game like Solitaire. In this instance, the game is completely "user-driven". When most people think of video games, the first thought that comes to mind is usually not a "user-driven" game.
Since most games are not "user-driven", the code must continually be processed and the screen updated. Even a simple game of Pong can illustrate the idea.
NOTE: For those who may not know about Pong, it is a game where one user has a small rectangle on one side of the screen which can be only be moved up and down. Another person, or the CPU, has another rectangle on the other side of the screen which also moves only up and down. A ball bounces between the two and the player must bounce the ball back to the opponent. When one player misses the ball, the other player gets a point. It is a basic digital version of tennis or ping-pong.
In Pong, if the player quits playing the game does not stop and the player will lose the game. If someone quits playing solitaire, the game is on hold until they return and continue playing.
So, what is the layout of these concepts to make it work this way?
After initialization and the loading of images, sounds and other files, the game enters into a loop. The loop continues until the system recognizes that the player has lost or they have selected to quit the game.
The loop is a basic one and is sometimes referred to as the Game Loop. The Game Loop has two steps. The steps are:
The Update portion will update items and variables. For instance, timers can be counting, enemies can be moving or attacking, points can be changing and many other various things can be occurring. As all of these items are updated, the changes must take effect visually, which is the second step.
The second step is the Draw portion of the Game Loop. The step is a simple one in which the screen is refreshed to show all the changes made in the Update portion.
If we remove the Update step, you can imagine what would happen in a simple game like Pac-Man. Since the movement of the ghosts would not be changed, they would suddenly stop moving. Pac-Man would not have its placement updated to move and when the images are redrawn, everything would still be in the same spot. The game would in fact "freeze".
If the draw portion is removed from the Game Loop, then everything is still moving, but the player could not tell where anything was located. Again, in Pac-Man, this would mean the Pac-Man and the ghosts are somewhere in the maze on the screen, but you have no idea where they can be since the images on the screen are not moving.
NOTE: The Game Loop is executed about 60 times a second depending on the system speed and the amount of code within the Game Loop. When the Draw subroutine is called, the screen is cleared and completely redrawn about 60 times a second. Every time the screen is redrawn, the image is referred to as a frame. Similar to animation, the frames work together to produce movement.
Hopefully, this helps you to understand the Game loop design.
Other portions of the XNA game code template in Visual Basic are the following:
These are the base Subroutines used in an XNA game whether it is for Windows, Windows Phone or the XBOX360.
- Initialize – used to set up variables and any initialization needed which is not graphical based such as images. To re-initialize the game, call MyBase.Initialize from the code.
- LoadContent – subroutine used to load graphics and other sprites. Sprites can be graphics, audio, fonts, etc. Only needed once per program execution.
- UnloadContent – subroutine used to unload all the content loaded during the LoadContent subroutine. Only needed once per program execution.
- Update – used to manage all the movement, updating of times and scores, playing background music, etc. Players can also exit the game during this subroutine.
- Draw – performs screen refresh to update all the information which was updated in the Update subroutine.
- XNA Reading Guide - https://dcjtech.info/topic/gaming/#xna