项目: 用封装的方法实现飞机大战游戏
生活随笔
收集整理的這篇文章主要介紹了
项目: 用封装的方法实现飞机大战游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、項目描述和最終的成果展示
- 二、用函數進行封裝
- 三、新型的發射子彈功能
- 四、實現移動的敵機功能和更正屏幕閃爍,清除光標功能
- 五、訂正一些BUG和完成一些美化
這是上一次的飛機大戰游戲的項目。項目: 最簡單的飛機大戰游戲
上次沒有用函數進行的封裝。這次在上次的基礎上進行封裝和一些功能的優化。
一、項目描述和最終的成果展示
項目描述: 在上一次的基礎上用函數進行了封裝,對于一些功能也進行了一些優化。最終效果圖如下:
二、用函數進行封裝
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h>//全局變量 int position_x,position_y;//飛機位置 int high,width;//游戲畫面尺寸void startup()//數據的初始化 {high = 20;width = 30;position_x = high/2;//飛機的上下位置position_y = width/2;//飛機的左右·位置 }void show()//顯示畫面 {system("cls");int i,j;for(i=0;i<high;i++){for(j=0;j<width;j++){if( (i == position_x) && (j== position_y))//輸出飛機printf("☆");elseprintf(" ");}printf("\n");} }void updateWithoutInput()//與用戶輸入無關的更新 {}void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit())//判斷有無輸入{input=getch();if( input == 'a' || input == 'A')position_y--;//左移if( input == 'd' || input == 'D')position_y++;//右移if( input == 'w' || input == 'W')position_x--;//上移if( input == 's' || input == 'S')position_x++;//下移} }int main(void) {startup(); //數據的初始化while(1){show();//顯示畫面updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新}return 0; }三、新型的發射子彈功能
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h>//全局變量 int position_x,position_y;//飛機位置 int high,width;//游戲畫面尺寸 int bullet_x,bullet_y;//子彈位置//定義隱藏光標函數 void HideCursor() {CONSOLE_CURSOR_INFO cursor; cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor); }void startup()//數據的初始化 {high = 120;width = 100;position_x = high/2;//飛機的上下位置position_y = width/2;//飛機的左右·位置bullet_x = 0;bullet_y = position_y; }void show()//顯示畫面 {system("cls");int i,j;for(i=0;i<high;i++){for(j=0;j<width;j++){if( (i == position_x) && (j== position_y))//輸出飛機printf("☆");else if( (i == bullet_x)&&(j == bullet_y))printf("|");//輸出子彈elseprintf(" ");}printf("\n");} }void updateWithoutInput()//與用戶輸入無關的更新 {if(bullet_x>-1)bullet_x--; }void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit()){input=getch();if( input == 'a' || input == 'A')position_y--;//左移if( input == 'd' || input == 'D')position_y++;//右移if( input == 'w' || input == 'W')position_x--;//上移if( input == 's' || input == 'S')position_x++;//下移if( input == ' '){bullet_x=position_x-1;bullet_y=position_y;}} }int main(void) {startup();//數據的初始化while(1){show();//顯示畫面HideCursor();//隱藏光標,防止光標亂閃。updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新}return 0; }效果圖如下:
四、實現移動的敵機功能和更正屏幕閃爍,清除光標功能
代碼如下;
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h>//全局變量 int position_x,position_y;//飛機位置 int high,width;//游戲畫面尺寸 int bullet_x,bullet_y;//子彈位置 int enemy_x,enemy_y;//敵機的位置 int score;//得分//定義隱藏光標函數 void HideCursor() {CONSOLE_CURSOR_INFO cursor; cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor); }void gotoxy(int x,int y)//將光標移動到(x,y)位置 {HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle,pos); } void startup()//數據的初始化 {system("color 09");high = 30;width =50;position_x = high/2;//飛機的上下位置position_y = width/2;//飛機的左右位置bullet_x = 0;bullet_y = position_y;enemy_x=0;enemy_y=position_y;score=0; }void show()//顯示畫面 {//system("cls");gotoxy(0,0);int i,j;for(i=0;i<high;i++){for(j=0;j<width;j++){if( (i == position_x) && (j== position_y))//輸出飛機printf("☆");else if( (i == bullet_x)&&(j == bullet_y))printf("|");//輸出子彈else if( (i== enemy_x) && ( j==enemy_y))printf("*");//輸出敵機elseprintf(" ");//輸出空格}printf("\n");}printf("得分:%d\n",score); }void updateWithoutInput()//與用戶輸入無關的更新 {static int speed=0;if(bullet_x>-1)bullet_x--;if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子彈擊中飛機{score++;//分數無效enemy_x=-1;//產生新的敵機enemy_y=rand()%width;bullet_x=-2;//子彈無效}// 用來控制敵機向下移動的速度,每隔幾次循環才移動一次敵機// 這樣修改,雖然用戶按鍵的交互速度還是很快,但是NPC的移動顯示可以降速if(speed<10)speed++;if(speed == 10 ){enemy_x++;speed = 0;} }void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit()){input=getch();if( input == 'a' || input == 'A')position_y--;//左移if( input == 'd' || input == 'D')position_y++;//右移if( input == 'w' || input == 'W')position_x--;//上移if( input == 's' || input == 'S')position_x++;//下移if( input == ' '){bullet_x=position_x-1;bullet_y=position_y;}} }int main(void) {startup();//數據的初始化while(1){show();//顯示畫面HideCursor();//隱藏光標,防止光標亂閃。updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新}return 0; }效果圖如下:
五、訂正一些BUG和完成一些美化
我們的項目基本是已經完成了。但是還有很多的漏洞。 比如: 飛機控制越界問題,以及敵機越界問題。 而且界面不夠好看我們要再美化一下。 以及增加游戲暫停功能。 游戲結束功能。代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h>//全局變量 int position_x,position_y;//飛機位置 int high,width;//游戲畫面尺寸 int bullet_x,bullet_y;//子彈位置 int enemy_x,enemy_y;//敵機的位置 int score;//得分//定義隱藏光標函數 void HideCursor() {CONSOLE_CURSOR_INFO cursor; cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor); }void gotoxy(int x,int y)//將光標移動到(x,y)位置 {HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle,pos); } void startup()//數據的初始化 {system("color 09");high = 30;width =50;position_x = high/2;//飛機的上下位置position_y = width/2;//飛機的左右位置bullet_x = 0;bullet_y = position_y;enemy_x=0;enemy_y=position_y;score=0; }void show()//顯示畫面 {//system("cls");gotoxy(0,0);int i,j;for(i=0;i<high;i++){for(j=0;j<width;j++){if( (i == position_x) && (j== position_y))//輸出飛機printf("☆");else if( (i == bullet_x)&&(j == bullet_y))printf("|");//輸出子彈else if( (i== enemy_x) && ( j==enemy_y))printf("*");//輸出敵機else if(j==width-1&&i==position_x)//飛機那一行,因為有飛機多占一格,所以要刪除前面的一個空格printf("\b+");else if(j==width-1)printf("+");else if(i==high-1)printf("-");elseprintf(" ");//輸出空格}printf("\n");}printf("得分:%d\n",score);printf("按1鍵游戲暫停\n"); }void updateWithoutInput()//與用戶輸入無關的更新 {static int speed=0;if(bullet_x>-1)bullet_x--;if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子彈擊中飛機{score++;//分數無效enemy_x=-1;//產生新的敵機enemy_y=rand()%width+1;bullet_x=-2;//子彈無效}// 用來控制敵機向下移動的速度,每隔幾次循環才移動一次敵機// 這樣修改,雖然用戶按鍵的交互速度還是很快,但是NPC的移動顯示可以降速if(speed<6)speed++;if(speed == 6 ){enemy_x++;if(enemy_x==high-1)//如果飛機越界再次生成{enemy_x=-1;//產生新的敵機enemy_y=rand()%width+1;}if( enemy_x==position_x-1)//撞機了 游戲結束{system("cls");printf("飛機墜毀了,游戲結束\n");printf("分數為:%d\n",score);printf("請重啟再開始新的一局\n");while(1){}}speed = 0;} }void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit()){input=getch();if( input == 'a' || input == 'A'){position_y--;//左移if(position_y==0)//判斷是否越界{position_y++;}}if( input == 'd' || input == 'D'){position_y++;//右移if(position_y==width-2)//判斷是否越界{position_y--;}}if( input == 'w' || input == 'W'){position_x--;//上移if(position_x==1)//判斷是否越界{position_x++;}}if( input == 's' || input == 'S'){position_x++;//下移if(position_x==high-1)//判斷是否越界{position_x--;}}if( input == ' ')//發射子彈{bullet_x=position_x-1;bullet_y=position_y;}if( input == '1')//按1鍵游戲暫停{while(1){input=getch();if(input == '1')//再按1鍵游戲繼續break;}}} }int main(void) {startup();//數據的初始化while(1){show();//顯示畫面HideCursor();//隱藏光標,防止光標亂閃。updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新}return 0; }效果圖如下:
總結
以上是生活随笔為你收集整理的项目: 用封装的方法实现飞机大战游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目: 最简单的飞机大战游戏
- 下一篇: 项目: 用函数实现反弹球消砖块