亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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;
};

到此我們設(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è)不足之處。具體操作就是每輪對飛機(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;
}

效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言數(shù)據(jù)類型枚舉enum全面詳解示例教程

    C語言數(shù)據(jù)類型枚舉enum全面詳解示例教程

    生活中有很多地方會(huì)用到枚舉,比如一周有7天,可以一一枚舉;性別有男、女...等等都可以可以一一枚舉,今天來和筆者一起學(xué)習(xí)一下c語言枚舉吧
    2021-10-10
  • C++ 內(nèi)存管理原理分析

    C++ 內(nèi)存管理原理分析

    本章主要介紹C++的內(nèi)存管理,以C++的內(nèi)存分布作為引入,介紹C++不同于C語言的內(nèi)存管理方式(new delete對比 malloc free),最后為了加深讀者的理解,會(huì)介紹new和delete的底層實(shí)現(xiàn)原理
    2021-11-11
  • C語言中.c和.h文件區(qū)別講解

    C語言中.c和.h文件區(qū)別講解

    這篇文章主要介紹了C語言中.c和.h文件區(qū)別講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是本文的詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言?超詳細(xì)介紹與實(shí)現(xiàn)線性表中的無頭單向非循環(huán)鏈表

    C語言?超詳細(xì)介紹與實(shí)現(xiàn)線性表中的無頭單向非循環(huán)鏈表

    無頭單向非循環(huán)鏈表:結(jié)構(gòu)簡單,一般不會(huì)單獨(dú)用來存數(shù)據(jù)。實(shí)際中更多是作為其他數(shù)據(jù)結(jié)構(gòu)的子結(jié)構(gòu),如哈希桶、圖的鄰接表等等。另外這種結(jié)構(gòu)在筆試面試中出現(xiàn)很多
    2022-03-03
  • 淺析VC++中的頭文件包含問題

    淺析VC++中的頭文件包含問題

    類中盡量采用指針或引用方式調(diào)用其它類,這樣就可以只聲明class xxx了。并且這也符合資源最優(yōu)利用,更利于使用多態(tài)
    2013-09-09
  • JsonCpp中double的問題解決

    JsonCpp中double的問題解決

    本文主要介紹了JsonCpp中double的問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C++?STL之string的模擬實(shí)現(xiàn)實(shí)例代碼

    C++?STL之string的模擬實(shí)現(xiàn)實(shí)例代碼

    C++中有命名空間的存在,我們只需把我們的代碼封到自定義的命名空間即可,下面這篇文章主要給大家介紹了關(guān)于C++?STL之string的模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • c++ sqlite3如何利用事務(wù)(BEGIN;COMMIT;)批量操作

    c++ sqlite3如何利用事務(wù)(BEGIN;COMMIT;)批量操作

    這篇文章主要介紹了c++ sqlite3如何利用事務(wù)(BEGIN;COMMIT;)批量操作,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C/C++新建注冊表項(xiàng)的代碼示例

    C/C++新建注冊表項(xiàng)的代碼示例

    今天小編就為大家分享一篇關(guān)于C/C++新建注冊表項(xiàng)的代碼示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 常用的C語言排序算法(兩種)

    常用的C語言排序算法(兩種)

    本文給大家分享兩種常用的C語言排序算法,代碼非常簡單,感興趣的朋友可以參考下
    2016-09-09

最新評論