C語言代碼實現(xiàn)飛機大戰(zhàn)
更新時間:2020年12月02日 10:25:29 作者:Cielfire
這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡單飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)簡單飛機大戰(zhàn)的具體代碼,供大家參考,具體內容如下
這個游戲的功能很單一,也就是“飛機大戰(zhàn)”,哈哈哈哈??偣仓挥?00多行代碼左右,你也可以想想它會有多簡陋,把它復制下來編譯一下可以直接執(zhí)行,需要的同學可以自取~
PS:我運行的環(huán)境是 dev c++,前提你要在C99的環(huán)境中執(zhí)行
以下是源代碼
#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; //獲得分數(shù) int flag; //飛機狀態(tài) void gotoxy(int x,int y); //光標移動到(x,y)位置 void welcometogame(); //初始化界面 int color(int c); //更改文字顏色 void explation(); //游戲右側顯示 void scoreandtips(); //顯示游戲提示 void show(); //顯示游戲界面 void endgame(); //游戲結束 /** * 文字顏色函數(shù) */ 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("飛 機 大 戰(zhàn)"); 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(); //游戲說明函數(shù) break; case 3: exit(0); //退出游戲 break; default: color(12); gotoxy(40,28); printf("請輸入1-3之間的數(shù)!"); _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. 按空格發(fā)射子彈,打中敵機即可得到一分"); 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("按下空格鍵即為發(fā)射炮彈"); gotoxy(50,14); printf("@ 的樣子就是敵人的飛機"); } void HideCursor() // 用于隱藏光標 { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二個值為0表示隱藏光標 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() //數(shù)據(jù)初始化 { 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("分數(shù) %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(); // 數(shù)據(jù)初始化 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(); // 數(shù)據(jù)初始化 //explation(); while(1) // 游戲循環(huán)執(zhí)行 { gotoxy(0,0); show(); // 顯示畫面 scoreandtips(); if(flag == 1) { endgame(); } withoutInpute(); // 與用戶輸入無關的更新 withInpute(); // 與用戶輸入有關的更新 } return 0; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。