[Original] greedy Snake game with C ++ (tc3.0)-(3)

Source: Internet
Author: User
Tags gety

Class game {// The most critical game class
Public:
Void mainbody ();
Void gameinitialize ();
Void mapobstacle (obstacle ob [], int X );
Void graphicend ();
Void keywaiting ();
Bool continue ();
Void pressanykey ();
};
Void game: gameinitialize (){
Screenlayout P;
Supercol brush;
Int midx, midy, I;
P. graphicstart ();
P. errordetect ();
Midx = getmaxx ()/2; midy = getmaxy ()/2;
Brush. bar2 (midx-20 * fudu, midy-15 * fudu, midx + 20 * fudu, midy + 15 * fudu, lightblue );
Brush. bar2 (midx-19 * fudu, midy-14 * fudu, midx + 19 * fudu, midy + 14 * fudu, black );
Brush. printword (midx-16 * fudu, midy-19 * fudu + 5, "snake", 0, lightgreen );
Brush. printword (midx-2 * fudu, midy-17 * fudu + 10, "made by: emilmatthew 05/1/16", 1, brown );
}
Void game: mapobstacle (obstacle ob [], int X ){
Supercol brush;
Int I = 0;
Int midx = getmaxx ()/2, midy = getmaxy ()/2;
For (I = 0; I <X; I ++ ){
If (OB [I]. Flag ){
Brush. bar2 (midx-19 * fudu + OB [I]. x * fudu, midy-14 * fudu + OB [I]. y * fudu,
Midx-18 * fudu + OB [I]. x * fudu, midy-13 * fudu + OB [I]. y * fudu, lightblue );
}
}

}
Void game: keywaiting (){
Gotoxy (5, 10 );
Printf ("pause ");
While (bioskey (1) = 0 ){;}
Gotoxy (5, 10 );
Printf ("");
}
Void game: pressanykey (){
Gotoxy (5, 10 );
Printf ("press any key to continue.../N ");
}
Bool game: Continue (){
// Clrscr ();
Char TMP;
Gotoxy (5, 10 );
Printf ("");
Gotoxy (8, 11 );
Printf ("oh, you failure/N ");
Gotoxy (8, 12 );
Printf ("Try again? (Y/N)/n ");
Gotoxy (8, 13 );
Scanf ("% C", & TMP );
If (TMP = 'y') return true;
Else return false;
}
Void game: mainbody (){
// Declare object or variables
Screenlayout test;
Drawhead SDH;
Drawbody SDB;
Cleantail SCT;
Drawbean Dou;
Obstacle stock [5];
Bean Douzi;
Int key = 0, Index = 0;
Bool gotbean = false, check;
Bool keypressed = false; // If the button is too fast, it will not change
Direction tmdir = right;
// Set start data;
Snake W (1, 0, right, null );
Snake B2 (2, 0, right, & W );
Snake B1 (3, 0, right, & B2 );
Snake H (4,0, right, & B1 );
Snake tb1 [300]; // (, right, null );
Snake tb2 (0, 0, right, null );
Randomize ();
Stock [0]. Flag = true;
Stock [0]. x = random (22) + 5;
Stock [0]. Y = random (26) + 1;
Douzi. Flag = true;
Score = 0;

Mapobstacle (stock, 1 );

// Draw first snake.
SDH. Draw (H. getx (), H. Gety ());
SDB. Draw (b1.getx (), b1.gety ());
SDB. Draw (b2.getx (), b2.gety ());
// Generate bean.
H. makenewbean (stock, 5, Douzi); // This interface is not a good one, as I thought
Dou. Draw (Douzi. X, Douzi. y );
While (gamestatur = gaming ){
While (bioskey (1) = 0 & gamestatur = gaming ){
If (gotbean ){
// 1 insert
H. insert (& tb1 [Index]);

SDB. Draw (tb1 [Index]. getx (), tb1 [Index]. Gety ());

Index ++;
SDH. Draw (H. getx (), H. Gety ());
Gotbean = false;
// Not so perfect.
// Add random make bean Code etc.
// W. settail ();
H. makenewbean (stock, 1, Douzi );
Dou. Draw (Douzi. X, Douzi. y );
}
Else {
// 2 Bian Li
// At first, you use this: * TB = & H, of course it just stands for H,
// Can't work as your thought.
Tb2 = H; // if you use tb1 = H, there will be an error, because of the shallow copy
H. bianlidir ();
H. setdir (tmdir );
SDH. Draw (H. getx (), H. Gety ());
SDB. Draw (tb2.getx (), tb2.gety ());
SCT. Draw (W. getx (), W. Gety ());
}
Delay2 (0.15, secperframe );
// Collision check
// Eat self
If (H. eatself ())
{Cout <"eat self" <Endl;
Pressanykey ();
Gamestatur = failure;
Break;
}
// Collision with Wall
If (H. hitwall ())
{Cout <"hit Wall" <Endl;
Pressanykey ();
Gamestatur = failure;
Break; // attention here
}
// Collision with obstacle
If (H. hitbrack (stock, 1 ))
{Cout <"hitbrack" <Endl;
Pressanykey ();
Gamestatur = failure;
Break;
}
If (H. gotbean (Douzi )){

Gotbean = true;
}
Keypressed = false;
}
// Check key
Key = bioskey (0 );
If (! Keypressed)
{Switch (key ){
Case keyup: If (tmdir! = Down ){
Tmdir = up;
}
Break;
Case keydown: If (tmdir! = Up ){
Tmdir = down ;}
Break;
Case keyleft: If (tmdir! = Right ){
Tmdir = left ;}
Break;
Case keyright: If (tmdir! = Left ){
Tmdir = right ;}
Break;
Case keyenter:
// Small keywaiting
Keywaiting ();
Break;
Case keyesc:
Gamestatur = exit;
Break;
Default:
Break;
}
Keypressed = true;
}

}

If (gamestatur = failure)
{
Check = continue ();
If (check) gamestatur = gaming;
Else gamestatur = exit;
}
}

Void game: graphicend (){
Screenlayout P;
P. graphicend ();
}

Void main (void ){
// Screenlayout GG;
Game director;
While (gamestatur = gaming ){
Director. gameinitialize ();
If (gamestatur! = Exit) Director. mainbody ();
Director. graphicend ();
}
Getch ();
}

Void delay2 (double times, float secperframe ){
Bool running = true;
Double tmptime = (double) Clock ()/(double) clocks_per_sec;
Double caculate = 0, delaying = secperframe;
While (running & caculate <times ){
If (tmptime + delaying) <(double) Clock ()/(double) clocks_per_sec ))
{Tmptime = (double) Clock ()/(double) clocks_per_sec;
Caculate + = delaying;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.