1.头文件、全局变量和结构体
a.玩家结构体
b.枚举玩家三种状态:奔跑、跳跃、滑行
c.障碍物结构体
d.障碍物结点
e.枚举出障碍物类型
#include<stdio.h>
#include<easyx.h>
#include<conio.h>
#include<assert.h>
#include<vector>
#include"tools.h"
#pragma comment(lib,"Winmm.lib")
using namespace std;
#define WIN_WIDTH 1012
#define WIN_HEIGHT 396
#define OBSTACLE_COUNT 10
IMAGE img_bk[3];
IMAGE people_run[12];//人物跑步图片
IMAGE people_leap[12];//人物跑步图片
IMAGE people_slide[2];//人物滑行
int bk_x[3];//背景坐标
int bk_y;
int bk_speed[3] = { 1,4,8 };//背景运动速度
int obst_count;//增加障碍物时计数
bool update;//是否立即刷新
bool switch_kb;//是否需要打开按键开关
int scored;//得分
//枚举人物状态,奔跑,跳跃,滑行
enum PEOPLE_STATE
{
run,
leap,
slide
};
//人物结构体
struct People
{
int x;
int y;
int frame_run;//跑步时帧
int motion_state;//人物运动状态
int leap_height;//跳跃高度
int frame_leap;//人物跳跃帧
int jump_offset;//人物跳跃偏移量
int frame_slide;//人物滑行帧
int slide_distance;//人物滑行距离
int exist;
}player;
//障碍物结点
struct Node
{
struct Obstacle* obstacle_t;
struct Node* next;
};
//障碍物类型枚举
enum OBSTACLE_TYPE
{
tortoise,
lion,
fire,
hock1,
hock2,
hock3,
hock4,
obstacle_type_count
};
//障碍物结构体
struct Obstacle
{
OBSTACLE_TYPE type;//障碍物类型
int x;//坐标
int y;
int frame_index;//图片帧
bool exist;//判断是否存在
int speed;//运动速度
};
vector<vector<IMAGE>>obstacleImgs;//用C++的动态数组
void LoadObstImg();
struct Obstacle* InitOb();
void PrintObImg(struct Obstacle* p_ob);
void MoveOb(struct Obstacle* p_ob);
struct Node* CreateList();
struct Node* CreateNode(Obstacle* p_ob);
void InsertNode(struct Node* p_head, struct Obstacle* p_ob);
void PrintList(struct Node* p_head);
void UpdateOb();
bool CheckCollision(struct Obstacle* p_ob);
struct Node* list = NULL;
2.制作背景和玩家
a.加载资源和数据初始化
b.背景绘制以及运动
c.玩家三种状态绘制与运动
d.键盘响应
e.判断玩家与障碍物碰撞
//加载图片
void LoadImg()
{
//加载背景
char bk_path[20] = {0};//背景图片路径
for (int i = 0; i < 3; i++)
{
sprintf(bk_path, "resource/bg%03d.png", i + 1);
loadimage(&img_bk[i], bk_path);
}
//加载背景音乐
mciSendString("open resource/bg.mp3 alias bgm", NULL, 0, NULL);
mciSendString("play bgm repeat", NULL, 0, NULL);
//加载人物
char people_path1[20] = { 0 };//人物奔跑图片路径
for (int i = 0; i < 12; i++)
{
sprintf(people_path1, "resource/hero%d.png", i + 1);
loadimage(&people_run[i], people_path1);
}
char people_path2[20] = { 0 };//人物跳跃图片路径
for (int i = 0; i < 12; i++)
{
sprintf(people_path1, "resource/g%02d.png", i + 1);
loadimage(&people_leap[i], people_path1);
}
char people_path3[20] = { 0 };//人物滑行图片路径
for (int i = 0; i < 2; i++)
{
sprintf(people_path3, "resource/d%d.png", i + 1);
loadimage(&people_slide[i], people_path3);
}
LoadObstImg();
}
//初始化数据
void InitData()
{
//加载窗口
initgraph(WIN_WIDTH, WIN_HEIGHT,1);
//背景X坐标初始为0
bk_x[3] = { 0 };
update = false;
switch_kb = true;
player.x = WIN_WIDTH*0.5 - people_run[0].getwidth()*0.5;
player.y = 360 - people_run[0].getheight();
player.frame_run = 0;
player.motion_state = run;
player.frame_leap = 0;
player.leap_height = 100;
player.jump_offset = -10;
player.frame_slide = 0;
player.slide_distance = 0;
player.exist = true;
scored = 0;
list = CreateList();//将头结点地址赋予list
for (int i = 0; i < OBSTACLE