C語言代碼實(shí)現(xiàn)飛機(jī)大戰(zhàn)
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
這個(gè)游戲的功能很單一,也就是“飛機(jī)大戰(zhàn)”,哈哈哈哈。總共只有300多行代碼左右,你也可以想想它會(huì)有多簡陋,把它復(fù)制下來編譯一下可以直接執(zhí)行,需要的同學(xué)可以自取~
PS:我運(yùn)行的環(huán)境是 dev c++,前提你要在C99的環(huán)境中執(zhí)行
以下是源代碼
#include<stdio.h> #include<stdio.h> #include<windows.h> //將用戶從鍵盤獲得的輸入進(jìn)行輸出 #include<conio.h> //獲得用戶鍵盤的輸入 //定義全局變量 int high,width; //定義邊界 int position_x,position_y; //飛機(jī)位置 int bullet_x,bullet_y; //子彈位置 int enemy_x,enemy_y; //敵軍飛機(jī) int score; //獲得分?jǐn)?shù) int flag; //飛機(jī)狀態(tài) void gotoxy(int x,int y); //光標(biāo)移動(dòng)到(x,y)位置 void welcometogame(); //初始化界面 int color(int c); //更改文字顏色 void explation(); //游戲右側(cè)顯示 void scoreandtips(); //顯示游戲提示 void show(); //顯示游戲界面 void endgame(); //游戲結(jié)束 /** * 文字顏色函數(shù) */ int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字顏色 return 0; } /** * 設(shè)置光標(biāo)位置 */ 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("飛 機(jī) 大 戰(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("請(qǐng)選擇 1 2 3:"); color(14); scanf("%d", &n); //輸入選項(xiàng) 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("請(qǐng)輸入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 分別控制飛機(jī)的上下左右移動(dòng)"); color(10); gotoxy(20,8); printf("2. 按空格發(fā)射子彈,打中敵機(jī)即可得到一分"); color(14); gotoxy(20,11); printf("3.碰到敵機(jī)子彈死亡"); 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()//游戲側(cè)邊提示 { gotoxy(50,8); color(14); printf("游戲得分:%d ",score); gotoxy(50,10); printf("用W A S D 分別控制飛機(jī)的移動(dòng)"); gotoxy(50,12); printf("按下空格鍵即為發(fā)射炮彈"); gotoxy(50,14); printf("@ 的樣子就是敵人的飛機(jī)"); } void HideCursor() // 用于隱藏光標(biāo) { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二個(gè)值為0表示隱藏光標(biāo) SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() //數(shù)據(jù)初始化 { high=20; //定義游戲界面的高度 width=40; //游戲界面的寬度 position_x=high-3; //定義飛機(jī)的初始位置 position_y=width/2; bullet_x=0; bullet_y=position_y; enemy_x=0; enemy_y=position_y; score=0; flag=0; //飛機(jī)完好 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)) //飛機(jī)坐標(biāo) { printf("^"); } else if((i==enemy_x)&&(j==enemy_y)) //敵機(jī)坐標(biāo) printf("@"); else if((i==bullet_x)&&(j==bullet_y)) //子彈坐標(biāo) 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; //飛機(jī)撞毀 游戲結(jié)束 system("cls"); printf("游戲結(jié)束!!!\n"); } else { printf("分?jǐn)?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("輸入錯(cuò)誤,輸入任意鍵回到主菜單"); _getch(); //輸入任意鍵 welcometogame(); startup(); system("cls"); //清屏 } } void withoutInpute() //與用戶輸入無關(guān) { if(bullet_x>0) //子彈上升效果 bullet_x--; if((bullet_x==enemy_x)&&(bullet_y==enemy_y)) //子彈命中敵機(jī) { score++; bullet_x=-1; enemy_x=1; enemy_y=2+rand()%width-2; } static int speed; if(speed<30) //減慢敵機(jī)速度,不影響飛機(jī)和子彈速度 speed++; if(speed==30) { if(enemy_x<high) enemy_x++; else { enemy_x=0; enemy_y=2+rand()%width-2; } speed=0; } } void withInpute() //與用戶輸入有關(guān) { char input; if(kbhit()) //控制飛機(jī)方向 { 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"); //設(shè)置控制臺(tái)的寬高 welcometogame(); startup(); // 數(shù)據(jù)初始化 //explation(); while(1) // 游戲循環(huán)執(zhí)行 { gotoxy(0,0); show(); // 顯示畫面 scoreandtips(); if(flag == 1) { endgame(); } withoutInpute(); // 與用戶輸入無關(guān)的更新 withInpute(); // 與用戶輸入有關(guān)的更新 } return 0; }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn)
- C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn)程序設(shè)計(jì)
- C語言用封裝方法實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲
- C語言實(shí)現(xiàn)簡單的飛機(jī)大戰(zhàn)游戲
- C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲完整代碼
- C語言控制臺(tái)實(shí)現(xiàn)字符飛機(jī)大戰(zhàn)
- C語言版飛機(jī)大戰(zhàn)游戲
- C語言之飛機(jī)大戰(zhàn)游戲
- C語言實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)
- C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
相關(guān)文章
C++各種數(shù)據(jù)類型所占內(nèi)存大小詳解
這篇文章主要介紹了C++各種數(shù)據(jù)類型所占內(nèi)存大小,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08C語言實(shí)現(xiàn)經(jīng)典小游戲井字棋的示例代碼
這個(gè)三子棋游戲是在學(xué)習(xí)C語言的過程中自己編寫的一個(gè)小游戲,現(xiàn)在將自己的思路(主要以流程圖形式和代碼中的注釋表達(dá))和具體代碼以及運(yùn)行結(jié)果分享出來以供大家學(xué)習(xí)參考,希望對(duì)大家有所幫助2022-11-11C語言實(shí)現(xiàn)簡單的三子棋項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單的三子棋項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08C++使用一棵紅黑樹同時(shí)封裝出map和set實(shí)例代碼
紅黑樹(Red?Black?Tre)是一種自平衡二叉查找樹,是在計(jì)算機(jī)科學(xué)中用到的一種數(shù)據(jù)結(jié)構(gòu),典型的用途是實(shí)現(xiàn)關(guān)聯(lián)數(shù)組,下面這篇文章主要給大家介紹了關(guān)于C++使用一棵紅黑樹同時(shí)封裝出map和set的相關(guān)資料,需要的朋友可以參考下2023-04-04C語言實(shí)現(xiàn)最簡單的剪刀石頭布小游戲示例
這篇文章主要介紹了C語言實(shí)現(xiàn)最簡單的剪刀石頭布小游戲,涉及C語言數(shù)組、隨機(jī)數(shù)與數(shù)值運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09