C ++ game programs-Tortoise and rabbit racing IV

Source: Internet
Author: User

WindowsBecause I do not know how to do the window program, I wrote it on the Character interface. In fact, the interface is not the most important, but the logical part of the program.

The first time I wrote a C ++ program, I was not very familiar with C ++ classes ......

Tortoise and hare race IV, Athlete information:
Bytes -------------------------------------------------------------------------------------------

Athlete's sport type, time, proportional speed

TortoiseFast crawling 50% 3 m/s
Slow crawling: 30% m/s
Slide to 20%-6 m/s

RabbitSleeping 20% 0 m/s
Stride 20% 9 m/s
Small Step Jump 30% 1 m/s
Stride to 10%-12 m/s
Small Step slide to 20%-2 m/s
Bytes -------------------------------------------------------------------------------------------

Venue: Console character Interface
Runway Length: limited by the screen width, about 60 characters

A program is required to simulate a race between two athletes. (In fact, it is not a game program >_< |)

Finally, the interface looks like this. The following yellow mark is drawn by fireworks:

After the program is run, the two athletes will rush forward, slow and sometimes fall and fall back.

On the left side of the runway, you can view the athlete's current sports status at any time. rabbits are sleeping, turtles are crawling fast ......

The first time I wrote the C ++ program, the first time I inherited the C program, and the first time I used the C ++ stored virtual function ......

1. Design concept.

Because the two athletes only have different sports modes and other attributes can be shared, an athlete abstract class is written as the public base class. In the athlete class, the run () function is defined as a saved virtual function. The two athlete classes implement the specific details.

2. How to Make animations on the Character interface.

This animation is very simple, that is, it constantly clears the screen content and prints new images. The clear screen is implemented using the system ("CLS") function and must contain the stdlib. h file.

3. How to control the animation speed.

In this animation loop, if you do not add any delayed functions, the program will disappear in a flash. Therefore, you need to pause for a certain period of time after each screen is displayed. To implement this function, you can use the clock () function, which requires the header file time. h. Customize a delay () function:
Void delay (INT dly ){
Int ST = clock ();
While (clock () <st + dly );
}

========================================================== ================================

Paste the code, which is not very beautiful ...... :

/*
Tortoise and hare race
*/

# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <time. h>
# Define L 50

// Class athlete
// Super class of tortoise and rabbit
Class athlete {
PRIVATE:
Char name [l];
Char State [l];
Char logo;
Int speed;
Int position;
Int ending;
Public:
Athlete (){
Name [0] = '/0 ';
Setstate ("waitting ");
Speed = position = 0;
Ending = 56;
}
// Set
Void setname (char * s) {strcpy (name, S );}
Void setstate (char * s) {strcpy (State, S );}
Void setlogo (char c) {logo = C ;}
Void setspeed (int x) {speed = x ;}
Void setposition (int x) {position = x ;}
Void incposition (int I ){
Position + = I;
If (position <0) Position = 0;
If (position> ending) Position = ending;
}
Void setinfo (char * s, int v ){
Setstate (s );
Setspeed (v );
}
// Get
Char * getname () {return name ;}
Char * getstate () {return state ;}
Char getlogo () {return logo ;}
Int getspeed () {return speed ;}
Int getending () {return ending ;}
Int getposition () {return position ;}
// Print state
Void printstate (){
Int v = getspeed ();
Char op = V <0? '-': '+ ';
V = V> 0? V:-V;
Printf ("%-8 s: % 9 S % C %-2D", getname (), getstate (), op, V );
}
// Print track
Void printtrack (){
Char S = 16, t = 17, P = getlogo ();
Int n = getposition (), M = getending ()-n-1;
Int I;

If (position! = 0) {printf ("% C", S); n --;}
For (I = 0; I <n; I ++) printf ("");
Printf ("% C", P );
For (I = 0; I <m; I ++) printf ("");
If (position! = Getending () printf ("% C", t );
}

// Virtual run
Virtual void run () = 0;
};

Class Rabbit: Public athlete {
Public:
Rabbit (){
Setname ("rabbit ");
Setlogo (1 );
}
// Virtual run ()-Rabbit
Virtual void run (){
Int e = rand () %100;
If (E <20 ){
Setinfo ("Sleep", 0); // sleeping 0% 20
}
Else if (E <40 ){
Setinfo ("bigjump", 9); // bigjump + 9% 20
}
Else if (E <50 ){
Setinfo ("bigslip",-7); // bigslip-7% 10
}
Else if (E <80 ){
SetInfo ("Jump", 5); // Jump + 5% 30
}
Else {
SetInfo ("Slip",-3); // Slip-5% 20
}
IncPosition (getSpeed ());
}
};

Class Tortoise: public Athlete {
Public:
Tortoise (){
SetName ("Tortoise ");
SetLogo (2 );
}
// Virtual run ()-Tortoise
Virtual void run (){
Int e = rand () %100;
If (e <20 ){
SetInfo ("Slip",-3); // Slip-3% 20
}
Else if (e <50 ){
SetInfo ("Crawl", 2); // Crawl + 30, 2%
}
Else {
SetInfo ("FastCrawl", 4); // FastCrawl + 4% 50
}
IncPosition (getSpeed ());
}
};

Void delay (int dly ){
Int st = clock ();
While (clock () <st + dly );
}

Bool win (Athlete * p ){
If (p-> getPosition () = p-> getEnding () return true;
Else return false;
}

Void printa (Athlete * p ){
Printf ("*** % s Reach the Ending ***/n", p-> getName ());
}

Void printRound (int r ){
Printf ("++ round % d ++/N", R );
}

Int main ()
{
Int I, K, M, N;
Integer delaytime = 500;
Int round = 0;
Bool finish;
Char MSG;
Athlete * P, * q, * W;
Srand (Time (null ));

While (true ){
P = new rabbit ();
Q = new tortoise ();
Round ++;

System ("CLS ");
Printround (round );
P-> printstate (); P-> printtrack (); puts ("");
Q-> printstate (); q-> printtrack (); puts ("");
Printf ("%-15 s === press enter to start. 'q' to quit ===/N ","");

MSG = getchar ();
If (MSG = 'q' | MSG = 'q') break;

// Play the match
Finish = false;
While (! Finish ){

System ("CLS ");
P-> Run ();
Q-> Run ();

Printround (round );
P-> printstate (); P-> printtrack (); puts ("");
Q-> printstate (); q-> printtrack (); puts ("");

Delay (delaytime );

If (WIN (p) {printa (p); finish = true ;}
If (WIN (q) {printa (Q); finish = true ;}
If (finish ){
Printf ("% 20 s ==== press enter to continue ==== ","");
Getchar ();
}
}
Delete P;
Delete Q;
}

Return 0;
}

 

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.