This topic is now archived and is closed to further replies.


Implementing game states

Recommended Posts

K, here''s the problem. I had my game up and running with it all in the Game_Main function. I then decided to split it up into various states so for example when all the blocks had been removed from the screen the next level and another set of blocks were put on the screen. I added: - //states for the game loop #define GAME_STATE_INIT 0 #define GAME_STATE_START_LEVEL 1 #define GAME_STATE_RUN 2 #define GAME_STATE_SHUTDOWN 3 #define GAME_STATE_EXIT 3 Then I created a var as a global to hold the various states: - int game_state = GAME_STATE_INIT; // holds the game_state This is my Game_Main function: -
int Game_Main(void *parms, int num_parms)
// this is the workhorse of your game it will be called

// continuously in real-time this is like main() in C

// all the calls for you game go here!


if (game_state == GAME_STATE_INIT)
	// set the random generator so each game is different



if (game_state == GAME_STATE_START_LEVEL)
	blocks_hit = 0;
	game_state = GAME_STATE_RUN;

if (game_state == GAME_STATE_RUN)
	// clear the drawing surface

	//DDraw_Fill_Surface(lpddsback, 0);

	// lock the back buffer


	//draw the background reactor image

	Draw_Bitmap(&reactor, back_buffer, back_lpitch, 0);
	Draw_Bitmap(&ball, back_buffer, back_lpitch, 1);
	Draw_Bitmap(&paddle, back_buffer, back_lpitch,1);

	if (blocks_hit >= (NUM_BLOCK_ROWS*NUM_BLOCK_COLUMNS))
			game_state = GAME_STATE_START_LEVEL;

	ball.x +=ball_dx;
	ball.y +=ball_dy;

	if (ball.x > (SCREEN_WIDTH - BALL_SIZE) || ball.x < 0)
	ball_dx = -ball_dx;
	ball.x += ball_dx;

	if (ball.y > (SCREEN_HEIGHT - BALL_SIZE) || ball.y < 0)
	ball_dy = -ball_dy;
	ball.y += ball_dy;

	if (ball_dx > 2) ball_dx = 2;
	if (ball_dy > 2) ball_dy = 2;

	if (keyboard_state[DIK_RIGHT])
	if (paddle.x > (SCREEN_WIDTH - PADDLE_WIDTH))
	if (keyboard_state[DIK_LEFT])
	if (paddle.x < 0)
		paddle.x = 0;

	// unlock the back buffer



	// Draw the current score

	sprintf(buffer, "SCORE %d",score);

	// Draw the current level to the screen

	sprintf(buffer, "LEVEL %d",level);
	Draw_Text_GDI(buffer, 450, 10,RGB(0,255,0),lpddsback);

	//Draw the amount of blocks hit to the screen

	sprintf(buffer, "BLOCKS HIT %d",blocks_hit);
	Draw_Text_GDI(buffer, 330, 10,RGB(0,255,0),lpddsback);

	// flip the surfaces



// check of user is trying to exit

if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
    PostMessage(main_window_handle, WM_DESTROY,0,0);
	game_state = GAME_STATE_SHUTDOWN;


if (game_state == GAME_STATE_SHUTDOWN)
	// kill the reactor

	Destroy_Bitmap(&reactor); // Get rid of background image

	// shutdown directdraw


	game_state = GAME_STATE_EXIT;


} // end Game_Main

I am using this to check if another level needs to be done: - if (blocks_hit >= (NUM_BLOCK_ROWS*NUM_BLOCK_COLUMNS)) { game_state = GAME_STATE_START_LEVEL; level++; } No matter where I put this it still does not draw a fresh set of blocks on the surface once the others have been deleted. It is working kind of because the level variable increases and block_hits which tracks the amount of blocks which have been hit is reset to 0. I have tried changing the above statement to 48 (the maount of blocks) and this doesn''t work. As far as I can see nothing is missing and it seems to be going through the game states just not drawing the blocks. One thing which it could be is I do not initialize the blocks in a statement I initialize them in the array but even when I put the array in the GAME_STATE_START_LEVEL still no joy. If I haven''t made an ounce of sense or missed off some needed code then let me know. Help appreciated. Paul

Share this post

Link to post
Share on other sites
There is no problem with the level checking code, I would guess your problem is with reseting all block, ball and paddle varibles. All items need to be reset to an inital state.

I would first check blocks (I''ve seen your code before and you initalize them when the vars are created).

try writing a function to setup block data based on a level parameter, and call that function each instance of a new level (inc level 1).


Void SetBlocks(int Level) {
int X, int y;

if level==1 {
for (x=0, x<NUM_BLOCK_ROWS,x++) {
for (y=0, y<NUM_BLOCK_COLUMNS,y++) {


It would be better in a switch statement, but I can''t remember the syntax.


Share this post

Link to post
Share on other sites