最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++編寫實現(xiàn)飛機大戰(zhàn)

 更新時間:2022年06月08日 10:30:21   作者:1coder.  
這篇文章主要為大家詳細介紹了C++編寫實現(xiàn)飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++編寫實現(xiàn)飛機大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下

前幾天看大佬寫了個神經(jīng)網(wǎng)絡訓練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ù)我其實感覺自己寫的太大了,應該進一步分裝,這確實是個不足之處。具體操作就是每輪對飛機的移動,還有子彈和敵機的前進以及判斷子彈是否達到敵機和我們的飛機是否撞到敵機。其中我用到了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)文章

  • Microsoft Visual C++ 程序的部署方法

    Microsoft Visual C++ 程序的部署方法

    由Microsoft Visual C++編譯的程序動態(tài)鏈接到C運行時(/MD 或 /MDd),它必須運行DLL的一份拷貝(通常被叫作MSVCRT.DLL 或 MSVCRxx.DLL,其中xx代表Visual C++的版本)
    2013-04-04
  • QT線程QThread的使用介紹

    QT線程QThread的使用介紹

    在進行桌面應用程序開發(fā)的時候,假設(shè)程序在某些情況要處理復雜邏輯, 如果一個線程去處理,就會導致窗口卡頓,無法處理用戶操作。這就需要使用多線程,其中一個線程處理窗口事件,其他線程進行邏輯運算,多個線程各司其職,不僅可以提高用戶體驗還可以提升程序的執(zhí)行效率
    2022-09-09
  • C++日期與時間 chrono庫介紹及使用教程

    C++日期與時間 chrono庫介紹及使用教程

    chrono庫是C++11中的一個標準庫,它提供了一系列與時間相關(guān)的類和函數(shù),用于表示和處理時間間隔,時鐘和時間點,C++20新增Calendar,這篇文章主要介紹了C++日期與時間 chrono庫介紹及使用,需要的朋友可以參考下
    2023-12-12
  • 詳解C語言中return返回函數(shù)局部變量的問題

    詳解C語言中return返回函數(shù)局部變量的問題

    本文主要介紹了C語言中return返回函數(shù)局部變量的問題,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 淺談stringstream 的.str()正確用法和清空操作

    淺談stringstream 的.str()正確用法和清空操作

    下面小編就為大家?guī)硪黄獪\談stringstream 的.str()正確用法和清空操作。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • 利用Qt實現(xiàn)獲取計算機的硬件信息

    利用Qt實現(xiàn)獲取計算機的硬件信息

    在開發(fā)時,常常會需要用到計算機的相關(guān)信息。利用這些信息,我們可以開發(fā)一些輔助模塊。本文將利用Qt實現(xiàn)獲取計算機的硬件信息,感興趣的可以嘗試一下
    2022-12-12
  • C語言字符串處理的驚天大坑問題解決

    C語言字符串處理的驚天大坑問題解決

    這篇文章主要為大家介紹了C語言字符串處理的驚天大坑問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • C++ 賦值構(gòu)造函數(shù)注意點介紹

    C++ 賦值構(gòu)造函數(shù)注意點介紹

    下面小編就為大家?guī)硪黄狢++ 賦值構(gòu)造函數(shù)注意點介紹。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • Visual Studio Code (VSCode) 配置搭建 C/C++ 開發(fā)編譯環(huán)境的流程

    Visual Studio Code (VSCode) 配置搭建 C/C++ 開發(fā)編譯環(huán)境的流程

    記得N年前剛開始接觸編程時,使用的是Visual C++6.0,下面這個可愛的圖標很多人一定很熟悉。不過今天想嘗鮮新的工具 Visual Studio Code 來搭建C/C++開發(fā)環(huán)境,感興趣的朋友一起看看吧
    2021-09-09
  • opencv求解區(qū)域的內(nèi)接矩形

    opencv求解區(qū)域的內(nèi)接矩形

    這篇文章主要為大家詳細介紹了opencv求解區(qū)域的內(nèi)接矩形,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07

最新評論

平江县| 平乡县| 锦州市| 无极县| 湟源县| 汉寿县| 彭泽县| 南投县| 柘荣县| 喜德县| 巧家县| 平武县| 康定县| 华池县| 天津市| 铜梁县| 芦山县| 佛冈县| 电白县| 石阡县| 福安市| 灵丘县| 泗阳县| 新巴尔虎右旗| 平远县| 虞城县| 沧州市| 东宁县| 隆尧县| 竹山县| 平顶山市| 余干县| 克东县| 包头市| 年辖:市辖区| 襄垣县| 石渠县| 建德市| 云南省| 满洲里市| 凤冈县|