C++ achieve color aircraft war
- 2020-11-20 06:12:02
- OfStack
Examples of this article for you to share C++ to achieve the specific code of color aircraft war, for your reference, the specific content is as follows
1. Basically realize the keyboard to control the aircraft, kill the enemy aircraft, and record the score (weakness: can't play again after death, need to restart the program,), the weakness will be solved in 2
/* Hide the cursor code
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);// Gets the console cursor information
CursorInfo.bVisible = false; // Hide console cursor
SetConsoleCursorInfo(handle, &CursorInfo);// Set the console cursor status
getchar();
}*/
/*
To understand two facts,
Enemy planes have the same horizontal and vertical coordinates as their own planes Said death
The enemy plane collided with its own bullet The coordinates of the bullet are the same as the coordinates of the bullet, the plane is dead and bonus points,
*/
#include "stdio.h"
#include <windows.h>
#include <conio.h>
#include <time.h>
#define Esc 27 // exit
#define Up 72 // Up, down, left, right
#define Down 80
#define Left 75
#define Right 77
#define Kong 32 // bullets
int x = 10; // The plane coordinates
int y = 18;
int d2 = 10;// The enemy plane coordinates
int d1 = 10;
int d = 10;//d and r Used for collision detection
int r = 1;
int r1 = 1;
int r2 = 1;
int t = 1; // Game over
int f = 0; // Meter scores
int m = 5; // Number of enemy planes
int j = 0; // kills
char p; // Accept button
void kongzhi(int bx, int by);// Declare functions
void huatu();
void gotoxy(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);
}
void gotoxy_red(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);
}
void gotoxy_blue(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1);
}
void gotoxy_green(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2);
}
void hidden()// Hide the cursor and don't let the cursor show
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0;// fu 1 For display, fu 0 To hide
SetConsoleCursorInfo(hOut, &cci);
}
//**************************************************************************************
// instructions
void shuoming()
{
printf("\t\t\t\n\n\n\n");
gotoxy_blue(0, 0);
printf("\t\t\t\t\t\t\tPlane Control\n\n"
"\t\t\t\t\t\t\t\tUP\n\n"
"\t\t\t\t\t\t\tDown\n\n"
"\t\t\t\t\t\t\tLeft \n\n"
"\t\t\t\t\t\t\tRight \n\n"
"\t\t\t\t\t\t\t bullet space\n\n\n"
"\t\t\t\t\t\t\tQuit Esc\n");
gotoxy_red(0, 0);
}
//****************************************************************************************
// Judge me if I'm dead / Game over
void byebye()
{
if (((x == d && y == r) || (x == d1 && y == r1) || (x == d2 && y == r2))||( (d>=19||r>=19)||(d1>=19||r1>=19)||(d1>=19||r1>=19) ))
{
gotoxy(1, 3);
printf(" !!! Game over !!!\n"
"*******************\n"
" Your total score : %d\n\n"
" Number of enemy planes : %d\n"
" kills : %d\n"
" shooting : %.0f %%\n"
"*******************\n", f, m, j, ((float)j / (float)m) * 100);
while (!_kbhit())
{
Sleep(500);
gotoxy(1, 12);
printf(" Press any key to continue ...\n\n\n");
Sleep(900);
gotoxy(1, 12);
printf(" ");
}
gotoxy_red(0, 0);
huatu();
f = 0; m = 0; j = 0;
if (x >= 18) x--;
else x++;
gotoxy(x, y);
printf("A");
}
}
// The scoring / Update the enemy planes
void jifan()
{ //x,y It's the coordinates of the bullet
if (x == d && y == r) // d=10, r=1 . d,d1,d2 Is the enemy x shaft , for 10 . r Is the ordinate of enemy aircraft
{
gotoxy(d, r); printf("3");
Sleep(200);
gotoxy(d, r); printf(" "); f += 2; r = 0; j++;// let r=0, To make the enemy's planes disappear
}
if (x == d1 && y == r1)
{
gotoxy(d1, r1); printf("1");
Sleep(200);
gotoxy(d1, r1); printf(" "); f += 3; r1 = 0; j++;
}
if (x == d2 && y == r2)
{
gotoxy(d2, r2); printf("0");
Sleep(200);
gotoxy(d2, r2); printf(" "); f += 1; r2 = 0; j++;
}
gotoxy(57, 2);
printf("%d\n", f);
}
// drawing
void huatu()
{
int i, n;
for (i = 0; i <= 20; i++)
{
for (n = 0; n <= 20; n++)
{
printf("*");
}
printf("\n");
}
for (i = 1; i <= 19; i++)
{
for (n = 1; n <= 19; n++)
{
gotoxy_red(i, n);
printf(" ");
}
}
}
// Random enemy aircraft are generated
void dfeiji()
{
while (t)
{
if (!r) { d = rand() % 17 + 1; m++; } //r,r1,r2 All of the initial values are zero 1 When a 0 "Starts generating random Numbers
if (!r1) { d1 = rand() % 17 + 1; m++; }
if (!r2) { d2 = rand() % 17 + 1; m++; }
while (t)
{
r=r+2; r1=r1+2; r2=r2+2;
gotoxy(d, r); printf("b");//d . d1, d2 Is the position generated by enemy aircraft, both are 10
gotoxy(d1, r1); printf("c");
gotoxy(d2, r2); printf("d");
Sleep(900);
gotoxy(d, r); printf(" ");
gotoxy(d1, r1); printf(" ");
gotoxy(d2, r2); printf(" ");
kongzhi(0, 0);// Once you take control of the plane, make your judgment immediately
byebye();// Determine if the plane is dead or not
if (r == 19) r = 0;
if (r1 == 19) r1 = 0;
if (r2 == 19) r2 = 0;
if (r == 0 || r1 == 0 || r2 == 0) break;
}
}
}
// Manipulation of the aircraft
void kongzhi(int bx, int by)// I passed it in when I called it 0 . 0
{
int a;
while (_kbhit())
{
if ((p = _getch()) == -32) p = _getch();
a = p;
gotoxy(22, 5);
switch (a)
{// Control the direction
case Up:if (y != 1)
{
gotoxy(x, y); printf(" ");
y--;
gotoxy(x, y); printf("A");
}break;
case Down:if (y != 18)
{
gotoxy(x, y); printf(" ");
y++;
gotoxy(x, y); printf("A");
}break;
case Left:if (x != 1)
{
gotoxy(x, y); printf(" ");
x--;
gotoxy(x, y); printf("A");
}break;
case Right:if (x != 18)
{
gotoxy(x, y); printf(" ");
x++;
gotoxy(x, y); printf("A");
}break;
case Kong: { bx = y;// The first y I'm going to save, I'm going to save bx
for (by = y; by > 1;) // bullets , y Axis coordinates 1 Straight down, print |
{
by--;//y The coordinates of the
gotoxy(x, by); printf("|");
Sleep(10);
gotoxy(x, by); printf(" ");
y = by;// Keep track of where the bullets hit so you can do a collision detection
jifan();// Meter scores
if (r == 0 || r1 == 0 || r2 == 0) break;
}
y = bx;// restore y The value of the
}break;
case Esc:t = 0; break; // exit
default:break;
}
}
}
int main()
{
srand(time(NULL));
shuoming();// Print game description , And then let the cursor go in 0,0
hidden();// Hide the cursor and don't let the cursor show
huatu();// Draw the walls
gotoxy(x, y);//x=10,y=8 . x and y It's the coordinates of your plane, it's the global variable
printf("A");
gotoxy(50, 2);
printf("Score:");
while (t) //t is 1 Global variables The initial value for the 1
{
kongzhi(0, 0);// Call the control aircraft function, ( After operating the aircraft, the score is recorded )
if (t) // If the game is not over, then Produce enemy planes
dfeiji();// Produce enemy planes , And determine if the plane is dead or not
}
}
2. (encapsulates 1 function) After finishing the game, it can restart for the next round
/* Hide the cursor code
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);// Gets the console cursor information
CursorInfo.bVisible = false; // Hide console cursor
SetConsoleCursorInfo(handle, &CursorInfo);// Set the console cursor status
getchar();
}*/
/*
To understand two facts,
Enemy planes have the same horizontal and vertical coordinates as their own planes Said death
The enemy plane collided with its own bullet The coordinates of the bullet are the same as the coordinates of the bullet, the plane is dead and bonus points,
*/
#include "stdio.h"
#include <windows.h>
#include <conio.h>
#include <time.h>
#define Esc 27 // exit
#define Up 72 // Up, down, left, right
#define Down 80
#define Left 75
#define Right 77
#define Kong 32 // bullets
int x = 10; // The plane coordinates
int y = 18;
int d2 = 10;// The enemy plane coordinates
int d1 = 10;
int d = 10;//d and r Used for collision detection
int r = 1;
int r1 = 1;
int r2 = 1;
int t = 1; // Game over
int f = 0; // Meter scores
int m = 5; // Number of enemy planes
int j = 0; // kills
char p; // Accept button
void kongzhi(int bx, int by);// Declare functions
void huatu();
void gotoxy(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);
}
void gotoxy_red(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);
}
void gotoxy_blue(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1);
}
void gotoxy_green(int x, int y) // Moving coordinates
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2);
}
void hidden()// Hide the cursor and don't let the cursor show
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0;// fu 1 For display, fu 0 To hide
SetConsoleCursorInfo(hOut, &cci);
}
//**************************************************************************************
// instructions
void shuoming()
{
printf("\t\t\t\n\n\n\n");
gotoxy_blue(0, 0);
printf("\t\t\t\t\t\t\tPlane Control\n\n"
"\t\t\t\t\t\t\t\tUP\n\n"
"\t\t\t\t\t\t\tDown\n\n"
"\t\t\t\t\t\t\tLeft \n\n"
"\t\t\t\t\t\t\tRight \n\n"
"\t\t\t\t\t\t\t bullet space\n\n\n"
"\t\t\t\t\t\t\tQuit Esc\n");
gotoxy_red(0, 0);
}
//****************************************************************************************
// Judge me if I'm dead / Game over
void byebye()
{
if (((x == d && y == r) || (x == d1 && y == r1) || (x == d2 && y == r2))||( (d>=19||r>=19)||(d1>=19||r1>=19)||(d1>=19||r1>=19) ))
{
gotoxy(1, 3);
printf(" !!! game over !!!\n"
"*******************\n"
" score: %d\n\n"
" di ji shu: %d\n"
" jian di shu: %d\n"
" ming zhong lv: %.0f %%\n"
"*******************\n", f, m, j, ((float)j / (float)m) * 100);
t=0;
}
}
// The scoring / Update the enemy planes
void jifan()
{ //x,y It's the coordinates of the bullet
if (x == d && y == r) // d=10, r=1 . d,d1,d2 Is the enemy x shaft , for 10 . r Is the ordinate of enemy aircraft
{
gotoxy(d, r); printf("3");
Sleep(200);
gotoxy(d, r); printf(" "); f += 2; r = 0; j++;// let r=0, To make the enemy's planes disappear
}
if (x == d1 && y == r1)
{
gotoxy(d1, r1); printf("1");
Sleep(200);
gotoxy(d1, r1); printf(" "); f += 3; r1 = 0; j++;
}
if (x == d2 && y == r2)
{
gotoxy(d2, r2); printf("0");
Sleep(200);
gotoxy(d2, r2); printf(" "); f += 1; r2 = 0; j++;
}
gotoxy(57, 2);
printf("%d\n", f);
}
// drawing
void huatu()
{
int i, n;
for (i = 0; i <= 20; i++)
{
for (n = 0; n <= 20; n++)
{
printf("*");
}
printf("\n");
}
for (i = 1; i <= 19; i++)
{
for (n = 1; n <= 19; n++)
{
gotoxy_red(i, n);
printf(" ");
}
}
}
// Random enemy aircraft are generated
void dfeiji()
{
while (t)
{
if (!r) { d = rand() % 17 + 1; m++; } //r,r1,r2 All of the initial values are zero 1 When a 0 "Starts generating random Numbers
if (!r1) { d1 = rand() % 17 + 1; m++; }
if (!r2) { d2 = rand() % 17 + 1; m++; }
while (t)
{
r=r+2; r1=r1+2; r2=r2+2;
gotoxy(d, r); printf("b");//d . d1, d2 Is the position generated by enemy aircraft, both are 10
gotoxy(d1, r1); printf("c");
gotoxy(d2, r2); printf("d");
Sleep(900);
gotoxy(d, r); printf(" ");
gotoxy(d1, r1); printf(" ");
gotoxy(d2, r2); printf(" ");
kongzhi(0, 0);// Once you take control of the plane, make your judgment immediately
byebye();// Determine if the plane is dead or not
if (r == 19) r = 0;
if (r1 == 19) r1 = 0;
if (r2 == 19) r2 = 0;
if (r == 0 || r1 == 0 || r2 == 0) break;
}
}
}
// Manipulation of the aircraft
void kongzhi(int bx, int by)// I passed it in when I called it 0 . 0
{
int a;
while (_kbhit())
{
if ((p = _getch()) == -32) p = _getch();
a = p;
gotoxy(22, 5);
switch (a)
{// Control the direction
case Up:if (y != 1)
{
gotoxy(x, y); printf(" ");
y--;
gotoxy(x, y); printf("A");
}break;
case Down:if (y != 18)
{
gotoxy(x, y); printf(" ");
y++;
gotoxy(x, y); printf("A");
}break;
case Left:if (x != 1)
{
gotoxy(x, y); printf(" ");
x--;
gotoxy(x, y); printf("A");
}break;
case Right:if (x != 18)
{
gotoxy(x, y); printf(" ");
x++;
gotoxy(x, y); printf("A");
}break;
case Kong: { bx = y;// The first y I'm going to save, I'm going to save bx
for (by = y; by > 1;) // bullets , y Axis coordinates 1 Straight down, print |
{
by--;//y The coordinates of the
gotoxy(x, by); printf("|");
Sleep(10);
gotoxy(x, by); printf(" ");
y = by;// Keep track of where the bullets hit so you can do a collision detection
jifan();// Meter scores
if (r == 0 || r1 == 0 || r2 == 0) break;
}
y = bx;// restore y The value of the
}break;
case Esc:t = 0; break; // exit
default:break;
}
}
}
void zuzhong(){
x = 10; // The plane coordinates
y = 18;
d2 = 10;// The enemy plane coordinates
d1 = 10;
d = 10;//d and r Used for collision detection
r = 1;
r1 = 1;
r2 = 1;
t = 1; // Game over
f = 0; // Meter scores
m = 5; // Number of enemy planes
j = 0; // kills
char p; // Accept button
srand(time(NULL));
shuoming();// Print game description , And then let the cursor go in 0,0
hidden();// Hide the cursor and don't let the cursor show
huatu();// Draw the walls
gotoxy(x, y);//x=10,y=8 . x and y It's the coordinates of your plane, it's the global variable
printf("A");
gotoxy(50, 2);
printf("Score:");
while (t) //t is 1 Global variables The initial value for the 1
{
kongzhi(0, 0);// Call the control aircraft function, ( After operating the aircraft, the score is recorded )
if (t) // If the game is not over, then Produce enemy planes
dfeiji();// Produce enemy planes , And determine if the plane is dead or not
}
}
int main()
{
while(1){
system("cls");
zuzhong();
printf("please enter Enter key contine");
getchar();
}
}