C language to achieve a simple aircraft war

Time:2021-10-27

This example shares the specific code of aircraft war in C language for your reference. The specific contents are as follows

Define four functions to realize aircraft war

#include<stdio.h>
#include<windows.h>
#include<conio.h>
//Define global variables 
int high,width; // Define boundary 
int position_ x,position_ y; // Aircraft location 
int bullet_ x,bullet_ y; // Bullet position 
int enemy_x,enemy_y;
int score;
int flag; // Aircraft status 
Void gotoxy (int x, int y) // move the cursor to the (x, y) position
{
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(handle,pos);
}
Void hidecursor() // used to hide the cursor
{
 CONSOLE_ CURSOR_ INFO cursor_ info = {1, 0}; //  A second value of 0 hides the cursor
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
 
Void startup() // data initialization 
{
 high=18;
 width=26;
 
 position_x=high-3; 
 position_y=width/2;
 
 bullet_x=0;
 bullet_y=position_y; 
 
 enemy_x=0;
 enemy_y=position_y;
 
 score=0;
 
 flag=0; // The plane is intact 
 
 HideCursor();
}
Void show() // the interface is displayed 
{
 int i,j;
 for(i=0;i<high;i++)
 {
 for(j=0;j<width;j++)
 {
 if(flag)
 break;
 
 Else if ((I = = position_x) & & (J = = position_y)) // aircraft coordinates 
 printf("*");
 Else if ((I = = enemy_x) & & (J = = enemy_y)) // enemy aircraft coordinates 
 printf("*");
 Else if ((I = = bullet_x) & & (J = = bullet_y)) // bullet coordinates 
 printf("|");
 Else if ((J = = width-1) | (I = = high-1) | (J = = 0) | (I = = 0)) // print boundary 
 printf("#");
 else
 printf(" ");
 }
 printf("\n"); 
 }
 printf("\n");
 if((position_x==enemy_x)&&(position_y==enemy_y))
 {
 flag=1; // Plane crash game over 
 Printf ("score:% d \ n", score);
 Printf ("game over");
 }
 else
 Printf ("score:% d \ n", score);
}
Void withoutinput() // it has nothing to do with user input
{
 If (bullet_x > 0) // bullet rising effect 
 bullet_x--;
 If ((bullet_x = = enemy_x) & & (bullet_y = = enemy_y)) // the bullet hit the enemy plane 
 {
 score++;
 bullet_x=-1; 
 enemy_x=1;
 enemy_y=2+rand()%width-2;
 } 
 
 static int speed;
 If (speed < 30) // slow down the enemy aircraft without affecting the speed of the aircraft and bullets 
 speed++;
 if(speed==30)
 {
 if(enemy_x<high)
 enemy_x++;
 else 
 {
 enemy_x=0;
 enemy_y=2+rand()%width-2;
 }
 speed=0;
 } 
 
}
Void withinput() // related to user input 
{
 char input;
 If (kbhit()) // control the aircraft direction 
 {
 input=getch();
 if((input=='w')&&position_x>1)
 position_x--; 
 if((input=='s')&&position_x<high-2)
 position_x++; 
 if((input=='a')&&position_y>1)
 position_y--; 
 if((input=='d')&&position_y<width-2)
 position_y++;
 if(input==' ')
 {
 bullet_x=position_x-1;
 bullet_y=position_y;
 }
 }
}
int main()
{
 system("color 2f");
 startup(); //  Data initialization
 While (1) // game loop execution
 {
 gotoxy(0,0);
 show(); //  display frame
 withoutInpute(); //  Updates unrelated to user input
 withInpute(); //  Updates related to user input
 }
 }

Another piece of code from the author:C language to achieve air combat games, also great, share with you:

#include<stdio.h>
#include<windows.h>
#include<conio.h>
#Define high 27 // define boundary
#define Width 45
#Define enemynum 5 // number of enemy aircraft 
//Define global variables
int canvas[High][Width]={0}; // Definition elements: 0 is a space, 1 is an aircraft, 2 is a bullet, 3 is an enemy aircraft, and 4 is the lower right boundary 
int position_ x,position_ y; // Aircraft coordinates
int enemy_ x[EnemyNum],enemy_ y[EnemyNum]; // Enemy aircraft coordinates
int score; // score 
int Speed; // Enemy aircraft speed
int bulletwidth; // Bullet width 
Void hidecursor() // hide cursor
{
 CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
Void gotoxy (int x, int y) // move the cursor to the (x, y) position
{
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(handle,pos);
}
Void startup() // data initialization
{
 position_ x=High-2; // Initialize aircraft position
 position_y=Width/2;
 canvas[position_x][position_y]=1;
 
 bulletwidth=0; // Initialize bullet width 
 Speed=25; // Initial minimum speed of enemy aircraft 
 int k;
 for(k=0;k<EnemyNum;k++)
 {
 enemy_ x[k]=rand()%2; // Initialize enemy position
 enemy_y[k]=rand()%Width;
 canvas[enemy_x[k]][enemy_y[k]]=3;
 } 
 score=0; // Score initialization 
 
 HideCursor();
}
Void show() // the interface is displayed
{
 int i,j;
 gotoxy(0,0);
 for(i=0;i<=High;i++)
 {
 for(j=0;j<=Width;j++)
 {
 if(canvas[i][j] == 1)
 printf("*"); // Output aircraft
 else if(canvas[i][j]==2)
 printf("|"); // Output bullet
 else if(canvas[i][j]==3)
 printf("@"); // Export enemy aircraft
 else if(canvas[i][j]==4)
 printf("#"); // Output boundary# 
 else
 printf(" "); // Output space
 }
 printf("\n");
 }
 Printf ("score:% d \ n", score); 
}
Void updatewithoutinput() // update without user input
{
 int i,j,k;
 for(i=0;i<High;i++)
 {
 for(j=0;j<Width;j++)
 {
 if(canvas[i][j]==2) 
 {
 for(k=0;k<EnemyNum;k++)
 {
 If (I = = enemy_x [k] & & J = = enemy_y [k]) // hit the enemy plane 
 {
 score++;
 If (score = = 5|score = = 10) // the bullet is widened when the score reaches the standard 
 bulletwidth++;
 canvas[enemy_x[k]][enemy_y[k]]=0; // Generate new enemy aircraft 
 enemy_x[k]=rand()%2;
 enemy_y[k]=rand()%Width;
 canvas[enemy_x[k]][enemy_y[k]]=3;
 } 
 }
 
 canvas[i][j]=0; // The bullet rises automatically
 if(i>0)
  canvas[i-1][j]=2;
 }
 }
 }
 for(k=0;k<EnemyNum;k++)
 {
 If (enemy_x [k] > high) // generate a new enemy aircraft 
 {
 canvas[enemy_x[k]][enemy_y[k]]=0;
 enemy_x[k]=rand()%2;
 enemy_y[k]=rand()%Width;
 canvas[enemy_x[k]][enemy_y[k]]=3;
 } 
 }
 static int speed=0; 
 If (speed < speed) // enemy aircraft speed 
 speed++;
 if(speed==Speed)
 {
 for(k=0;k<EnemyNum;k++)
 {
 
 canvas[enemy_x[k]][enemy_y[k]]=0; // The enemy plane dropped automatically 
 enemy_x[k]++;
 canvas[enemy_x[k]][enemy_y[k]]=3; 
 }
 speed=0;
 } 
 for(k=0;k<EnemyNum;k++)
 {
 If (enemy_x [k] = = position_x & & enemy_y [k] = = position_y) // aircraft crash 
 {
 Printf ("end of game \ n");
 exit(0);
 }
 } 
} 
Void updatewithinput() // updates that require user input
{
 char input;
 if(kbhit())
 {
 input=getch();
 If (input = ='w '& & position_x > 0) // control the aircraft direction
 {
 canvas[position_x][position_y]=0;
 position_x--;
 canvas[position_x][position_y]=1;
 }
 else if(input=='s' && position_x<High-1)
 {
 canvas[position_x][position_y]=0;
 position_x++;
 canvas[position_x][position_y]=1;
 }
 else if(input=='a' && position_y>0)
 {
 canvas[position_x][position_y]=0;
 position_y--;
 canvas[position_x][position_y]=1;
 }
 else if(input=='d' && position_y<Width-1)
 {
 canvas[position_x][position_y]=0;
 position_y++;
 canvas[position_x][position_y]=1;
 }
 Else if (input = '' ') // space fire bullets
 {
 int left,right;
 int x;
 left=position_y-bulletwidth;
 if(left<0)
 left=0;
 right=position_y+bulletwidth;
 if(right>Width-1)
 right=0;
 for(x=left;x<=right;x++)
 canvas[position_x-1][x]=2;
 }
 }
}
int main()
{
 startup();
 system("color 2f");
 while(1)
 {
 show(); // Display interface
 updateWithoutInput(); // Updates without user input
 updateWithInput(); // Updates requiring user input
 }
}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.