Blog

All News | All Blogs

Programming for video game (super basic)

Making a video game, this is much to do. It is easy to throw the game after 3 minute-play, but making games requires many steps and processes.

Here I want to focus programmers. They are the smartest in a development team and they make a core, a heart of games. Graphic engine, Audio engine, AI, etc... If programmers are not good, the game will never be fun. They need not only skills and technique, but also need creativity and productivity of fun game. Good programmers can make UI menu comfortable and smooth, AI smart, powerful graphics... Even the timing of sound effects really matters on comfortableness.
You may have experience that Super Mario is very comfortable to do a jump action because timing of sound effects and moves are natural, but some other similar type games are not as comfortable for some reasons. There is a difference of skills and sense of programmer.

I guess that the biggest question is, "How does the program work for games?” First you need to understand how the TV/monitor works. They update the screen 60 times a second for example (It depends on monitor systems). The game provides new picture every frame just like how cartoon works. The difference is that the game updates the picture on the fly based on the interaction while cartoon shows prepared pictures.
So, how you move the ball from the left edge to the right edge of the TV screen? First you know the frame rate, 60 frames per second. Secondly you need screen aspects like 320x240 and size of the ball of radius 8 pixel. It should look like this.

(0,0)                                         (320,0)
+-------------------------------------------+
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|(8, 160)>>>>>>>>>>>>>>>>>>>>(232, 160)|
|o____________________________________o|
|START                                             GOAL|
|                                                             |
|                                                             |
|                                                             |
+-------------------------------------------+
(0,240)                                   (320,240)

Horizontal position of the center of the ball is X. The ball touches the left edge of the screen now, so value X is 8. The goal of the ball is to touch the right edge of the screen, so value X will be 232 (240 - 8).
START X = 8
GOAL X  = 232
If you want the ball finish moving in just 1 second, the ball should move from left to right in 60 times of screen updates.
So we need those functions to move the ball as you wish. Function is an action command chunk so to speak .

// function to initialize values. Called once before start moving.
function BallInitialize()
{
    ballX = 8;
    ballY = 168;
    frameCount = 0;
}

// function to move the ball. (update X value) called every frame
function BallMove()
{
    ballX = 8 + (232-8) * frameCount / 60;
    if(232 < ballX)    ballX = 232;
    frameCount = frameCount + 1;
}

In BallInitialize(), ballX is set to the starting point. It also resets ballY, height of the ball to 160. so now ball is at (8,160). frameCount needs to be initialized, too.
The function will be called only once before moving the ball but the merit is that you can call this function when you want to redo the action. For example, you can call this function every time button A on the controller is pressed and the ball will jump back to the original place and start moving.

In ballMove(), ballX value is calculated using frameCount value. The frameCount will be incremented every frame so ballX gets greater every frame at the same time.
If ballX gets greater than GOAL X, force it to stay there.
frameCount is incremented in the last line.
frameCount and ballX will be updated as frameCount increased like this.

fr    bX
0    8
1    12
2    15
3    19
=-=-=-=-=
58    225
59    228
60    232

This is extremely simple example, but this is how we move the object in the game basically. This is 2D base and 3D is much more complex that I cannot explain in here. Also the real code needs more lines to define everything accurately. You need to understand many logics and I should be paid =). Hmm... how about opening game programming school after retirement?
Anyway, it should show the ball like moving smoothly from left to right.

Just for your information, I put more realistic code version in C++ here; (I did not try compiling and this is my style of coding)

//=====================================================================
// static variables
static int g_ballX;
static int g_ballY;
static int g_frameCount;
// constant variables
static const int s_ballX_start = 8;            // tsrating position
static const int s_ballX_end   = 240-8;        // goal position
static const int s_ballY_0     = 160;        // height
static const int s_frameCount_max = 60;        // duration

void BallInitialize(void)
{
    g_ballX = s_ballX_start;    // set ballX to the startiing position
    g_ballY = s_ballY_0;        // set ballY(height)
    g_frameCount = 0;            // reset frameCount
}

bool BallMove(void)
{
    g_ballX = s_ballX_start + (s_ballX_end - s_ballX_start) * g_frameCount / s_frameCount_max;
    // check if the ball reach to the end
    if(s_ballX_end < g_ballX)    // can be also "if(g_frameCount_max <= g_frameCount)" instead
    {
        g_ballX = s_ballX_end;
        return true;
    }
    else
        g_frameCount = g_frameCount + 1;
    return false;
}

Hiro@LA

Published Friday Jun 27, 2008