C++編寫實(shí)現(xiàn)飛機(jī)大戰(zhàn)
本文實(shí)例為大家分享了C++編寫實(shí)現(xiàn)飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
前幾天看大佬寫了個(gè)神經(jīng)網(wǎng)絡(luò)訓(xùn)練AI玩飛機(jī)大戰(zhàn),我想,憑我現(xiàn)有知識(shí)能不能也寫一個(gè)飛機(jī)大戰(zhàn),就進(jì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;
};到此我們?cè)O(shè)計(jì)了飛機(jī)的模型(我水平不夠 整個(gè)游戲就用一個(gè)類了- -,這個(gè)類其實(shí)是整個(gè)游戲的類 不是飛機(jī)類)關(guān)于變量cont的說(shuō)明我放在后面了 接下來(lái)我寫了一個(gè)初始化函數(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ù)的隨機(jī)數(shù)得到敵機(jī)的位置,這里其實(shí)有個(gè)問(wèn)題,因?yàn)闀r(shí)間是按一定順序均勻變化的,我們?nèi)绻苯佑脮r(shí)間作隨機(jī)數(shù)種子的話,敵機(jī)的出現(xiàn)會(huì)非常均勻,因此我引入了一個(gè)cont變量,用來(lái)打亂我們均勻的時(shí)間參數(shù)的個(gè)位數(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;
}這個(gè)函數(shù)就是隨機(jī)生成敵機(jī)的位置,cont在此就起到打亂隨機(jī)生成數(shù)的個(gè)位數(shù)的目的,每更新一次,cont++,為防止cont過(guò)大,我規(guī)定cont==10時(shí),就將cont = 0,使其能在1到9變化,影響個(gè)位數(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;
}這里是一個(gè)打印的函數(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();
}這個(gè)函數(shù)我其實(shí)感覺自己寫的太大了,應(yīng)該進(jìn)一步分裝,這確實(shí)是個(gè)不足之處。具體操作就是每輪對(duì)飛機(jī)的移動(dòng),還有子彈和敵機(jī)的前進(jìn)以及判斷子彈是否達(dá)到敵機(jī)和我們的飛機(jī)是否撞到敵機(jī)。其中我用到了windows.h文件中的GetAsyncKeyState函數(shù),其參數(shù)為鍵盤某個(gè)鍵的VK值(可查表),返回一個(gè)16個(gè)位的數(shù)(因操作系統(tǒng)不同而不同,我的計(jì)算機(jī)是返回16位)。若該鍵在上次判斷到此次判斷之間被按下過(guò),則0號(hào)位為1,反之為0;若該鍵正在被按下,則15號(hào)位為1,反之為0.將返回值與0x8000作“與&”操作,則第一位的數(shù)字決定了我們&操作的結(jié)果。因?yàn)閄XXX XXXX XXXX XXXX & 1000 0000 0000 0000 == X000 0000 0000 0000.從而操控我們的飛機(jī)。
void plane::start()
{
? ? reset();
? ? while(1)
? ? {
? ? ? ? Sleep(50);
? ? ? ? update_print();
? ? }
}開始函數(shù),用以從類外部訪問(wèn)類內(nèi)的private函數(shù),并且組織起循環(huán)。
然后 用主函數(shù)運(yùn)行即可。
int main()
{
? ? plane plane_game;
? ? plane_game.start();
? ? return 0;
}效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++日期與時(shí)間 chrono庫(kù)介紹及使用教程
chrono庫(kù)是C++11中的一個(gè)標(biāo)準(zhǔn)庫(kù),它提供了一系列與時(shí)間相關(guān)的類和函數(shù),用于表示和處理時(shí)間間隔,時(shí)鐘和時(shí)間點(diǎn),C++20新增Calendar,這篇文章主要介紹了C++日期與時(shí)間 chrono庫(kù)介紹及使用,需要的朋友可以參考下2023-12-12
詳解C語(yǔ)言中return返回函數(shù)局部變量的問(wèn)題
本文主要介紹了C語(yǔ)言中return返回函數(shù)局部變量的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
淺談stringstream 的.str()正確用法和清空操作
下面小編就為大家?guī)?lái)一篇淺談stringstream 的.str()正確用法和清空操作。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
利用Qt實(shí)現(xiàn)獲取計(jì)算機(jī)的硬件信息
在開發(fā)時(shí),常常會(huì)需要用到計(jì)算機(jī)的相關(guān)信息。利用這些信息,我們可以開發(fā)一些輔助模塊。本文將利用Qt實(shí)現(xiàn)獲取計(jì)算機(jī)的硬件信息,感興趣的可以嘗試一下2022-12-12
C++ 賦值構(gòu)造函數(shù)注意點(diǎn)介紹
下面小編就為大家?guī)?lái)一篇C++ 賦值構(gòu)造函數(shù)注意點(diǎn)介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
Visual Studio Code (VSCode) 配置搭建 C/C++ 開發(fā)編譯環(huán)境的流程
記得N年前剛開始接觸編程時(shí),使用的是Visual C++6.0,下面這個(gè)可愛的圖標(biāo)很多人一定很熟悉。不過(guò)今天想嘗鮮新的工具 Visual Studio Code 來(lái)搭建C/C++開發(fā)環(huán)境,感興趣的朋友一起看看吧2021-09-09

