C++編寫實現(xiàn)飛機大戰(zhàn)
本文實例為大家分享了C++編寫實現(xiàn)飛機大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
前幾天看大佬寫了個神經(jīng)網(wǎng)絡(luò)訓練AI玩飛機大戰(zhàn),我想,憑我現(xiàn)有知識能不能也寫一個飛機大戰(zhàn),就進行了嘗試,成果如下。
#include<iostream> #include<ctime> #include<stdlib.h> #include<windows.h> using namespace std; const int mapx = 40, mapy = 35, cost = 2, prise = 5; ? //cost: cost of bullet, ? prise: prise of killing a enemy. class plane { ? ? public: ? ? ? ? void start(); ? ? private: ? ? ? ? void reset(); ? ? ? ? void get_enemy(int &y); ? ? ? ? void print() const; ? ? ? ? void update_print(); ? ? ? ? char map[mapx][mapy];/* ? ? ?plane model: ? ? ?/=|=\ ? ? ? ? ? */ ? ? ? ? int plane_y, plane_x, score, cont; };
到此我們設(shè)計了飛機的模型(我水平不夠 整個游戲就用一個類了- -,這個類其實是整個游戲的類 不是飛機類)關(guān)于變量cont的說明我放在后面了 接下來我寫了一個初始化函數(shù),為類內(nèi)變量初始化。
void plane::reset() { ? ? for(int i = 0; i < mapx; i++) ? ? { ? ? ? ? for(int j = 0; j < mapy; j++) ? ? ? ? { ? ? ? ? ? ? if(!i || !j || j == mapy - 1) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? map[i][j] = '#'; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? ? ? map[i][j] = ' '; ? ? ? ? } ? ? } ? ? plane_x = mapx - 1; ? ? plane_y = mapy/2 - 2; ? ? score = cont = 0; ? ? map[plane_x][plane_y] = '/'; ? ? map[plane_x][plane_y + 1] = map[plane_x][plane_y + 3] = '='; ? ? map[plane_x][plane_y + 2] = '|'; ? ? map[plane_x][plane_y + 4] = '\\'; }
然后我利用時間參數(shù)的隨機數(shù)得到敵機的位置,這里其實有個問題,因為時間是按一定順序均勻變化的,我們?nèi)绻苯佑脮r間作隨機數(shù)種子的話,敵機的出現(xiàn)會非常均勻,因此我引入了一個cont變量,用來打亂我們均勻的時間參數(shù)的個位數(shù)。具體使用見后文。
void plane::get_enemy(int &y) const { ? ? srand(int(time(0))); ? ? int n = rand(); ? ? if(cont%2) ? ? ? ? n -= cont; ? ? else ? ? ? ? n += cont; ? ? y = n % (mapy - 2) + 1; }
這個函數(shù)就是隨機生成敵機的位置,cont在此就起到打亂隨機生成數(shù)的個位數(shù)的目的,每更新一次,cont++,為防止cont過大,我規(guī)定cont==10時,就將cont = 0,使其能在1到9變化,影響個位數(shù)。
void plane::print() const { ? ? system("cls"); ? ? for(int i = 0; i < mapx; i++) ? ? { ? ? ? ? for(int j = 0; j < mapy; j++) ? ? ? ? { ? ? ? ? ? ? cout<<map[i][j]; ? ? ? ? } ? ? ? ? cout<<endl; ? ? } ? ? cout<<"Score : "<<score<<'.'<<endl<<"Pay "<<cost<<" scores to send '+' and get "<<prise<<" scores by killing enemies."<<endl; }
這里是一個打印的函數(shù),不贅述。
void plane::update_print() { ? ? for(int i = 1; i < mapx; i++) ? ? { ? ? ? ? for(int j = 1; j < mapy - 1; j++) ? ? ? ? { ? ? ? ? ? ? if(map[i][j] == 'M') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(i == mapx - 1) ? ? ? ? ? ? ? ? ? ? map[i][j] = ' '; ? ? ? ? ? ? ? ? else if(map[i + 1][j] == '+') ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? map[i][j] = map[i+1][j] = ' '; ? ? ? ? ? ? ? ? ? ? score += prise; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? else if(map[i][j] == '+') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? map[i][j] = ' '; ? ? ? ? ? ? ? ? if(i != 1) ? ? ? ? ? ? ? ? ? ? map[i-1][j] = '+'; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? for(int i = mapx - 2; i > 0; i--) ? ? { ? ? ? ? for(int j = 1; j < mapy - 1; j++) ? ? ? ? { ? ? ? ? ? ? if(map[i][j] == 'M') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(i != mapx - 1) ? ? ? ? ? ? ? ? ? ? if(map[i+1][j] == '+') ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? map[i + 1][j] = ' '; ? ? ? ? ? ? ? ? ? ? ? ? score += prise; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? map[i + 1][j] = 'M'; ? ? ? ? ? ? ? ? map[i][j] = ' '; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? int enemy_y; ? ? get_enemy(enemy_y); ? ? if(map[1][enemy_y] == '+') ? ? { ? ? ? ? map[1][enemy_y] = ' '; ? ? ? ? score += prise; ? ? } ? ? else ? ? ? ? map[1][enemy_y] = 'M'; ? ? ? ?? ? ? for(int i = 0; i < 5; i++) ? ? { ? ? ? ? if(map[plane_x][plane_y + i] != 'M') ? ? ? ? ? ? map[plane_x][plane_y + i] = ' '; ? ? } ? ? bool jleft, jright, jup, jdown; ? ? jleft = jright = jup = jdown = false; ? ? if(GetAsyncKeyState(VK_LEFT) & 0x8000) ? ? ? ? if(plane_y != 1) ? ? ? ? ? ? jleft = true; ? ? if(GetAsyncKeyState(VK_RIGHT) & 0x8000) ? ? ? ? if(plane_y + 4 != mapy - 2) ? ? ? ? ? ? jright = true; ? ? if(GetAsyncKeyState(VK_UP) & 0x8000) ? ? ? ? if(plane_x != 1) ? ? ? ? ? ? jup = true; ? ? if(GetAsyncKeyState(VK_DOWN) & 0x8000) ? ? ? ? if(plane_x != mapx - 1) ? ? ? ? ? ? jdown = true; ? ? if(!(jleft && jright)) ? ? { ? ? ? ? if(jleft) ? ? ? ? ? ? plane_y--; ? ? ? ? if(jright) ? ? ? ? ? ? plane_y++; ? ? } ? ? if(!(jup && jdown)) ? ? { ? ? ? ? if(jup) ? ? ? ? ? ? plane_x--; ? ? ? ? if(jdown) ? ? ? ? ? ? plane_x++; ? ? } ? ? if(GetAsyncKeyState(VK_SPACE) & 0x8000) ? ? ? ? { ? ? ? ? ? ? score -= cost; ? ? ? ? ? ? if(map[plane_x - 1][plane_y + 2] == ' ') ? ? ? ? ? ? ? ? map[plane_x - 1][plane_y + 2] = '+'; ? ? ? ? ? ? else if(map[plane_x - 1][plane_y + 2] == 'M') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? map[plane_x - 1][plane_y + 2] = ' '; ? ? ? ? ? ? ? ? score += prise; ? ? ? ? ? ? } ? ? ? ? } ? ? if(map[plane_x][plane_y]=='M'||map[plane_x][plane_y+1]=='M'|| ? ? map[plane_x][plane_y+2]=='M'||map[plane_x][plane_y+3]=='M'||map[plane_x][plane_y+4]=='M') ? ? { ? ? ? ? system("cls"); ? ? ? ? for(int i = 0; i < mapx; i++) ? ? ? ? { ? ? ? ? ? ? cout<<"GAME OVER."<<endl; ? ? ? ? } ? ? ? ? cout<<"Your final scores are "<<score<<'.'<<endl; ? ? ? ? system("pause"); ? ? ? ? exit(1); ? ? } ? ? map[plane_x][plane_y] = '/'; ? ? map[plane_x][plane_y + 1] = map[plane_x][plane_y + 3] = '='; ? ? map[plane_x][plane_y + 2] = '|'; ? ? map[plane_x][plane_y + 4] = '\\'; ? ? cont++; ? ? if(cont == 10) ? ? ? ? ? ? cont = 0; ? ? print(); }
這個函數(shù)我其實感覺自己寫的太大了,應(yīng)該進一步分裝,這確實是個不足之處。具體操作就是每輪對飛機的移動,還有子彈和敵機的前進以及判斷子彈是否達到敵機和我們的飛機是否撞到敵機。其中我用到了windows.h文件中的GetAsyncKeyState函數(shù),其參數(shù)為鍵盤某個鍵的VK值(可查表),返回一個16個位的數(shù)(因操作系統(tǒng)不同而不同,我的計算機是返回16位)。若該鍵在上次判斷到此次判斷之間被按下過,則0號位為1,反之為0;若該鍵正在被按下,則15號位為1,反之為0.將返回值與0x8000作“與&”操作,則第一位的數(shù)字決定了我們&操作的結(jié)果。因為XXXX XXXX XXXX XXXX & 1000 0000 0000 0000 == X000 0000 0000 0000.從而操控我們的飛機。
void plane::start() { ? ? reset(); ? ? while(1) ? ? { ? ? ? ? Sleep(50); ? ? ? ? update_print(); ? ? } }
開始函數(shù),用以從類外部訪問類內(nèi)的private函數(shù),并且組織起循環(huán)。
然后 用主函數(shù)運行即可。
int main() { ? ? plane plane_game; ? ? plane_game.start(); ? ? return 0; }
效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言數(shù)據(jù)類型枚舉enum全面詳解示例教程
生活中有很多地方會用到枚舉,比如一周有7天,可以一一枚舉;性別有男、女...等等都可以可以一一枚舉,今天來和筆者一起學習一下c語言枚舉吧2021-10-10C語言?超詳細介紹與實現(xiàn)線性表中的無頭單向非循環(huán)鏈表
無頭單向非循環(huán)鏈表:結(jié)構(gòu)簡單,一般不會單獨用來存數(shù)據(jù)。實際中更多是作為其他數(shù)據(jù)結(jié)構(gòu)的子結(jié)構(gòu),如哈希桶、圖的鄰接表等等。另外這種結(jié)構(gòu)在筆試面試中出現(xiàn)很多2022-03-03c++ sqlite3如何利用事務(wù)(BEGIN;COMMIT;)批量操作
這篇文章主要介紹了c++ sqlite3如何利用事務(wù)(BEGIN;COMMIT;)批量操作,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08