c++ 打飛機遊戲開發日誌

來源:互聯網
上載者:User

標籤:auto   win   init   names   建立   math   ane   date   stat   


設計思路:
控制台模式
  初始化:
  建立畫面,初始化資料
  遊戲過程:
    1.擷取操作
    2.修改資料
    3.更新畫面
  結束:
    關閉畫面,delete動態分配資料

 

4.29日 

  建立遊戲背景,實現飛機移動操作,實現子彈飛行

4.30日

  實現遊戲資料管理,飛機擊落動畫,隨機出現敵機

代碼:

#include<iostream>#include<list>#include<time.h>#include<easyx.h>#include<graphics.h>#include"stdio.h"#include"math.h"#include "dos.h" #include<windows.h>#include<mmsystem.h>#include<cstdlib>#define KEY_DOWN(vk_c) (GetAsyncKeyState(vk_c)&0x8000)using namespace std;/*如何設計一個飛機遊戲?    寫的不咋地類的處理很一般: 重構:    FlyingObject{資料:pos,speed,dir  操作:Getpos Setpos Getspeed Set... Move }等    子類: Bullet Plane控制台模式初始化:    建立畫面,初始化資料遊戲過程:    1.擷取操作    2.修改資料    3.更新畫面結束:    關閉畫面,delete動態分配資料    */double direction[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };struct pos{    double x, y;};class bullet{public:    bullet(pos _p,bool _up):p(_p),speed(1),up(_up){}    inline bool check()    {        if (p.x < 0 || p.y < 0 || p.x>768 || p.y>512)            return false;        return true;    }    void Getimg()    {        loadimage(&img, _T("D:\\bullet1.ico"));    }    void Move();    int Getspeed()    {        return speed;    }    pos Getpos()    {        return p;    }    IMAGE img;private:    pos p;    int speed;    bool up;};void bullet::Move(){    if (up)        p.y -= Getspeed();    else        p.y += Getspeed();}IMAGE background,ash;list<bullet> L;class plane{public:    ~plane()    {    }    plane(int _x, int _y)    {        p.x = _x; p.y = _y; speed = 1;    }    void Move(int dir);    void Setpos(double  x, double y)    {        p.x = x; p.y = y;    }    void Setspeed(double _s)    {        speed = _s;    }    void Shoot(bool up);    void Drawplane();    pos Getpos()    {        return p;    }    IMAGE img;private:    double speed;    pos p;    int life;};list<plane> PlaneList;plane hero(230, 700);void plane::Drawplane(){    putimage(p.x, p.y, &img);}void plane::Move(int dir){    if (p.x + direction[dir][0] >= -20 && p.x + direction[dir][0] <= 500)        Setpos(p.x + direction[dir][0]*speed, p.y + direction[dir][1] * speed);    else if (p.x + direction[dir][0] * speed < -20)        Setpos(500, p.y + direction[dir][1] * speed);    else        Setpos(0, p.y + direction[dir][0] * speed);}void plane::Shoot(bool up){    pos tp = p;    tp.x += 15;    bullet tmp(tp, up);    tmp.Getimg();    L.insert(L.begin(), tmp);    putimage(tp.x, p.y, &tmp.img);    FlushBatchDraw();}void init(){    initgraph(512, 768);    loadimage(&background, _T("D:\\background.jpg"));    loadimage(&hero.img, _T("D:\\hero1.ico"));    loadimage(&ash, _T("D:\\ashes.ico"));    putimage(0, 0, &background);}void check(){    pos tmp;    if (!L.empty())    for (list<bullet>::iterator it = L.begin(); it != L.end();)    {        it->Move();        tmp = it->Getpos();        if (tmp.y < 0 || tmp.y>700)        {            it = L.erase(it);        }        else            it++;    }    for (list<plane>::iterator it = PlaneList.begin(); it != PlaneList.end();)    {        it->Move(2);        tmp = it->Getpos();        if (tmp.y < -20 || tmp.y>750)        {            it = PlaneList.erase(it);        }        else            it++;    }    int d = 24;    for (list<bullet>::iterator it = L.begin(); it != L.end();)    {        auto tbullet = it->Getpos();        bool f = false;        for (auto k = PlaneList.begin(); k != PlaneList.end();)        {            auto tpos = k->Getpos();            tpos.x += 15;            tpos.y += 15;            if ((tbullet.x - tpos.x<d&&tbullet.x - tpos.x>-d) && (tbullet.y - tpos.y<d&&tbullet.y - tpos.y>-d))            {                f = true;                k = PlaneList.erase(k);            }            else k++;        }        if (!f)            it++;        else            it = L.erase(it);    }}void update(){    putimage(0, 0, &background);    if (rand() % 1000 == 0)    {        plane p1(rand()%400+20,0);        p1.Setspeed(0.2);        loadimage(&p1.img, _T("D:\\plane1.ico"));        PlaneList.insert(PlaneList.begin(), p1);    }    hero.Drawplane();    check();    if (!L.empty())        for (list<bullet>::iterator it = L.begin(); it != L.end(); it++)        {            auto t = it->Getpos();            putimage(t.x, t.y, &it->img);        }    for (auto it = PlaneList.begin(); it != PlaneList.end(); it++)    {        it->Drawplane();    }    FlushBatchDraw();}int main(){    init();    BeginBatchDraw();    while (1)    {        update();        if (KEY_DOWN(VK_UP))        {            hero.Move(3);        }        else if (KEY_DOWN(VK_LEFT))        {            hero.Move(1);        }        else if (KEY_DOWN(VK_RIGHT))        {            hero.Move(0);        }        else if (KEY_DOWN(VK_DOWN))        {            hero.Move(2);        }        else if (KEY_DOWN(VK_RETURN) || KEY_DOWN(VK_SPACE))        {            while (KEY_DOWN(VK_RETURN) || KEY_DOWN(VK_SPACE))            { }            if(L.size()<5)                hero.Shoot(true);        }    }    return 0; }

 

5.1日

  感覺類的編寫處理不好,導致有很多重複的代碼。決定重構一下。

 

c++ 打飛機遊戲開發日誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.