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

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

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

本文實(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的說明我放在后面了 接下來我寫了一個(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è)問題,因?yàn)闀r(shí)間是按一定順序均勻變化的,我們?nèi)绻苯佑脮r(shí)間作隨機(jī)數(shù)種子的話,敵機(jī)的出現(xiàn)會(huì)非常均勻,因此我引入了一個(gè)cont變量,用來打亂我們均勻的時(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ī)定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位)。若該鍵在上次判斷到此次判斷之間被按下過,則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ù),用以從類外部訪問類內(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)文章

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

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

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

    QT線程QThread的使用介紹

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論

花莲县| 铜陵市| 十堰市| 抚松县| 大英县| 班戈县| 夹江县| 巴林左旗| 三台县| 阿图什市| 利川市| 都昌县| 平原县| 五原县| 福清市| 墨江| 通许县| 北海市| 杨浦区| 饶河县| 华阴市| 东港市| 定陶县| 青河县| 阜宁县| 临沧市| 文化| 巴林右旗| 竹北市| 冀州市| 辉南县| 南昌县| 昆明市| 新龙县| 剑川县| 新乐市| 嘉禾县| 丰都县| 垫江县| 调兵山市| 宁安市|