Các ví dụ về games với C++ – Example Games

Các ví dụ về games với C++ – Example Games

Ví dụ 1 – Example 1:

Tic-Tac-Toe

Here is the source code to the game, you will also need the X and O images. This is a very simple version of the game, nothing fancy, no menus, no nothing, just the actuall game was coded in hopes of making it easier for beginners.

#include

BITMAP *xSprite;
BITMAP *oSprite;

int board[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0}; //This will be used to keep
//track of the Xs and Os
int curSquare = 0; //This will keep track of the current square
//the selector is on

int turn = 1; //This will keep track of whose turn it is
//1 Will be for X and 2 for O

int x = 0; //X and Y position of selector
int y = 0;

int tempX = 0; //holds temporary values used to clear selector
int tempY = 0;

void setupBoard(){ //This function will draw in the grid

acquire_screen();

line( screen, 200, 0, 200, 480, makecol( 255, 255, 255));
line( screen, 400, 0, 400, 480, makecol( 255, 255, 255));
line( screen, 0, 150, 680, 150, makecol( 255, 255, 255));
line( screen, 0, 300, 680, 300, makecol( 255, 255, 255));

rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0));

release_screen();

}

void updateBoard(){ //draws in selector

rect( screen, tempX+1, tempY+1, tempX + 199, tempY + 149, makecol( 0, 0, 0));
rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0));
rest(100);
}

void announceWinner(){ //Announces the winner

if( turn == 1){
textout_ex( screen, font, “X Wins!!!!”, 300, 240, makecol( 255, 0, 0), makecol(0, 0, 0));
} else {
textout_ex( screen, font, “O Wins!!!!”, 300, 240, makecol( 255, 0, 0), makecol(0, 0, 0));
}

}

void checkWin(){ //checks for a winner

if( board[0] == turn && board[1] == turn && board[2] == turn){
announceWinner();
} else if( board[0] == turn && board[3] == turn && board[6] == turn){
announceWinner();
} else if( board[0] == turn && board[4] == turn && board[8] == turn){
announceWinner();
} else if( board[1] == turn && board[4] == turn && board[7] == turn){
announceWinner();
} else if( board[2] == turn && board[4] == turn && board[6] == turn){
announceWinner();
} else if( board[2] == turn && board[5] == turn && board[8] == turn){
announceWinner();
} else if( board[3] == turn && board[4] == turn && board[5] == turn){
announceWinner();
} else if( board[6] == turn && board[7] == turn && board[8] == turn){
announceWinner();
}

}

void drawXO(){ //draws in the X and O

acquire_screen();

if(turn == 1){
draw_sprite( screen, xSprite, x, y);
board[curSquare] = 1;
checkWin();
++turn;
} else if( turn == 2){
draw_sprite( screen, oSprite, x, y);
board[curSquare] = 2;
checkWin();
–turn;
}

release_screen();

rest(100);

}

void moveBox(){ //takes input

clear_keybuf();
tempX = x;
tempY = y;

if( key[KEY_UP] && y != 0){

y -= 150;
curSquare -=3;
updateBoard();

} else if( key[KEY_DOWN] && y != 300){

y += 150;
curSquare +=3;
updateBoard();

} else if( key[KEY_RIGHT] && x != 400){

x += 200;
++curSquare;
updateBoard();

} else if( key[KEY_LEFT] && x != 0){

x -= 200;
–curSquare;
updateBoard();

} else if( key[KEY_ENTER] && board[curSquare] == 0){

drawXO();

}

}

int main(){

allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

xSprite = load_bitmap( “x.bmp”, NULL);

oSprite = load_bitmap( “o.bmp”, NULL);

setupBoard();

while( !key[KEY_ESC]){
moveBox();

}

destroy_bitmap( xSprite);
destroy_bitmap( oSprite);

return 0;

}
END_OF_MAIN();

Lets break the code down function by function.

main() runs the normal functions used to setup Allegro and the screen. It includes a call to setupBoard() which will simply draw lines to for the grid and draw in a box so the player knows what square is currently selected.

while ( !key[KEY_ESC]) is the main loop, this keeps the game running until ESC is pressed.

moveBox() takes input from the player and calls different functions depending on the key pressed. If an arrow key is pressed the x or y variable will be adjusted to get the selector to the next square and curSquare is updated so that we can easily find which part of board[] corresponds to the square the player is currently in. updateBoard() is called to simply clear out the old selector and draw in the new one.

If enter is pressed and the current square is not occupied, drawXO() is called. This function will check whose turn it is and draw in the corresponding shape ( X or O ). It then updates board[] so that the current square will be marked as being occupied by either a X or an O. Then it calls checkWin() which checks to see if someone has won, and then changes the turn variable so that the next player can go ( 1 = X’s turn, 2 = O’s turn).

checkWin() checks for a winner. Since drawXO() changes board[curSquare] so that it equals the current users turn, board will have 1’s to represent X’s and 2’s to represent O’s. checkWin() will check to see if there are three 1’s or 2’s in a row, depending on whose turn it is and call announceWinner() if there are.

announceWinner() simply checks whose turn it is (who should be the winner since the turn has not been modified by drawXO() yet) and prints out who the winner is to screen.

Ví dụ 2 – Example 2:

Pong

Pong
Pong requires a little bit more skill to code than the last example, Tic-Tac-Toe. Here we have three objects moving about, we need to know when the ball hits a player’s paddle or someone scores, plus we need to calculate a random number for the direction of the ball. Here is the code:

#include
#include
#include

int ball_x = 320;
int ball_y = 240;

int ball_tempX = 320;
int ball_tempY = 240;

int p1_x = 20;
int p1_y = 210;

int p1_tempX = 20;
int p1_tempY = 210;

int p2_x = 620;
int p2_y = 210;

int p2_tempX = 620;
int p2_tempY = 210;

time_t secs; //The seconds on the system clock will be stored here
//this will be used as the seed for srand()

int dir; //This will keep track of the circles direction
//1= up and left, 2 = down and left, 3 = up and right, 4 = down and right

BITMAP *buffer; //This will be our temporary bitmap for double buffering

void moveBall(){

ball_tempX = ball_x;
ball_tempY = ball_y;

if (dir == 1 && ball_x > 5 && ball_y > 5){

if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y 5 && ball_y < 475){ if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y dir = rand()% 2 + 3;
}else{
–ball_x;
++ball_y;
}

} else if (dir == 3 && ball_x < 635 && ball_y > 5){

if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y dir = rand()% 2 + 1;
}else{
++ball_x;
–ball_y;
}

} else if (dir == 4 && ball_x < 635 && ball_y < 475){ if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y0){

–p1_y;

} else if( key[KEY_S] && p1_y < 420){ ++p1_y; } acquire_screen(); rectfill( buffer, p1_tempX, p1_tempY, p1_tempX + 10, p1_tempY + 60, makecol ( 0, 0, 0)); rectfill( buffer, p1_x, p1_y, p1_x + 10, p1_y + 60, makecol ( 0, 0, 255)); release_screen(); } void p2Move(){ p2_tempY = p2_y; if( key[KEY_UP] && p2_y > 0){

–p2_y;

} else if( key[KEY_DOWN] && p2_y < 420){

++p2_y;

}

acquire_screen();
rectfill( buffer, p2_tempX, p2_tempY, p2_tempX + 10, p2_tempY + 60, makecol ( 0, 0, 0));
rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255));
release_screen();

}

void startNew(){

clear_keybuf();
readkey();
clear_to_color( buffer, makecol( 0, 0, 0));
ball_x = 320;
ball_y = 240;

p1_x = 20;
p1_y = 210;

p2_x = 620;
p2_y = 210;

}

void checkWin(){

if ( ball_x < p1_x){ textout_ex( screen, font, “Player 2 Wins!”, 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0)); startNew(); } else if ( ball_x > p2_x){
textout_ex( screen, font, “Player 1 Wins!”, 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));
startNew();
}

}

void setupGame(){

acquire_screen();
rectfill( buffer, p1_x, p1_y, p1_x + 10, p1_y + 60, makecol ( 0, 0, 255));
rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255));
circlefill ( buffer, ball_x, ball_y, 5, makecol( 128, 255, 0));
draw_sprite( screen, buffer, 0, 0);
release_screen();

time(&secs);
srand( (unsigned int)secs);
dir = rand() % 4 + 1;

}

int main(){

allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

buffer = create_bitmap( 640, 480);

setupGame();

while( !key[KEY_ESC]){

p1Move();
p2Move();
moveBall();
checkWin();

}

return 0;

}
END_OF_MAIN();

Lets break this down. There is a new header file not covered in the lessons so far, time.h. This contains the functions used to get the current time from the system clock, this will be used to calculate the random numbers.

main() runs through the usuall setup for the game and then calls setupGame(). This function draws the screen and calls time() to get the current time. srand() is a function that changes the “seed” that rand() will use to make random numbers, the seed is the initial value rand() will begin with. In this case the seed is set to the current amount of seconds the system clock is displaying, this way there will always be a new seed everytime the program runs so there will never be a repetetive pattern.

dir = rand() % 4 + 1; will give dir a value between 1 and 4. rand() % x will return a random number between 0 and x-1, in this case 0 to 3. Since we want a number from 1 to 4, we simply add 1 to the result.

Once the setup is complete, the main loop begins. p1Move() will move the paddle on the left side of the screen when W and S are pressed, and p2Move() will do the same for the other paddle when up and down are pressed. moveBall() checks to see if it is about to go off the screen, or hit a paddle and change its course accordingly.

checkWin() will see if the ball has passed a paddle and declare a winner if it has. startNew() will simply clear the screen and reset the values for the ball’s and players’ positions.

Ví dụ 3 – Example 3:

Space Fight

Now our games are really starting to get big! Nothing thats not covered in the previous lessons and examples is used here, this example just puts everything together. First you’ll need the graphics. I’ve included them, along with the source, in a .zip file. Since it would take up so much space, I won’t put the source on this page, you can download the zip file here. There was one big change, instead of having a function that handles all the drawing,each object will redraw itself as it moves. This way theres less coding to do in the main program, especially if you decide to use the same object for another game! If you have any questions about this code, post a question on the board so everyone can benefit!

3 Responses to Các ví dụ về games với C++ – Example Games

  1. I simply want to mention I am beginner to weblog and absolutely liked this blog. Probably I’m likely to bookmark your blog . You absolutely have terrific articles and reviews. Regards for revealing your blog site.

  2. I simply want to mention I am newbie to blogs and honestly liked you’re blog. Likely I’m going to bookmark your website . You absolutely have great stories. Appreciate it for revealing your blog.

  3. Sam Foo nói:

    I simply want to say I’m all new to blogging and site-building and honestly enjoyed you’re blog site. Likely I’m going to bookmark your blog . You surely have fantastic well written articles. Many thanks for sharing your website.

Gửi phản hồi

Please log in using one of these methods to post your comment:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s

%d bloggers like this: