此次魔塔通过图形库进行绘制:
graphics.h
是TC的针对DOS下的一个C语言图形库,如果要用的话应该用TC的编译器来编译,VC++环境有其它的针对Windows的图形库。分为:像素函数、直线和线型函数、多边形函数、填充函数等。如果有需要在VC及VS环境中使用graphics.h的功能,可以下载 EasyX 图形库(注意:这是一个C++的图形库,如果一定要在C语言环境下使用graphics.h,您可以使用Windows GDI。)
定义窗口大小:initgraph(750, 700);
关闭窗口函数:closegraph();
绘制地图:
地图通过文件存储和读写方式实现
文件的存储
FILE *pfile= fopen("1.map","wb");
fwrite(Map, sizeof(Map), 1, pfile);
fclose(pfile);
文件的读写
int mm[18][18];
FILE*pfile = fopen("1.map", "rb");
fread(mm, sizeof(mm), 1, pfile);
fclose(pfile);
for (int i = 0; i < 18; i++)
{
for (int j = 0; j < 18; j++)
{
cout << mm[i][j];
}
cout << endl;
}
初始化函数读取地图文件
void CGameMap::InitMap(char *path)
{
loadimage(&img, "res/img.jpg");
//读取地图数据中的文件
FILE *pFile = fopen(path, "rb");
fread(m_Map, sizeof(m_Map), 1, pFile);
fclose(pFile);
}
通过自定义画图方法,显示地图
void CGameMap::DrawMap(CHero*hero)
{
// loadimage(&img, L"res/img.jpg");
for (int i = 0; i < 18; i++)
{
for (int j = 0; j < 18; j++)
{
switch (m_Map[i][j])
{
case 0:背景墙
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 8, 32 * 16);
break;
case 1:///阻隔墙
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 0, 32 * 16);
break;
case 2:///上台阶
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 3, 32 * 16);
break;
case 3:///下台阶
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 2, 32 * 16);
break;
case 4:
///怪物蓝小球
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 1, 32 * 0);
break;
case 5:
怪物红小球
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 2, 32 * 0);
break;
case 6:
怪物蝙蝠
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 6, 32 * 0);
break;
case 7:/僵尸
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 0, 32 * 1);
break;
case 9:///英雄
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 6, 32 * 6);
break;
case 14:红钥匙
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 5, 32 * 14);
break;
case 15:蓝钥匙
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 4, 32 * 14);
break;
case 16:黄钥匙
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 3, 32 * 14);
break;
case 24://红药水
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 3, 32 * 13);
break;
case 25://蓝药水
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 4, 32 * 13);
break;
case 26:///黄药水
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 5, 32 * 13);
break;
case 27:///刀
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 6, 32 * 12);
break;
case 28:///盾
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 6, 32 * 13);
break;
case 34:///红色的门
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 8, 32 * 15);
break;
case 35:///蓝色的门
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 7, 32 * 15);
break;
case 36:///黄色的门
putimage(32 * i, 32 * j, 32, 32, &img, 32 * 6, 32 * 15);
break;
default:
break;
}
}
}
//char *m = hero->intToWchar(hero->GetBlueKey());
///字符串转换宽字符
/*wchar_t *wide = new wchar_t[10];
int len = MultiByteToWideChar(0, 0, hp, -1, NULL, 0);
MultiByteToWideChar(0, 0, hp, -1, wide, len);
*/
//char *e = hero->intToWchar(hero->GetExp());
outtextxy(32 * 19, 32, "名字:");
outtextxy(32 * 22, 32, "Bob");
outtextxy(19 * 32, 32 * 2, "血量:");
outtextxy(32 * 22, 32 * 2, hero->intToWchar(hero->GetHp()));
// *hp = hero->intToWchar(hero->GetHp());
//outtextxy(32 * 23, 32 * 2, hp);
outtextxy(32 * 19, 32 * 3, "等级:");
outtextxy(32*22, 32 * 3, hero->intToWchar(hero->GetLv()));
outtextxy(32 * 19, 32 * 4, "经验值:");
outtextxy(32 * 22, 32 * 4, hero->intToWchar(hero->GetExp()));
outtextxy(32 * 19, 32 * 5, "攻击力:");
outtextxy(32 * 22, 32 * 5, hero->intToWchar(hero->GetAtt()));
outtextxy(32 * 19, 32 * 6, "防御力:");
outtextxy(32 * 22, 32 * 6,hero->intToWchar(hero->GetDef()));
//hp = hero->intToWchar(hero->GetBlueKey());
outtextxy(18 * 32 + 32, 32 * 7, "蓝钥匙:");
outtextxy(32 * 22, 32 * 7, hero->intToWchar(hero->GetBlueKey()));
outtextxy(18 * 32 + 32, 32 * 8, "红钥匙:");
outtextxy(32 * 22, 32 * 8, hero->intToWchar(hero->GetRedKey()));
outtextxy(18 * 32 + 32, 32 * 9, "红钥匙:");
outtextxy(32 * 22, 32 * 9, hero->intToWchar(hero->GetYellowKey()));
}
第二步:英雄属性,功能的实现
在.h文件中定义英雄的基本属性和实现功能的方法
#include"GameMap.h"
class CHero
{
public:
//初始化英雄属性///
void HeroInit(char name[20], CGameMap &gamemap);
//英雄的移动和判断下一位置进而实现不同的功能//
void HeroMove(CGameMap& gamemap);
//英雄的战斗//
bool HeroFight(char *path);
类型转换
char *intToWchar(int xNum);
char* GetName()
{
return m_Name;
}
int GetRedKey(){
return m_RedKey;
}
int GetBlueKey()
{
return m_BlueKey;
}
int GetYellowKey()
{
return m_YellowKey;
}
int GetHp()
{
return m_Hp;
}
int GetLv()
{
return m_Lv;
}
int GetExp()
{
return m_Exp;
}
int GetAtt()
{
return m_Att;
}
int GetDef()
{
return m_Def;
}
private:
char m_Name[20];
unsigned int m_Lv;
int m_RedKey;
int m_YellowKey;
int m_BlueKey;
int m_MaxHp;
int m_Hp;
int m_Exp;
int m_Att;
int m_Def;
int m_PosX;
int m_PosY;
};
HeroInit方法的实现
HeroMove()方法的实现(只展示部分功能,函数方法基本一样,碰都不同的物体判断是否移动,和修改位置属性)
英雄的战斗
bool CHero::HeroFight(char *path)
{
CMonster redmonster(path);
//这个指针指向那个new的对象啊
while (true)
{
outtextxy(32 * 1, 32 * 19, m_Name);
outtextxy(32 * 2, 32 * 19, "PK");
outtextxy(32 * 3, 32 * 19, redmonster.GetName());
//outtextxy(32 * 22, 32 * 8, redmonster.intToWchar(redmonster.GetDef()));
//outtextxy(32 * 22 + 32 * 15, redmonster.intToWchar(redmonster.GetDef()));
//outtextxy(32 * 19, 32 * 8, "Bog攻击了:" + redmonster.intToWchar(redmonster.GetDef()));
//cout << m_Name << "攻击了" << monster.GetName() << "," << monster.GetName() << "失去了" << m_Att - monster.GetDef() << endl;
英雄攻击
if (redmonster.GetHp() - (m_Att - redmonster.GetDef()) <= 0)
{
m_Exp += redmonster.GetExp();
redmonster.SetHp(0);
outtextxy(32 * 1, 32 * 20, "怪物死亡:");
Sleep(500);
outtextxy(32 * 1, 32 * 20, " ");
system("CLS");
return true;
}
else
{
redmonster.SetHp(redmonster.GetHp() - (m_Att - redmonster.GetDef()));
outtextxy(32 * 1, 32 * 20, "怪物被攻击:");
Sleep(500);
outtextxy(32 * 1, 32 * 20, " ");
}
/怪物攻击/
if (m_Hp - (redmonster.GetAtt() - m_Def) <= 0)
{
m_Hp = 0;
outtextxy(32 * 1, 32 * 20, "英雄死亡:");
Sleep(500);
outtextxy(32 * 1, 32 *20, " ");
return false;
}
else
{
m_Hp -= redmonster.GetAtt() - m_Def;
outtextxy(32 * 1, 32 * 20, "英雄被攻击丢失血量");
outtextxy(32 * 22, 32 * 2, " ");
//char *hp = intToWchar(redmonster.GetAtt() - m_Def);
outtextxy(32 * 9, 32 * 20, intToWchar(redmonster.GetAtt() - m_Def));
Sleep(1000);
outtextxy(32 * 1, 32 * 20, " ");
}
}
return true;
}
sprintf方法现实整型转换成字符型
类型转换方法
char* CHero::intToWchar(int xNum)
{
static char str[20] = "";
//转换成字符型
sprintf(str, "%d", xNum);
/*
//转换成宽字符型
static wchar_t *wide = new wchar_t[10];
int len = MultiByteToWideChar(0, 0, str, -1, NULL, 0);
MultiByteToWideChar(0, 0, str, -1, wide, len);
return wide;*/
return str;
}
`
`
怪物也是通过文件读取方式,获取
//自定义类
class CMonster
{
public:
CMonster()
{
strcpy(m_Name, "绿小球");
m_Exp = 15;
m_Hp = 20;
m_Att = 10;
m_Def = 5;
}
private:
char m_Name[20];//名字
int m_Exp;//经验值
int m_Hp;//当前血量
int m_Att;//攻击力
int m_Def;//防御力
};
//main方法实现
//存储怪物的数据
CMonster monster;
FILE *pFile1 = fopen("4.monster", "wb");
fwrite(&monster, sizeof(monster), 1, pFile1);
fclose(pFile1);
通过怪物类获得怪物的基本属性
//.cpp文件
#include"Monster.h"
#include
using namespace std;
CMonster::CMonster(char* path)
{
FILE *pFile = fopen(path, "rb");
fread(this, sizeof(CMonster), 1, pFile);
fclose(pFile);
}
CMonster::~CMonster()
{
}
void CMonster::SetHp(int hp)
{
m_Hp = hp;
}
char* CMonster::intToWchar(int xNum)
{
static char str[20] = "";
sprintf(str, "%d", xNum);
/*
static wchar_t *wide = new wchar_t[10];
int len = MultiByteToWideChar(0, 0, str, -1, NULL, 0);
MultiByteToWideChar(0, 0, str, -1, wide, len);*/
/*return wide;*/
return str;
}
源码链接:
链接:https://pan.baidu.com/s/138rY-OFvAncn6R-jgOQPHg
提取码:wcus