先上效果图。
游戏的实现主要基于EasyX图形库,有暂停界面、结束界面、游戏音效、游戏存档,并最终集成为一个exe文件。可谓麻雀虽小,五脏俱全 。这里先放出源代码和下载链接,以后有时间会以此为例写一个C++小游戏开发教程系列。
链接:https://pan.baidu.com/s/1sBPD-LTGCTa7bfkI1RloXg
提取码:8848
组件类及其定义:
#pragma once
#include
#include
using namespace std;
typedef pair BVec;
class Object
{
int _x;
int _y; //组件基类的四个成员,逻辑坐标(x,y),运动速度v,加速度g,贴图ptr、掩码ym_ptr
int _width;
int _height;
BVec _v;
BVec _g;
IMAGE _ptr;
IMAGE ym_ptr;
public:
Object(LPCTSTR path, LPCTSTR ympath, int width, int height, int posx= 0, int posy= 0);//构造函数
Object(LPCTSTR path, int width, int height, int posx = 0, int posy = 0);
Object(LPCTSTR path);
Object():_x(0),_y(0),_width(0),_height(0),_v(make_pair(0,0)),_g(make_pair(0,0)){}
int x()const{ return _x; };
int y()const { return _y; };
BVec v()const { return _v; }; //数据成员操作接口
BVec g()const { return _g; }
int width()const { return _width; };
int height()const { return _height; };
void setx(int newx) { _x = newx; };
void sety(int newy) { _y = newy; };
void setv(int x, int y) { _v.first = x; _v.second = y; };
void setvx(int x) { _v.first = x; };
void setvy(int y) { _v.second = y; }
void setg(BVec newg) { _g = newg; };
void setptr(IMAGE newptr) { _ptr = newptr; }
void setptr_ym(IMAGE newptr_ym) { ym_ptr = newptr_ym; }
void setwidth(int newwidth) { _width = newwidth; }
void setheight(int newheight) { _height = newheight; }
void move();//此方法更新组件位置。
void show_n()const; //显示组件,不显示掩码
void show()const; //显示组件和掩码
~Object() { };
};
#include "Object.h"
Object::Object(LPCTSTR path,LPCTSTR ympath, int width, int height, int posx , int posy )
{
loadimage(&_ptr, _T("PNG"), path, width, height);
loadimage(&ym_ptr,_T("PNG"), ympath, width, height);
_x = posx;
_y = posy;
_width = width;
_height = height;
_v.first = 0;
_v.second = 0;
_g.first = 0;
_g.second = 0;
}
Object::Object(LPCTSTR path, int width, int height, int posx , int posy )
{
loadimage(&_ptr, _T("PNG"),path, width, height);
_x = posx;
_y = posy;
_width = width;
_height = height;
_v.first = 0;
_v.second = 0;
_g.first = 0;
_g.second = 0;
}
Object::Object(LPCTSTR path)
{
loadimage(&_ptr, path);
_x = 0;
_y = 0;
_width = _ptr.getwidth();
_height = _ptr.getheight();
_v.first = 0;
_v.second = 0;
_g.first = 0;
_g.second = 0;
}
void Object::move()
{
_x += _v.first;
_y += _v.second;
_v.first += _g.first;
_v.second += _g.second;
}
void Object::show_n()const
{
putimage(_x, _y, &_ptr);
}
void Object::show()const
{
putimage(_x, _y, &ym_ptr, SRCAND);
putimage(_x, _y, &_ptr, SRCPAINT);
}
角色类及其定义
#pragma once
#include "Object.h"
#include
class bird : //角色类继承组件类(游戏角色只有小鸟,故名为小鸟类)
public Object //添加动作序列、死亡贴图
{
vector seq;
vector seq_ym;
IMAGE die;
IMAGE die_ym;
int index = 0;
public:
bird();
void add_seq(LPCTSTR path,LPCTSTR ympath);//添加动作序列
void add_die(LPCTSTR path, LPCTSTR ympath);//添加死亡贴图
void select_next();//切换动作
IMAGE& getdie() { return die; }
IMAGE& getdie_ym() { return die_ym; }
};
#include "bird.h"
#include "GamePragma.h"
#include
using std::make_pair;
void bird::add_seq(LPCTSTR path,LPCTSTR ympath)
{
IMAGE img,ym;
loadimage(&img,_T("PNG"), path);
loadimage(&ym, _T("PNG"),ympath);
seq.push_back(img);
seq_ym.push_back(ym);
setptr(seq.back());
setptr_ym(seq_ym.back());
setwidth(img.getwidth());
setheight(img.getheight());
}
void bird::select_next()
{
int len = seq.size();
if (index == len - 1)
{
index = 0;
}
else index++;
setptr(seq[index]);
setptr_ym(seq_ym[index]);
}
bird::bird()
{
setg(make_pair(0, gravity));
setx(100);
sety(200);
}
void bird::add_die(LPCTSTR path, LPCTSTR ympath)
{
loadimage(&die, _T("PNG"), path);
loadimage(&die_ym,_T("PNG"), ympath);
}
障碍物类及其实现
#pragma once
#include "Object.h"
class Tube : //管道类继承组件类,添加方向、和小鸟是否通过。
public Object
{
int direction;
bool if_pass ;
public:
Tube(LPCTSTR path, LPCTSTR ympath, int width, int height, int posx, int posy,int d);
int getd()const { return direction; }
bool getp() { return if_pass; }
void setp(bool p) { if_pass = p; }
};
#include "Tube.h"
Tube::Tube(LPCTSTR path, LPCTSTR ympath, int width, int height, int posx, int posy, int d) :
Object(path, ympath, width, height, posx, posy)
{
direction = d;
if_pass = false;
}
游戏参数定义
#pragma once
#define WinWidth 800
#define WinHeight 600
#define gravity 0.5
#define BirdUpdate 5
#define TubeWidth 90
#define TubeSpeed -3.5
#define BirdJump -9
#define TubeGenerateSpeed 85
#define CloudSpeed -5
用到的函数
含游戏运作需要的函数
#pragma once
#include "Object.h"
#include "GamePragma.h"
#include
#include
#include "bird.h"
#include"Tube.h"
#include
#include
#include
using namespace std;
void PlayBg(Object& bg1,Object& bg2)//播放背景
{
bg1.show_n();
bg2.setx(bg1.x() + WinWidth);
bg2.show_n();
bg1.move();
bg2.move();
if (bg1.x() == -WinWidth)bg1.setx(0);
}
void PlayCloud(vector
主函数
#include
#include
#include
#include "Object.h"
#include "function.h"
#include
#include
#include
#include"Tube.h"
#include
#include
#include
#include
#include
#pragma comment(lib,"WINMM.LIB")
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int main()
{
initgraph(800, 600);
BEGIN:
Object Background1(_T("Background"), 800, 600); //加载贴图资源及相关变量
Object Background2(_T("Background"), 800, 600);
Background1.setv(-1, 0);
Background2.setv(-1, 0);
Object pausemenu(_T("pausemenu"), _T("pausemenu_ym"),400,450,200,50);
Object overmenu(_T("overmenu"), _T("overmenu_ym"), 400, 450, 200,-500);
Object startflag(_T("startflag"), _T("startflag_ym"), 0, 0, 200, 200);
bird b;
b.add_seq(_T("bird1"), _T("bird1_ym"));
b.add_seq(_T("bird2"), _T("bird2_ym"));
b.add_seq(_T("bird3"), _T("bird3_ym"));
b.add_seq(_T("bird4"), _T("bird4_ym"));
b.add_die(_T("bird_die"), _T("bird_die_ym"));
vector tube;
vector