·一、需求設計
1.為檢測C語言的學習成果,根據所學的C語言知識,設計程序:飛機大戰小游戲;
2.自行定義變量,函數或結構體,編寫源代碼並進行編譯運行測試;
3.根據編寫的代碼,自行攥寫實驗報告;
·二、系統設計
1 實驗題目:
飛機大戰小游戲
2 游戲描述
飛機大戰是一款驚險刺激的射擊游戲,通過控制飛機的上下左右移動,發射子彈,擊退敵機並贏得分數,玩游戲既可以鍛煉人的反應能力,也可以起到放松身心的作用.
進行C語言面向對象程序設計課程設計主要是在學習C語言這門面向對象語言的基礎上,並通過實踐加深對 C語言面向對象程序設計語言特點的認識與理解。同時,可以提高運用C編程語言解決實際問題的能力;鍛煉實際的編程能力;還能提高調查研究、查閱技術文 獻、資料以及編寫軟件設計文檔的能力。
3 功能要求
【1】 實現游戲難度的選擇,飛機模型以及界面的外觀設計等的功能。
【2】 實現方向操移動的操作和敵機隨機生成功能。
【3】 分數計算功能。
【4】 游戲的開始,暫停,結束以及分數顯示的功能。
三、概要設計
1 簡要設計及所涉及的知識
運用二維數組的加減和循環來設計游戲.通過下,xy的加減實現位置的移動;然后用一系列函數,實現各功能;
2 功能描述
(1) 難度選擇: 1為困難,2為一般,3為簡單,Enter開始游戲;
(2) 操作:按1235控制飛機左下右上移動,空格發射子彈,按8暫停,按0停止游戲並輸出分數;;
(3) 游戲的測評方式為計分制,每擊中一架敵機分數加1,無分數限制;
·四 詳細設計
1定義全局變量
(1) long long int speed = 0;//控制敵機的速度
(2) int position_x, position_y;//飛機的所在位置
(3) int high, width;//地圖的大小
(4) int bullet_x, bullet_y;//子彈的位置
(5) int enemy_x, enemy_y;//敵人的位置
(6) int map[MAX][MAX];
(7) /*地圖符號說明:0表示空白,1表示戰機*的區域,2表示敵人戰機的位置。
(8) 3表示上下圍牆,4表示左右圍牆,5表示子彈的位置*/
(9) int score,time,degree,choice;//分數,時間,難度,重新開始
2 定義功能函數
(1) void starup();//初始化所有的信息
(2) void startMap();//地圖內容初始化
(3) void HideCursor();//隱藏光標 ,不會出現光標的閃爍
(4)void gotoxy(int x, int y);//清理一部分屏幕
(5)void updateWithoutInput();//與輸入無關的更新
(6)void updateWithInput();//與輸入有關的更新
(7)void show();//展示的內容
(8)int menu();//菜單初始化
(9)void showend();//結束菜單
(10)void stop();//暫停游戲
3 制作流程
飛機大戰的主要實現的功能是讓飛機移動,飛機的移動可以通過二維數組實現,首先對飛機的坐標設置初值為屏幕的中間位置,通過橫縱坐標的加減實現左右移動,每移動一個單位清屏一次;
第二個過程是隨機產生敵機,我們可以定義一個隨機數組[]0][y],y是隨機產生的,通過x++實現敵機的前進;在定義一個void函數,當輸入空格鍵時,在當前飛機所在的位置產生一個子彈[x][y],之后x遞減,從而實現子彈的前進;
第三,當敵機和子彈的坐標相同時,子彈擊中敵機,此時將此刻的敵機和子彈同時清除,分數加1,並隨機生成一架敵機,;或當飛機飛出規定區域后,清除該敵機並隨機生成;
實現這三個功能后即可完成基本的操作;之后需要考慮的是敵機速度的調控,在每次敵機x++之前添加一次循環.飛機前進的速度與循環的次數成正比;這樣就實現了速度的快慢;
剩下的問題就是完善其他簡單的內容,如難度選擇,分數的輸出,界面設置及游戲的暫停和終止等;
五、系統實現與測試
5.1開始游戲測試
圖5-1-1 游戲開始及難度選擇界面
5.2難度選擇
圖5-2-1 難度1 困難模式
圖5-2-2 難度2 一般模式
圖5-2-3 難度3 簡單模式
難度選擇后,按Enter開始游戲
5.3游戲開始
圖5-3-1 游戲開始界面
圖5-3-2 游戲正在進行的截圖
按1 2 3 5 進行←↓→↑移動,按 空格 發射子彈射擊
5.4游戲暫停
圖5-4-1 暫停界面
按8 暫停游戲 ,再次輸入8 繼續游戲
5.5游戲結束
圖5-5-1 游戲結束界面
按0結束游戲,並會輸出游戲得分,按任意鍵退出游戲;
5.6 綜合說明
游戲開始后,先進行難度選擇.1 2 3依次為困難,一般和簡單,玩家可根據自身喜好進行難度選擇;難度選擇好后按Enter開始游戲,進入游戲后,按1 2 3 5 進行←↓→↑移動,按 空格 發射子彈射擊,對准敵機進行射擊,
六、結論與心得
通過這段時間學習的C語言課程設計,學會了很多同時也收獲了很多,學會了如何根據根據問題去思考解決的思路,和編寫代碼;雖然這個過程有點枯燥乏味,有時半天都沒思路,想不出該怎么寫怎么算,但是等到真的想到思路的時候,會發現自己之前的付出是值得的,那種喜悅感非常棒;
而且還收獲了好多知識,不光有上學期學到的C語言知識,還有還有好多編程方面的技巧和經驗,這些東西對以后的學習和工作會有很大的幫助;最后通過本次大作業可有讓自己對所學過的知識加以整理和檢驗,既可以整理C語言和編程知識,還可以檢驗自己的一些缺點和不足,為以后的學習和工作打下基礎;
七、源代碼
1、int main();//主函數
#include<stdio.h> #include<string.h> #include<conio.h>//清屏所需頭文件 #include<windows.h>//清屏所需頭文件 #include<stdlib.h>
//*********定義全局變量*********
long long int speed = 0;//控制敵機的速度
int position_x, position_y;//飛機的所在位置
int high, width;//地圖的大小
int bullet_x, bullet_y;//子彈的位置
int enemy_x, enemy_y;//敵人的位置
int map[MAX][MAX];
/*地圖符號說明:0表示空白,1表示戰機*的區域,2表示敵人戰機的位置。
3表示上下圍牆,4表示左右圍牆,5表示子彈的位置*/
int score,time,degree,choice;//分數,時間,難度,重新開始
//********定義功能函數*********
void starup();//初始化所有的信息
void startMap();//地圖內容初始化
void HideCursor();//隱藏光標 ,不會出現光標的閃爍
void gotoxy(int x, int y);//清理一部分屏幕
void updateWithoutInput();//與輸入無關的更新
void updateWithInput();//與輸入有關的更新
void show();//展示的內容
int menu();//菜單初始化
void showend();//結束菜單
void stop();//暫停游戲
#define MAX 100 int main() { int a; if(menu()) starup(); while (1) { HideCursor(); startMap(); show(); updateWithoutInput(); updateWithInput(); } return 0; }
2、void starup();//初始化所有的信息
void starup()//初始化所有的信息 { high = 20; width = 30; position_x = high / 2; position_y = width / 2; bullet_x = 0; bullet_y = position_y; enemy_x = 2; enemy_y = position_y - 1; score = 0; }
3、void startMap();//地圖內容初始化
void startMap()//地圖內容初始化 { int i, j; for (i = 1; i <= high -1; i++) { map[i][1] = 4; for (j = 2; j <= width - 1; j++) map[i][j] = 0; map[i][width] = 4; } //下方圍牆的初始化 i = high; for (j = 1; j <= width; j++) map[i][j] = 3; map[bullet_x][bullet_y] = 5; /*這里是戰機大小的初始化開始*/ map[position_x - 1][position_y] = 1; i = position_x; for (j = position_y - 2; j <= position_y + 2; j++) map[i][j] = 1; map[position_x + 1][position_y - 1] = 1; map[position_x + 1][position_y + 1] = 1; /*** 初始化結束 **/ /* 敵人戰機的初始化 */ map[enemy_x][enemy_y] = 2; map[enemy_x - 1][enemy_y - 1] = 2; map[enemy_x - 1][enemy_y + 1] = 2; /* 敵人戰機初始化結束*/ }
4、void HideCursor();//隱藏光標 ,不會出現光標的閃爍
void HideCursor()//隱藏光標 ,不會出現光標的閃爍 { CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }
5、void gotoxy(int x, int y);//清理一部分屏幕
void gotoxy(int x, int y)//清理一部分屏幕 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos); }
6、void updateWithoutInput();//與輸入無關的更新
void updateWithoutInput()//與輸入無關的更新 { if (bullet_x > 0)//實現子彈的前進 bullet_x--; if (((bullet_x == enemy_x) && (bullet_y == enemy_y))||((bullet_x == enemy_x-1) && (bullet_y == enemy_y-1))||((bullet_x == enemy_x+1) && (bullet_y == enemy_y+1)))//當敵人的飛機被擊中時,同時清子彈和飛機 { score++; enemy_x = 0; enemy_y = rand() % width;//隨機產生新敵機 bullet_x = 0; } else if (enemy_x > high)//當飛機超出區域 { enemy_x = 0; enemy_y = rand() % width;//隨機產生新敵機 } if (speed == 1) for (int i = 1; i <=10*(15*degree-10); i++)//用來控制敵機的速度 ,通過循環的次數實現 { for (int j = 1; j <=10000; j++) { speed = 1; } } speed = 0; if (speed == 0) { enemy_x++; speed = 1; } }
7、void updateWithInput();//與輸入有關的更新
void updateWithInput()//與輸入有關的更新 { char input; if (kbhit()) { input = getch(); if (input == '1') position_y--; if (input == '2') position_x++; if (input == '3') position_y++; if (input == '5') position_x--; if (input =='8') stop(); if( input=='0') showend(); if (input == ' ') { bullet_x = position_x - 1; bullet_y = position_y; } } }
8、void show();//展示的內容
void show()//展示的內容 { gotoxy(0, 0); int i, j; for (i = 1; i <= high; i++) { for (j = 1; j <= width; j++) { if (map[i][j] == 0) printf(" ");// 活動空間 if (map[i][j] == 1) printf("*");//控制的飛機 if (map[i][j] == 2) printf("#");//敵方飛機 if (map[i][j] == 3) printf("~");//下邊界 if (map[i][j] == 4) printf("|");//左右邊界 if (map[i][j] == 5) printf(":");//子彈 } printf("\n"); } printf("\n你的得分:%d\n\n", score); printf("操作說明: 1235分別操作 左下右上四個的移動\n"); printf("**空格是發出子彈**\n"); }
9、int menu();//菜單初始化
int menu() { printf("******************************\n") ; printf("* *\n") ; printf("* 歡迎來到飛機大戰 *\n") ; printf("* *\n") ; printf("* Hard == 1 *\n") ; printf("* Normal == 2 *\n") ; printf("* Easy == 3 *\n") ; printf("* *\n") ; printf("* Enter開始游戲 *\n") ; printf("* *\n") ; printf("* *\n") ; printf("******************************\n") ; scanf("%d",°ree); system("cls"); return degree; }
10、void showend();//結束菜單
void showend() { int s; system("cls"); printf("******************************\n") ; printf("* *\n") ; printf("* Gime Over ! *\n") ; printf("* *\n") ; printf("* Your scores are %d! *\n",score); printf("* *\n") ; printf("* Very Good ! *\n") ; printf("* *\n") ; printf("* 按任意鍵退出 *\n") ; printf("* *\n") ; printf("* *\n") ; printf("* *\n") ; printf("******************************\n") ; exit(0); }
11、void stop();//暫停游戲
void stop() { int s; system("cls"); printf("******************************\n") ; printf("* *\n") ; printf("* Gime Pause ! *\n") ; printf("* *\n") ; printf("* Your scores are %d! *\n",score); printf("* *\n") ; printf("* 按8繼續游戲 *\n") ; printf("* *\n") ; printf("* 按任意鍵退出 *\n") ; printf("* *\n") ; printf("* *\n") ; printf("* *\n") ; printf("******************************\n") ; scanf("%d",&s); if(s!=8) exit(0); }