c++魔塔制作

老廖
2021-06-18 / 33 评论 / 172 阅读 / 正在检测是否收录...

1、简要说明:
此次魔塔通过图形库进行绘制:
graphics.h
是TC的针对DOS下的一个C语言图形库,如果要用的话应该用TC的编译器来编译,VC++环境有其它的针对Windows的图形库。分为:像素函数、直线和线型函数、多边形函数、填充函数等。如果有需要在VC及VS环境中使用graphics.h的功能,可以下载 EasyX 图形库(注意:这是一个C++的图形库,如果一定要在C语言环境下使用graphics.h,您可以使用Windows GDI。)


2、具体实现
kq1mgtr0.png
实现效果展示:
kq1mhtki.png

定义窗口大小:initgraph(750, 700);
关闭窗口函数:closegraph();
绘制地图:
地图通过文件存储和读写方式实现
kq1m2sk2.png
文件的存储
`FILE *pfile= fopen("1.map","wb");

fwrite(Map, sizeof(Map), 1, pfile);
fclose(pfile);` 

文件的读写
`int mm18;

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方法的实现
kq1mya0y.png
HeroMove()方法的实现(只展示部分功能,函数方法基本一样,碰都不同的物体判断是否移动,和修改位置属性)
kq1mzlm5.png
kq1n257v.png
英雄的战斗
`
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;//防御力

};
`
`
//存储怪物的数据

CMonster monster;
FILE *pFile1 = fopen("4.monster", "wb");
fwrite(&monster, sizeof(monster), 1, pFile1);
fclose(pFile1);

`
如图所示:
kq1nf0kn.png

通过怪物类获得怪物的基本属性
`
//.cpp文件

include"Monster.h"

include<iostream>

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;

}
`
在main方法实现
kq1ngnxg.png

网盘链接:
https://pan.baidu.com/s/138rY-OFvAncn6R-jgOQPHg
提取码:wcus

1

评论 (33)

取消
  1. 头像
    Neurontine
    Windows 10 · Google Chrome

    Viagra Buen Precio

    回复
  2. 头像
    Fraurry
    Windows 10 · Google Chrome

    http://buyneurontine.com/ - Neurontine

    回复
  3. 头像
    vieviesee
    Windows 8.1 · Google Chrome

    https://prednisonebuyon.com/ - Prednisone

    回复
  4. 头像
    unfomeons
    Windows 7 · Google Chrome

    Neurontine

    回复
  5. 头像
    sefiave
    Windows 10 · Google Chrome

    buy prednisone online for dogs

    回复
  6. 头像
    Snopism
    Windows 7 · Google Chrome

    priligy where to buy

    回复
  7. 头像
    Lasix
    Windows 10 · Google Chrome

    Acquisto Levitra Bayer

    回复
  8. 头像
    Phigreeli
    Windows 10 · Google Chrome

    http://buypriligyhop.com/ - Priligy

    回复
  9. 头像
    jimdofreeKab
    Windows 7 · Google Chrome

    We spent a lot of time at her home. Maybe so her mother could keep an eye on us. Mrs. Spencer made sure to be around, offering drinks, snacks, chit chat. I noticed that she was fairly young herself. Granted at my age, anyone over 25 was old, but she was probably mid-30s, divorced. If she was a indiction of how Carley would develop, maybe I should wait. Mrs. Spencer had fuller breasts and a nice butt. She appeared to be in great shape for her "advanced" age. I knew she was keeping an eye on me as much as I was on her and her younger daughter. Her eldest, Sharon was away at college at the time. With Mrs. Spencer around we mostly limited ourselves to holding hands and sneaking in a few light kisses. One day Mrs. Spencer caught us by surprise walking in as I'd slid my hand up from Carley's stomach to rub her right breast through her shirt. She didn't really need a bra yet, so I could feel her nipple, hard, through her shirt. Just this much contact had me hard also.

    https://sites.google.com/view/8atT7IY3X5IFNRnv https://sites.google.com/view/JF9g6G6FYpL4amXi

    回复
  10. 头像
    CeaxVally
    Windows 8.1 · Google Chrome

    https://buylasixshop.com/ - lasix potassium

    回复
  11. 头像
    Prahdeeds
    Windows 8.1 · Google Chrome

    https://buyzithromaxinf.com/ - azithromycin places

    回复
  12. 头像
    Isodwig
    Windows 8.1 · Google Chrome

    https://buyplaquenilcv.com/ - buy hcq online

    回复
  13. 头像
    Bruinee
    Windows 10 · Google Chrome

    furosemide over the counter substitute

    回复
  14. 头像
    plaquenil sulfate
    Windows 10 · Google Chrome

    Viagra 100 Roma

    回复
  15. 头像
    priligy fda approval
    Windows 10 · Google Chrome

    Preisvergleich cialis generika 20mg

    回复
  16. 头像
    generic azithromycin price
    Windows 10 · Google Chrome

    Viagra 25 Mg

    回复
  17. 头像
    AgrItow
    Windows 10 · Google Chrome

    Zithromax

    回复
  18. 头像
    nestionna
    Windows 10 · Google Chrome

    Plaquenil

    回复
  19. 头像
    Punteetry
    Windows 8.1 · Google Chrome

    Stromectol

    回复
  20. 头像
    unodemn
    Windows 10 · Google Chrome

    https://buystromectolon.com/ - Stromectol

    回复
  21. 头像
    Zortabs
    Windows 10 · Google Chrome

    https://buypropeciaon.com/ - buy finasteride 5mg with amex

    回复
  22. 头像
    Viagra
    Windows 10 · Google Chrome

    Zentel Pills Nashville

    回复
  23. 头像
    FlierlY
    Windows 10 · Google Chrome

    https://buytadalafshop.com/ - Cialis

    回复
  24. 头像
    Cialis
    Windows 8.1 · Google Chrome

    Tadalis Sx En Ligne

    回复
  25. 头像
    Chusiabuh
    Windows 10 · Google Chrome

    https://buysildenshop.com/ - Viagra

    回复
  26. 头像
    how effective is propecia
    Windows 10 · Google Chrome

    Walking Pneumonia Zithromax

    回复
  27. 头像
    Stromectol
    Windows 8.1 · Google Chrome

    Found A Chewed Up Amoxicillin Pill

    回复
  28. 头像
    Edipuct
    Windows 7 · Google Chrome

    Propecia

    回复
  29. 头像
    AlCorruff
    Windows 10 · Google Chrome

    Viagra

    回复
  30. 头像
    admique
    Windows 8.1 · Google Chrome

    Cialis

    回复
  31. 头像
    bpidpimsw
    Windows XP · Internet Explore

    c 魔塔制作 - 老廖&&Games
    [url=http://www.g26dj016dn15n74wzw2avt0307j7ebs3s.org/]ubpidpimsw[/url]
    bpidpimsw http://www.g26dj016dn15n74wzw2avt0307j7ebs3s.org/
    abpidpimsw

    回复
  32. 头像
    mfjpobcqke
    Windows XP · Internet Explore

    c 魔塔制作 - 老廖&&Games
    mfjpobcqke http://www.gont096e1peo14c5y08bzh46032l78mis.org/
    [url=http://www.gont096e1peo14c5y08bzh46032l78mis.org/]umfjpobcqke[/url]
    amfjpobcqke

    回复
  33. 头像
    ecznoxslnc
    Windows XP · Internet Explore

    c 魔塔制作 - 老廖&&Games
    [url=http://www.g2neb8z95skxt9i72y0791c39u720ebws.org/]uecznoxslnc[/url]
    aecznoxslnc
    ecznoxslnc http://www.g2neb8z95skxt9i72y0791c39u720ebws.org/

    回复