C language enables simple aircraft warfare

  • 2020-07-21 09:33:13
  • OfStack

This paper shares the specific code of C language for your reference. The specific content is as follows

Define 4 functions to implement aircraft war


#include<stdio.h>
#include<windows.h>
#include<conio.h>
// Define global variables  
int high,width; // Define the boundary  
int position_x,position_y; // The plane position  
int bullet_x,bullet_y; // The bullet position  
int enemy_x,enemy_y;
int score;
int flag; // The plane state  
void gotoxy(int x,int y) // Move the cursor to (x,y) location 
{
 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}; //  The first 2 A value of 0 Represents the hidden 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 intact  
 
 HideCursor();
}
void show() // display  
{
 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)) // The plane coordinates  
 printf("*");
 else if((i==enemy_x)&&(j==enemy_y)) // The enemy plane coordinates  
 printf("*");
 else if((i==bullet_x)&&(j==bullet_y)) // The bullet coordinate  
 printf("|");
 else if ((j==width-1)||(i==high-1)||(j==0)||(i==0)) // Print the border  
 printf("#");
 else
 printf(" ");
 }
 printf("\n"); 
 }
 printf("\n");
 if((position_x==enemy_x)&&(position_y==enemy_y))
 {
 flag=1; // The plane crashed   Game over  
 printf(" score : %d\n",score);
 printf(" Game over ");
 }
 else
 printf(" score : %d\n",score);
}
void withoutInpute() // It has nothing to do with user input 
{
 if(bullet_x>0) // Bullet 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 enemy planes without affecting the speed of planes 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 withInpute() // It has to do with user input  
{
 char input;
 if(kbhit()) // Steering the plane  
 {
 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(); //  According to the picture 
 withoutInpute(); //  Updates unrelated to user input 
 withInpute(); //  Updates related to user input 
 }
 } 

The author's other 1 piece of code: C language to achieve the air combat game, also very good, to share with you:


#include<stdio.h>
#include<windows.h>
#include<conio.h>
#define High 27 // Define the boundary 
#define Width 45
#define EnemyNum 5 // The enemy plane number  
// Define global variables 
int canvas[High][Width]={0}; // Define elements, 0 As the space, 1 As the plane, 2 As the bullet, 3 As the enemy, 4 Is the lower right boundary  
int position_x,position_y; // The plane coordinates 
int enemy_x[EnemyNum],enemy_y[EnemyNum]; // The enemy plane coordinates 
int score; // score  
int Speed; // Enemy aircraft speed 
int bulletwidth; // The bullet width  
void HideCursor()  // Hide the 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 (x,y) location 
{
 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; // Initializes the aircraft position 
 position_y=Width/2;
 canvas[position_x][position_y]=1;
 
 bulletwidth=0; // Initializes the 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() // display 
{
 int i,j;
 gotoxy(0,0);
 for(i=0;i<=High;i++)
 {
 for(j=0;j<=Width;j++)
 {
 if(canvas[i][j] == 1)
 printf("*"); // The output plane 
 else if(canvas[i][j]==2)
 printf("|"); // Output the bullet 
 else if(canvas[i][j]==3)
 printf("@"); // Output the enemy 
 else if(canvas[i][j]==4)
 printf("#"); // The output boundary # 
 else
 printf(" "); // The output space 
 }
 printf("\n");
 }
 printf(" Score: %d\n",score); 
}
void updateWithoutInput() // Updates 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  
 {
 score++;
 if(score==5||score==10) // Score the standard bullet broadening  
 bulletwidth++;
 canvas[enemy_x[k]][enemy_y[k]]=0; // Create new enemy planes  
 enemy_x[k]=rand()%2;
 enemy_y[k]=rand()%Width;
 canvas[enemy_x[k]][enemy_y[k]]=3;
 } 
 }
 
 canvas[i][j]=0; // Automatic ascent of bullet 
 if(i>0)
  canvas[i-1][j]=2;
 }
 }
 }
 for(k=0;k<EnemyNum;k++)
 {
 if(enemy_x[k]>High) // Create new enemy planes  
 {
 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; // Enemy planes fall by themselves  
 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) // The plane crashed  
 {
 printf(" Game over \n");
 exit(0);
 }
 } 
} 
void updateWithInput() // Updates requiring user input 
{
 char input;
 if(kbhit())
 {
 input=getch();
 if(input=='w' && position_x>0) // Steering the plane 
 {
 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 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 
 updateWithoutInput(); // Updates without user input 
 updateWithInput(); // Updates requiring user input 
 }
}

Related articles: