首页 > 代码库 > c++ 打飞机游戏开发日志

c++ 打飞机游戏开发日志


设计思路:
控制台模式
  初始化:
  建立画面,初始化数据
  游戏过程:
    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++ 打飞机游戏开发日志