1. 程式人生 > 程式設計 >C語言程式碼實現飛機大戰

C語言程式碼實現飛機大戰

本文例項為大家分享了C語言實現簡單飛機大戰的具體程式碼,供大家參考,具體內容如下

這個遊戲的功能很單一,也就是“飛機大戰”,哈哈哈哈。總共只有300多行程式碼左右,你也可以想想它會有多簡陋,把它複製下來編譯一下可以直接執行,需要的同學可以自取~

PS:我執行的環境是 dev c++,前提你要在C99的環境中執行

以下是原始碼

#include<stdio.h>
#include<stdio.h>
#include<windows.h>   //將使用者從鍵盤獲得的輸入進行輸出 
#include<conio.h>   //獲得使用者鍵盤的輸入 
//定義全域性變數 
int high,width;   //定義邊界 
int position_x,position_y;  //飛機位置 
int bullet_x,bullet_y;  //子彈位置 
int enemy_x,enemy_y;  //敵軍飛機 
int score;    //獲得分數 
int flag;    //飛機狀態 
void gotoxy(int x,int y);   //游標移動到(x,y)位置
void welcometogame();     //初始化介面
int color(int c);       //更改文字顏色
void explation();   //遊戲右側顯示 
void scoreandtips();  //顯示遊戲提示 
void show();   //顯示遊戲介面 
void endgame();   //遊戲結束 
/**
 * 文字顏色函式   
 */
int color(int c)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);    //更改文字顏色
 return 0;
}
 
 /**
 * 設定游標位置
 */
void gotoxy(int x,int y)
{
  COORD c;
  c.X=x;
  c.Y=y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
 
 
 
void welcometogame() //開始介面
{
 int n;
 color(15);
 gotoxy(43,10);
 printf("飛 機 大 戰");
 color(11);
 gotoxy(25,22);
 printf("1.開始遊戲");
 gotoxy(45,22);
 printf("2.遊戲說明");
 gotoxy(65,22);
 printf("3.退出遊戲");
 gotoxy(40,27);
 color(3);
 printf("請選擇 1 2 3:");    
 color(14);
  scanf("%d",&n);   //輸入選項
  switch (n)
  {
   case 1:
   system("cls");
   show();
     break;
   case 2:
     explation();    //遊戲說明函式
     break;
   case 3:
     exit(0);    //退出遊戲
     break;
 default:  
  color(12);
  gotoxy(40,28);
  printf("請輸入1-3之間的數!");
  _getch();  //輸入任意鍵
  system("cls"); //清屏
  welcometogame();
  }
}
 
 
void explation() //遊戲提示 
{
 int i,j = 1;
  system("cls");
  color(10);
  gotoxy(44,1);
  printf("遊戲說明");
  color(2);
  for (i = 3; i <= 28; i++)  //輸出上下邊框===
 {
 for (j = 6; j <= 80; j++) //輸出左右邊框||
 {
  gotoxy(j,i);
  if (i == 3 || i == 28) printf("=");
  else if (j == 6 || j == 80) printf("||");
 }
 }
  color(3);
  gotoxy(20,5);
  printf("1. W,A,S,D 分別控制飛機的上下左右移動");
  color(10);
  gotoxy(20,8);
  printf("2. 按空格發射子彈,打中敵機即可得到一分");
  color(14);
  gotoxy(20,11);
  printf("3.碰到敵機子彈死亡");
  color(11);
  gotoxy(20,14);
  printf("4. ESC :退出遊戲");
  color(4);
  gotoxy(20,17);
  printf("5. 玩的愉快!!!");
  color(7);
 gotoxy(20,20);
 printf("/*****按任意鍵返回主頁面*****/");
  _getch();        //按任意鍵返回主介面
  system("cls");
  welcometogame();
}
 
void scoreandtips()//遊戲側邊提示 
{
 gotoxy(50,8);
 color(14);
 printf("遊戲得分:%d ",score);
 gotoxy(50,10);
 printf("用W A S D 分別控制飛機的移動");
 gotoxy(50,12);
 printf("按下空格鍵即為發射炮彈");
 gotoxy(50,14);
 printf("@ 的樣子就是敵人的飛機");
}
 
 
void HideCursor() // 用於隱藏游標
{
 CONSOLE_CURSOR_INFO cursor_info = {1,0}; // 第二個值為0表示隱藏游標
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
 
 
void startup()   //資料初始化 
{
 high=20;   //定義遊戲介面的高度 
 width=40;   //遊戲介面的寬度 
 
 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;    //飛機完好 
 
 HideCursor();
}
 
void show()    //顯示介面 
{
 
 int i,j,k;
 for(i=0;i<high;i++)
 {
 for(j=0;j<width;j++)
 {
  if(flag)
  break;
  else if((i==position_x)&&(j==position_y)) //飛機座標 
  {
  printf("^");
  }
  else if((i==enemy_x)&&(j==enemy_y))  //敵機座標 
  printf("@");
  else if((i==bullet_x)&&(j==bullet_y))  //子彈座標 
   printf("|");
  else if ((j==width-1)||(i==high-1)||(j==0)||(i==0))  //列印邊界 
  printf("-");
  else
  printf(" ");
 }
 printf("\n"); 
 }
 printf("\n");
 if((position_x==enemy_x)&&(position_y==enemy_y))
 {
 
 flag=1;   //飛機撞毀 遊戲結束 
 system("cls");
 printf("遊戲結束!!!\n");
 
 }
 else
 {
  printf("分數 %d",score);
  }
  /** _getch();        //按任意鍵返回主介面
    system("cls");
    welcometogame();
 */ 
 
 }
 
void endgame()
{
 int k,f;
 system("cls");
 printf("輸入1再玩一次,輸入2返回主選單,輸入3退出遊戲");
 scanf("%d",&k);
 system("cls");
 switch(k) 
 {
  case 1:
   printf("重新玩遊戲");
   system("cls");
   startup();   // 資料初始化
   show();
   break;
  case 2:
   printf("返回主選單");
   system("cls");
   welcometogame();
   startup();   
   break;
  case 3:printf("退出成功");
   exit(0);
   break;
  default:
   color(12);
   gotoxy(40,28);
   system("cls");
   printf("輸入錯誤,輸入任意鍵回到主選單");
   _getch();  //輸入任意鍵
   welcometogame(); 
   startup();   
   system("cls");  //清屏
 } 
}
 
 
void withoutInpute()   //與使用者輸入無關
{
 if(bullet_x>0)    //子彈上升效果 
 bullet_x--;
 if((bullet_x==enemy_x)&&(bullet_y==enemy_y)) //子彈命中敵機 
 {
 score++;
 bullet_x=-1; 
 enemy_x=1;
 enemy_y=2+rand()%width-2;
 } 
 
 
 static int speed;
 if(speed<30)   //減慢敵機速度,不影響飛機和子彈速度 
 speed++;
 if(speed==30)
 {
 if(enemy_x<high)
  enemy_x++;
 else 
 {
  enemy_x=0;
  enemy_y=2+rand()%width-2;
 }
 speed=0;
 }
 
 
 
 
}
void withInpute()   //與使用者輸入有關 
{
 char input;
 if(kbhit())     //控制飛機方向 
 {
 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("mode con cols=100 lines=30");  //設定控制檯的寬高
 welcometogame();
 startup();   // 資料初始化
 //explation();
 while(1)   // 遊戲迴圈執行
 {
 gotoxy(0,0);
 show();   // 顯示畫面
 scoreandtips(); 
 if(flag == 1)
 {
  endgame(); 
 }
 withoutInpute(); // 與使用者輸入無關的更新
 withInpute();  // 與使用者輸入有關的更新
 }
 return 0;
 } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。