C語言實現(xiàn)飛機大戰(zhàn)小游戲完整代碼
大一課設做的飛機大戰(zhàn),可以進行登入和注冊,這個是利用單鏈表做的,源代碼已經(jīng)給出,這個是最基本的飛機大戰(zhàn)模式,我設置了幾個功能,比如排行榜之類的。排行榜是用結(jié)構(gòu)體數(shù)組做的,已及冒泡排序,并且在文件里保存信息。比較簡單。
這個是注冊頁面規(guī)范:
這個是登入頁面:
游戲菜單:
飛機大戰(zhàn)頁面:
話不多說,直接上代碼
以下是源代碼
#include"stdio.h" #include"windows.h" //用于獲取窗口的句柄與屏幕控制 #include"conio.h" //用于獲取鍵盤輸入的內(nèi)容 #include"string.h" #include"stdlib.h" /*兩個結(jié)構(gòu)體*/ /*用于存用戶信息*/ typedef struct usepeople { char name[20];//用戶名 char mm[20];//密碼 usepeople *next; }U; /*輸出歷史記錄*/ struct history { char NAME[20]; int SCORE; }s[10]; //定義全局變量 int o; //保存排行榜分數(shù) char j[20]; //保存排行榜用戶名 int h,w; //定義畫面的高和寬 int p_x,p_y; // 定義我方飛機的位置 int e_x,e_y; // 定義敵方飛機的位置 int b_x,b_y; //定義子彈的位置 int score; //得分 int f; //定義飛機的狀態(tài) int num=1; //控制排名的數(shù)量 (我設置的是8個) void gotoxy(int x,int y); //光標移動到(x,y)位置 void menu1(U *h,int num); //菜單功能 int color(int c); //更改文字顏色 void explation(); //游戲右側(cè)顯示 void tip(); //游戲說明 void seescore(); //分數(shù)顯示 void startup(); //游戲初始化 void show(); //顯示游戲畫面 void Fly(); //定義函數(shù)來控制子彈和敵人的移動 void Planefly() ; //定義函數(shù)來控制飛機的移動和子彈的發(fā)射 void gameover(U *h); //設計游戲結(jié)束界面 U * createpeople(U *h); //用于注冊用戶的信息 void create(U *h); //建立第一個注冊信息 void History(struct history s[],int num); //用于遍歷輸出挑戰(zhàn)者信息 void insert(U *h); //在結(jié)尾加入注冊后的玩家賬號 U* search(U *h,char *name); //用于判斷用戶的用戶名是否正確 void deletenum(U *h,char name[]);//注銷用戶信息 void iregister(U *h); //注冊頁面 void dengru(U *h,int num); //登入頁面 void log_in(U *h); //保存用戶信息 void msort(history *a,int n); //記錄冒泡排序 int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字顏色 return 0; } //注銷用戶信息 void deletenum(U *h,char name[]) { U *p=h->next; while(p&&strcmp(p->name,name)!=0) { h=p; p=p->next; } if(p) { h->next=p->next; free(p); } } /*設置光標的位置*/ void gotoxy(int x,int y) { COORD c; c.X=x; c.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); } /*光標的隱藏*/ void HideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1,0};//第二個值為0表示隱藏光標 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } /*進行冒泡排序*/ void msort(history *a,int n) { for(int i=1;i<num;i++) { for(int f=num;f>i;f--) { if(s[f].SCORE>s[f-1].SCORE) { o=a[f].SCORE; a[f].SCORE=a[f-1].SCORE; a[f-1].SCORE=o; strcpy(j,a[f].NAME); strcpy(a[f].NAME,a[f-1].NAME); strcpy(a[f-1].NAME,j); } } } } //用于遍歷輸出挑戰(zhàn)者信息 void History(struct history s[],int num,U* h) { system("cls"); if(num==1) { gotoxy(1,1); printf("你還沒有記錄"); } for(int i=1;i<num;i++) //這里的for是用來逐一輸出每一個用戶的數(shù)據(jù) { gotoxy(15,3+2*i); printf("%d:用戶名:%s ",i,s[i].NAME); printf("得分:%d ",s[i].SCORE); } gotoxy(1,28); printf("請注意最多保存8次記錄"); getch(); system("cls"); menu1(h,num); //看完以后返回開始界面 } //建立第一個注冊信息 void create(U *h) { U *p,*q=h; char name[20]; printf("請輸入用戶名:\n"); gotoxy(39,12); scanf("%s",name); while(strcmp(name,"OVER")) { p=(U*)malloc(sizeof(U)); strcpy(p->name,name); char mm[20]; gotoxy(39,13); printf("請輸入密碼:(換行輸入OVER結(jié)束)\n"); gotoxy(39,14); scanf("%s",mm); strcpy(p->mm,mm); q->next=p; q=p; gotoxy(39,15); scanf("%s",name); } q->next=NULL; } U* search(U *h,char *name) { while(h=h->next,h) {if(!strcmp(h->name,name)) return h;} return NULL; } U * createpeople(U *h) { U *q; q=(U*)malloc(sizeof(U)); W: printf("輸入你要注冊的用戶名:\n"); scanf("%s",q->name); if(search(h,q->name)) {system("cls");printf("該用戶名已存在!?。n");_getch();system("cls");goto W;} printf("輸入你的密碼:\n"); scanf("%s",q->mm); q->next=NULL; return q; } //在結(jié)尾加入注冊后的玩家賬號 void insert(U *h) { U *q,*temp; q=createpeople(h); while(h=h->next,h){if(h->next==NULL)break;}temp=h; q->next=temp->next; temp->next=q; } /*游戲說明*/ void tip() { gotoxy(30,5);printf("w向上,s向下,a向左,d向右,按空格射擊"); gotoxy(30,10) ;printf("你擊落一架敵機可得1分"); gotoxy(30,15); printf("如果你被敵機撞上就會死亡"); } void menu1(U *h,int num) //菜單功能 { int n; gotoxy(37,10); printf("飛 機 大 戰(zhàn)"); gotoxy(15,20); printf("輸入數(shù)字1:開始游戲") ; gotoxy(55,20); printf("輸入數(shù)字2:查看規(guī)則"); gotoxy(15,22); printf("輸入數(shù)字3:查看挑戰(zhàn)榜"); gotoxy(55,22); printf("輸入數(shù)字4:退出游戲"); gotoxy(15,24); printf("輸入數(shù)字5:注銷賬號"); gotoxy(55,24); printf("輸入數(shù)字6:請你簽到"); gotoxy(37,25); printf("請輸入你的選擇:"); scanf("%d",&n); switch(n) { case 1: { system("cls"); }break; case 2: { system("cls"); tip(); printf("\n"); gotoxy(30,17); printf("輸入1 ,返回主頁"); int a=0; scanf("%d",&a); if(a==1) {system("cls");menu1(h,num);} }break; case 3: { History(s,num,h); }break; case 4: { system("cls"); printf("你確定要退出游戲嗎?\n"); printf("確定輸入1,不確定輸入2\n"); int a=0; scanf("%d",&a); if(a==1) exit(0); else {system("cls");menu1(h,num);} }break; case 5: { char name[20]; system("cls"); printf("輸入要刪除在帳號:"); scanf("%s",name); deletenum(h,name); printf("輸入1 ,返回主頁"); int a=0; scanf("%d",&a); if(a==1) { system("cls"); if(h->next==NULL) {iregister(h);dengru(h,num);} else dengru(h,num);menu1(h,num); } }break; case 6: { system("cls");log_in(h); gotoxy(42,12);printf("請輸入任意鍵繼續(xù)?。?!\n"); _getch(); system("cls"); menu1(h,num); }break; default: { gotoxy(37,25); printf("請看清楚條件\n"); _getch(); system("cls"); menu1(h,num); }break; } } void startup() //游戲初始化 { h=20; //高為20 w=50; //寬為50 score=0; //得分初始化 f=1; //飛機的狀態(tài) p_x=w/2; //飛機x的位置 p_y=h-4; //飛機y的位置 e_x=2+rand()%w-2; e_y=0; b_x=p_x; b_y=0; HideCursor(); //隱藏光標 } /*顯示游戲畫面*/ void show() { int i,j; for(i=0;i<h;i++) { for(j=0;j<w;j++) { if(f==0) break; else { if((i==0)||(j==0)||(i==h-1)||(j==w-1)) printf("+"); else if((i==p_y)&&(j==p_x)) printf("A"); else if((i==b_y)&&(j==b_x)) printf("!"); else if((i==e_y)&&(j==e_x)) printf("@"); else printf(" "); } } printf("\n"); } if((p_x==e_x)&&(p_y==e_y)) f--; } /*分數(shù)顯示 */ void seescore(int num) { gotoxy(56,5);printf("w向上,s向下,a向左,d向右,按空格射擊"); gotoxy(56,7);printf("A為您的戰(zhàn)機"); gotoxy(56,8);printf("@為敵機"); gotoxy(2,21);printf("你的得分為%d",score); s[num].SCORE=score; } /*用循環(huán)控制敵機的飛行速度*/ void Fly() { static int speed=0; if(speed<10) speed++; if(speed==10) { if(e_y<h) e_y++; else { e_y=0;e_x=2+rand()%w-2;} speed=0; } } void b_Fly() { if(b_y>0) b_y--;//控制子彈的飛行效果 if((b_y==e_y)&&(b_x==e_x)) {score++;e_y=0;e_x=2+rand()%w-2;b_y=0;} } /*控制飛機的移動*/ void Planefly() { char input; if(kbhit()) //kbhit函數(shù)是判斷是否有輸入 { input=getch(); //將輸入的值傳入input里面 if((input=='w')&&(p_y>1)) //如果按下wsad則相應移動飛機的位置 { p_y--; } if((input=='s')&&(p_y<h-2)) { p_y++; } if((input=='a')&&(p_x>1)) { p_x--; } if((input=='d')&&(p_x<w-2)) { p_x++; } if((input==' ')&&(b_y==0)) { b_x=p_x; b_y=p_y; } } } void gameover(U *h)//游戲結(jié)束提示 { system("cls");int a; gotoxy(39,14);printf("輸入1繼續(xù)打飛機,輸入2結(jié)束游戲"); scanf("%d",&a); switch(a) { case 1:{system("cls");}break; case 2:{system("cls");exit(0);}break; default:{system("cls");printf("請看清楚要求!");_getch();gameover(h);}break; } } /*登入與注冊*/ void iregister(U *h) { char name[20]; char mm[20]; gotoxy(51,6); printf("注冊") ; gotoxy(39,11); create(h);//注冊 gotoxy(39,16); printf("注冊成功?。?!(按任意鍵登入)\n"); HideCursor(); _getch();system("cls"); } void dengru(U *h,int num) { char name[20];char mm[20]; o: gotoxy(39,16); printf("請輸入用戶名:\n"); gotoxy(39,17); scanf("%s",name); strcpy(s[num].NAME,name);//將用戶名存入結(jié)構(gòu)體數(shù)組 gotoxy(39,18); printf("請輸入密碼:\n"); gotoxy(39,19); scanf("%s",mm); if(!search(h,name)) { system("cls");gotoxy(39,16);printf("沒有該用戶信息,請先注冊!??!(按任意鍵繼續(xù))"); _getch();system("cls") ;insert(h);system("cls"); goto o; } U *temp=search(h,name);//判斷密碼 if(strcmp(temp->mm,mm)) {system("cls");gotoxy(39,17);printf("密碼錯誤,請重新輸入?。ò慈我怄I繼續(xù))");_getch();system("cls") ;dengru(h,num);} else{gotoxy(39,20);printf("登入成功!!!(按任意鍵開始游戲)");_getch();system("cls");} } void log_in(U *h) { FILE*fp; char name[20];char time[20]; if ((fp = fopen("d:\\informeation storage.txt", "a+")) == NULL) { printf("文件不存在,創(chuàng)建成功"); } M: gotoxy(36,9); printf("為了為您的賬號簽到,請再次輸入賬號\n"); gotoxy(42,10); scanf("%s", name); if(!search(h,name)) {gotoxy(42,11);printf("輸入錯誤,請重新輸入");_getch();system("cls");goto M; } fputs(name, fp); gotoxy(36,11); printf("請輸入登入的時間\n"); gotoxy(42,12); scanf("%s", time); fputs(time, fp); fclose(fp); } int main () { system("mode con cols=100 lines=30"); //創(chuàng)建寬100高30的程序界面大小 U *head=(U*)malloc(sizeof(U)); //建立一個帶頭的鏈表 iregister(head); L:dengru(head,num);menu1(head,num);startup();//初始化 while(1) { gotoxy(0,0);show();seescore(num); if(f==0)//判斷飛機的狀態(tài) { gameover(head);s[num].SCORE=score;num++;if(num==10) num=1; msort(s,num+1); goto L; } b_Fly();Fly();Planefly(); } return 0; }
到此這篇關(guān)于C語言實現(xiàn)飛機大戰(zhàn)小游戲完整代碼的文章就介紹到這了,更多相關(guān)C語言飛機大戰(zhàn)小游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++精要分析右值引用與完美轉(zhuǎn)發(fā)的應用
C++11標準為C++引入右值引用語法的同時,還解決了一個短板,即使用簡單的方式即可在函數(shù)模板中實現(xiàn)參數(shù)的完美轉(zhuǎn)發(fā)。那么,什么是完美轉(zhuǎn)發(fā)?它為什么是C++98/03 標準存在的一個短板?C++11標準又是如何為C++彌補這一短板的?別急,本節(jié)將就這些問題給讀者做一一講解2022-05-05C++實現(xiàn)LeetCode(642.設計搜索自動補全系統(tǒng))
這篇文章主要介紹了C++實現(xiàn)LeetCode(642.設計搜索自動補全系統(tǒng)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08

關(guān)于C語言程序的內(nèi)存分配的入門知識學習

stl容器set,map,vector之erase用法與返回值詳細解析

C++中關(guān)鍵字Struct和Class的區(qū)別