• C/C++游戏开发(easyx框架)回合制——魔塔


    本游戏用到了图形界面库graphics.h,图形界面库下载安装:
    下载链接:https://easyx.cn/download
    游戏截图:
    请添加图片描述
    首先是玩家的定义,使用结构体,这个名字是可以自己改变的

    struct gamerole{
    	char name[20];		//玩家名字
    	int HP;				//血量
    	int MP;				//魔法
    	int DEF;			//防御
    	int ATT;			//攻击
    	int Lv;				//等级
    	int Exp;			//经验
    	int Num_Blue_Key;	//蓝钥匙数量
    	int Num_Yellow_Key;	//黄钥匙数量
    	int x, y;			//坐标
    }player;
    int exp_need = 100;	//升级所需经验
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    怪物属性的定义

    struct monster{
    	char name[20];		//怪物名字
    	int HP;				//血量
    	int ATT;			//攻击
    	int DEF;			//防御
    	int Exp;			//经验
    }monsters[15];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    初始化玩家属性

    void initPlayer(){
    	//输入用户名
    	printf("Input role name: ");
    	scanf("%s", player.name);
    
    	//初始化游戏角色信息
    	player.Lv = 0;
    	player.ATT = 50;
    	player.DEF = 50;
    	player.Num_Blue_Key = 0;
    	player.Num_Yellow_Key = 0;
    	player.HP = 500;
    	player.MP = 250;
    	player.Exp = 0;
    	player.x = -1;
    	player.y = -1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    初始化怪物属性

    void initMonsters() {
    	monsters[2] = { "绿史莱姆", 50, 10, 12, 100 };	//绿史莱姆属性
    	monsters[3] = { "红史莱姆", 100, 50, 12, 500 };	//红史莱姆属性
    	monsters[11] = { "小蝙蝠", 20, 10, 9, 50 };		//小蝙蝠属性
    	monsters[12] = { "小巫师", 100, 30, 9, 400 };	//小巫师属性
    	monsters[13] = { "小骷髅", 30, 20, 10, 200 };	//小骷髅属性
    	monsters[14] = { "大骷髅", 60, 50, 25, 300 };	//大骷髅属性
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    加载游戏资源图片
    右键打开项目属性,添加 -> 现有项,添加资源图片进项目·。
    资源图片链接:https://pan.baidu.com/s/1NZN7yDFAzBbHDHrxBiWEJA?pwd=c685
    提取码:c685
    在这里插入图片描述

    IMAGE img[19], playerInfoBox;	//素材图片
    void initgamePicture(){
    	//加载游戏角色信息框
    	loadimage(&playerInfoBox, "info.jpg");
    
    	//加载地图资源
    	//60 * 60 固定尺寸
    	loadimage(&img[0], "墙.jpg", 60, 60);
    	loadimage(&img[1], "地板.jpg", 60, 60);
    	loadimage(&img[2], "绿史莱姆.jpg", 60, 60);
    	loadimage(&img[3], "红史莱姆.jpg", 60, 60);
    	loadimage(&img[4], "蓝水晶.jpg", 60, 60);
    	loadimage(&img[5], "红水晶.jpg", 60, 60);
    	loadimage(&img[6], "蓝钥匙.jpg", 60, 60);
    	loadimage(&img[7], "黄钥匙.jpg", 60, 60);
    	loadimage(&img[8], "小红药水.jpg", 60, 60);
    	loadimage(&img[9], "小蓝药水.jpg", 60, 60);
    	loadimage(&img[10], "梯子.jpg", 60, 60);
    	loadimage(&img[11], "小蝙蝠.jpg", 60, 60);
    	loadimage(&img[12], "小巫师.jpg", 60, 60);
    	loadimage(&img[13], "骷髅兵.jpg", 60, 60);
    	loadimage(&img[14], "大骷髅兵.jpg", 60, 60);
    	loadimage(&img[15], "蓝门.jpg", 60, 60);
    	loadimage(&img[16], "黄门.jpg", 60, 60);
    	loadimage(&img[17], "人.jpg", 60, 60);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    设置地图,用数字来决定地图的每一个方块是什么类型(与img数组的索引对应)。

    int map[13][13] = {
    	0,0,0,0,0,0,0,0,0,0,0,0,0,
    	0,10,6,2,3,2,1,1,1,1,1,1,0,
    	0,0,0,0,0,0,0,0,0,0,0,1,0,
    	0,8,1,1,15,1,0,0,1,1,1,1,0,
    	0,1,14,1,0,1,0,0,5,0,0,1,0,
    	0,0,15,0,0,1,15,11,12,11,0,1,0,
    	0,6,1,1,0,1,0,0,0,0,0,1,0,
    	0,1,14,1,0,1,0,0,0,0,0,1,0,
    	0,0,15,0,0,1,1,0,0,0,0,1,0,
    	0,1,1,1,0,0,15,0,0,0,0,15,0,
    	0,8,1,9,0,6,1,6,0,6,11,1,0,
    	0,8,1,9,0,1,17,1,0,2,6,2,0,
    	0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    屏幕的笛卡尔坐标如下:
    在这里插入图片描述
    所以地图的二维坐标是(其中的一个点是 map [ y ] [ x ] ):
    在这里插入图片描述

    接下来就是定义画布,然后加载图片,我用一个二维数组存下了地图,不同的数字代表不同的图片,然后根据二维数组的值把不同的地方贴上不同的图片。

    void SetMap(int x, int y){
    	for (int i = 0; i < y; i++){
    		for (int j = 0; j < x; j++){
    			putimage(j * 60, i * 60, &img[map[i][j]]);
    			if (map[i][j] == 17) {
    				player.x = j;
    				player.y = i;
    			}
    		}
    	}
    	//地图没有角色
    	if (player.x == -1) {
    		MessageBox(hwnd, "地图没找到角色", "错误", MB_ICONWARNING);
    		closegraph();
    		exit(-1);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在地图右边显示角色信息

    void SetPlayer(){
    	static char temp[16];
    	//打印游戏角色信息框
    	putimage(13 * 60, 0, &playerInfoBox);
    	setbkmode(TRANSPARENT);	//透明背景
    	outtextxy(60 * 13 + 12, 100, player.name);
    	outtextxy(60 * 13 + 12, 180, _itoa(player.Lv, temp, 10));
    	outtextxy(60 * 13 + 12, 235, _itoa(player.Exp, temp, 10));
    	outtextxy(60 * 13 + 12, 362, _itoa(player.HP, temp, 10));
    	outtextxy(60 * 13 + 12, 420, _itoa(player.MP, temp, 10));
    	outtextxy(60 * 13 + 12, 517, _itoa(player.ATT, temp, 10));
    	outtextxy(60 * 13 + 12, 567, _itoa(player.DEF, temp, 10));
    	outtextxy(60 * 13 + 12, 689, _itoa(player.Num_Yellow_Key, temp, 10));
    	outtextxy(60 * 13 + 12, 759, _itoa(player.Num_Blue_Key, temp, 10));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述
    接下来就是人物的移动和战斗了,人物的移动我就是直接对上下左右四种情况分别讨论。

    void playGame() {
    	static char dir;
    	dir = _getch();
    	switch (dir) {
    		//左
    		case 'a':
    		case 'A':
    		case 75:
    			step(player.x - 1, player.y);
    			break;
    		//右
    		case 'd':
    		case 'D':
    		case 77:
    			step(player.x + 1, player.y);
    			break;
    		//上
    		case 'w':
    		case 'W':
    		case 72:
    			step(player.x, player.y - 1);
    			break;
    		//下
    		case 's':
    		case 'S':
    		case 80:
    			step(player.x, player.y + 1);
    			break;
    		default:
    			break;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在人物走动的时候要判断能不能走,不能走就不处理,如果能走,就把走到的那个位置上变成人,把之前人的位置变成地板。
    如果是钥匙,把对应的钥匙数量加1。
    如果是门,判断一下对应颜色的钥匙是否足够,如果足够,钥匙数量减1,然后把对应位置上的门变为空地。
    如果是药水,吃了之后会增加生命。
    如果是水晶,根据水晶的颜色加对应的属性。
    如果是楼梯,就游戏胜利。
    当遇到怪物的时候回产生战斗。

    void step(int dstx, int dsty) {
    	if (map[dsty][dstx] == 1) {			//下一步是地板
    	}
    	else if (map[dsty][dstx] == 15) {   //下一步是蓝门
    		if (player.Num_Blue_Key <= 0) {
    			return;
    		}
    		player.Num_Blue_Key--;
    	}
    	else if (map[dsty][dstx] == 10) {   //下一步是蓝门
    		MessageBox(hwnd, "游戏结束了", "通关", MB_OK);
    		closegraph();
    		exit(0);
    	}
    	else if (map[dsty][dstx] == 16) {   //下一步是黄门
    		if (player.Num_Yellow_Key <= 0) {
    			return;
    		}
    		player.Num_Yellow_Key--;
    	}
    	else if (map[dsty][dstx] == 4) {   //下一步是蓝水晶
    		player.DEF += 2;
    	}
    	else if (map[dsty][dstx] == 5) {   //下一步是红水晶
    		player.ATT += 2;
    	}
    	else if (map[dsty][dstx] == 6) {   //下一步是蓝钥匙
    		player.Num_Blue_Key++;
    	}
    	else if (map[dsty][dstx] == 7) {   //下一步是黄钥匙
    		player.Num_Yellow_Key++;
    	}
    	else if (map[dsty][dstx] == 8) {   //下一步是红药水
    		player.HP += 50;
    	}
    	else if (map[dsty][dstx] == 9) {   //下一步是蓝药水
    		player.MP += 50;
    	}
    	else if (map[dsty][dstx] != 0) {   //下一步是怪物
    		if (!Combat(map[dsty][dstx])) {//不打怪物
    			return;
    		}
    	}
    	else {								//其他情况
    		return;
    	}
    	//移动
    	drawMapCell(player.x, player.y, 1);
    	drawMapCell(dstx, dsty, 17);
    	player.x = dstx;
    	player.y = dsty;
    }
    
    void drawMapCell(int x, int y, int type) {
    	putimage(x * 60, y * 60, &img[type]);
    	map[y][x] = type;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    Combat函数,判断玩家和那个怪物进行战斗。
    VS函数,用于计算战斗是否成功,如果成功,会加相应的属性,如果失败,则会弹出打不过的窗口。
    在这里插入图片描述

    bool Combat(int monsterType){
    	int ID;
    	char msg[36];
    	sprintf(msg, "遇到%s,是否攻击?", monsters[monsterType].name);
    	ID = MessageBox(hwnd, msg, "战斗阶段", MB_YESNO);
    	if (ID == IDYES) {
    		if (VS(player.HP, player.ATT, player.DEF, monsters[monsterType].HP, monsters[monsterType].ATT, monsters[monsterType].DEF)) {	//胜利
    			//获得经验值
    			player.Exp += monsters[monsterType].Exp;
    			while (player.Exp >= exp_need) {	//升级
    				player.Lv++;
    				player.Exp -= exp_need;
    				exp_need *= player.Lv;
    			}
    			return true;
    		}
    		else {	//失败
    			MessageBox(hwnd, "你死了", "打不过", MB_ICONWARNING);
    			return false;
    		}
    	}
    	return false;
    }
    
    bool VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF){
    	while (playHP > 0 || monHP > 0){
    		monHP -= playATT < monDEF ? 0 : playATT - monDEF;
    		if (monHP < 0)
    			break;
    		playHP -= monATT < playDEF ? 0 : monATT - playDEF;
    	}
    	if (playHP > 0) {
    		player.HP = playHP;
    		return true;
    	}
    	return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    游戏流程

    int main() {
    	initPlayer();
    	initMonsters();
    	initgamePicture();
    	initgraph(60 * 14, 60 * 13);
    	putimage(0, 0, &playerInfoBox);
    	SetMap(13, 13);
    	SetPlayer();
    	while (player.HP > 0) {
    		playGame();
    		SetPlayer();
    	}
    	closegraph();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    完整的代码

    #define _CRT_SECURE_NO_WARNINGS	//去掉scanf_s问题
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <graphics.h>
    using namespace std;
    
    
    void initgamePicture();							//加载游戏图片
    void SetPlayer();								//显示角色信息
    void initPlayer();								//初始化游戏角色
    void initMonsters();							//初始化游戏怪物
    void SetMap(int x, int y);						//加载游戏地图
    void playGame();								//开始游戏
    void step(int dstx, int dsty);					//移动
    void drawMapCell(int x, int y, int type);		//画图
    bool VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF);	//打架
    bool Combat(int monstersTpye);					//战斗阶段
    
    //地图二维数组
    int map[13][13] = {
    	0,0,0,0,0,0,0,0,0,0,0,0,0,
    	0,10,6,2,3,2,1,1,1,1,1,1,0,
    	0,0,0,0,0,0,0,0,0,0,0,1,0,
    	0,8,1,1,15,1,0,0,1,1,1,1,0,
    	0,1,14,1,0,1,0,0,5,0,0,1,0,
    	0,0,15,0,0,1,15,11,12,11,0,1,0,
    	0,6,1,1,0,1,0,0,0,0,0,1,0,
    	0,1,14,1,0,1,0,0,0,0,0,1,0,
    	0,0,15,0,0,1,1,0,0,0,0,1,0,
    	0,1,1,1,0,0,15,0,0,0,0,15,0,
    	0,8,1,9,0,6,1,6,0,6,11,1,0,
    	0,8,1,9,0,1,17,1,0,2,6,2,0,
    	0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    
    struct gamerole{
    	char name[20];		//玩家名字
    	int HP;				//血量
    	int MP;				//魔法
    	int DEF;			//防御
    	int ATT;			//攻击
    	int Lv;				//等级
    	int Exp;			//经验
    	int Num_Blue_Key;	//蓝钥匙数量
    	int Num_Yellow_Key;	//黄钥匙数量
    	int x, y;			//坐标
    }player;
    
    struct monster{
    	char name[20];		//怪物名字
    	int HP;				//血量
    	int ATT;			//攻击
    	int DEF;			//防御
    	int Exp;			//经验
    }monsters[15];
    
    IMAGE img[19], playerInfoBox;	//素材图片
    
    HWND hwnd;	//弹窗窗口句柄
    
    int exp_need = 100;	//升级所需经验
    
    int main() {
    	initPlayer();
    	initMonsters();
    	initgamePicture();
    	initgraph(60 * 14, 60 * 13);
    	putimage(0, 0, &playerInfoBox);
    	SetMap(13, 13);
    	SetPlayer();
    	while (player.HP > 0) {
    		playGame();
    		SetPlayer();
    	}
    	closegraph();
    	return 0;
    }
    
    /*
    * 输入用户名,初始化游戏角色信息
    */
    void initPlayer(){
    	//输入用户名
    	printf("Input role name: ");
    	scanf("%s", player.name);
    
    	//初始化游戏角色信息
    	player.Lv = 0;
    	player.ATT = 50;
    	player.DEF = 50;
    	player.Num_Blue_Key = 0;
    	player.Num_Yellow_Key = 0;
    	player.HP = 500;
    	player.MP = 250;
    	player.Exp = 0;
    	player.x = -1;
    	player.y = -1;
    }
    
    /*
    * 初始化游戏怪物
    */
    void initMonsters() {
    	monsters[2] = { "绿史莱姆", 50, 10, 12, 100 };	//绿史莱姆属性
    	monsters[3] = { "红史莱姆", 100, 50, 12, 500 };	//红史莱姆属性
    	monsters[11] = { "小蝙蝠", 20, 10, 9, 50 };		//小蝙蝠属性
    	monsters[12] = { "小巫师", 100, 30, 9, 400 };	//小巫师属性
    	monsters[13] = { "小骷髅", 30, 20, 10, 200 };	//小骷髅属性
    	monsters[14] = { "大骷髅", 60, 50, 25, 300 };	//大骷髅属性
    }
    
    /*
    *  加载游戏资源图片
    */
    void initgamePicture(){
    	//加载游戏角色信息框
    	loadimage(&playerInfoBox, "info.jpg");
    
    	//加载地图资源
    	//60 * 60 固定尺寸
    	loadimage(&img[0], "墙.jpg", 60, 60);
    	loadimage(&img[1], "地板.jpg", 60, 60);
    	loadimage(&img[2], "绿史莱姆.jpg", 60, 60);
    	loadimage(&img[3], "红史莱姆.jpg", 60, 60);
    	loadimage(&img[4], "蓝水晶.jpg", 60, 60);
    	loadimage(&img[5], "红水晶.jpg", 60, 60);
    	loadimage(&img[6], "蓝钥匙.jpg", 60, 60);
    	loadimage(&img[7], "黄钥匙.jpg", 60, 60);
    	loadimage(&img[8], "小红药水.jpg", 60, 60);
    	loadimage(&img[9], "小蓝药水.jpg", 60, 60);
    	loadimage(&img[10], "梯子.jpg", 60, 60);
    	loadimage(&img[11], "小蝙蝠.jpg", 60, 60);
    	loadimage(&img[12], "小巫师.jpg", 60, 60);
    	loadimage(&img[13], "骷髅兵.jpg", 60, 60);
    	loadimage(&img[14], "大骷髅兵.jpg", 60, 60);
    	loadimage(&img[15], "蓝门.jpg", 60, 60);
    	loadimage(&img[16], "黄门.jpg", 60, 60);
    	loadimage(&img[17], "人.jpg", 60, 60);
    }
    
    /*
    *打印游戏地图
    */
    void SetMap(int x, int y){
    	for (int i = 0; i < y; i++){
    		for (int j = 0; j < x; j++){
    			putimage(j * 60, i * 60, &img[map[i][j]]);
    			if (map[i][j] == 17) {
    				player.x = j;
    				player.y = i;
    			}
    		}
    	}
    	//地图没有角色
    	if (player.x == -1) {
    		MessageBox(hwnd, "地图没找到角色", "错误", MB_ICONWARNING);
    		closegraph();
    		exit(-1);
    	}
    }
    
    /*
    *显示角色信息
    */
    void SetPlayer(){
    	static char temp[16];
    	//打印游戏角色信息框
    	putimage(13 * 60, 0, &playerInfoBox);
    	setbkmode(TRANSPARENT);
    	outtextxy(60 * 13 + 12, 100, player.name);
    	outtextxy(60 * 13 + 12, 180, _itoa(player.Lv, temp, 10));
    	outtextxy(60 * 13 + 12, 235, _itoa(player.Exp, temp, 10));
    	outtextxy(60 * 13 + 12, 362, _itoa(player.HP, temp, 10));
    	outtextxy(60 * 13 + 12, 420, _itoa(player.MP, temp, 10));
    	outtextxy(60 * 13 + 12, 517, _itoa(player.ATT, temp, 10));
    	outtextxy(60 * 13 + 12, 567, _itoa(player.DEF, temp, 10));
    	outtextxy(60 * 13 + 12, 689, _itoa(player.Num_Yellow_Key, temp, 10));
    	outtextxy(60 * 13 + 12, 759, _itoa(player.Num_Blue_Key, temp, 10));
    }
    
    void playGame() {
    	static char dir;
    	dir = _getch();
    	switch (dir) {
    		//左
    		case 'a':
    		case 'A':
    		case 75:
    			step(player.x - 1, player.y);
    			break;
    		//右
    		case 'd':
    		case 'D':
    		case 77:
    			step(player.x + 1, player.y);
    			break;
    		//上
    		case 'w':
    		case 'W':
    		case 72:
    			step(player.x, player.y - 1);
    			break;
    		//下
    		case 's':
    		case 'S':
    		case 80:
    			step(player.x, player.y + 1);
    			break;
    		default:
    			break;
    	}
    }
    
    void step(int dstx, int dsty) {
    	if (map[dsty][dstx] == 1) {			//下一步是地板
    	}
    	else if (map[dsty][dstx] == 15) {   //下一步是蓝门
    		if (player.Num_Blue_Key <= 0) {
    			return;
    		}
    		player.Num_Blue_Key--;
    	}
    	else if (map[dsty][dstx] == 10) {   //下一步是蓝门
    		MessageBox(hwnd, "游戏结束了", "通关", MB_OK);
    		closegraph();
    		exit(0);
    	}
    	else if (map[dsty][dstx] == 16) {   //下一步是黄门
    		if (player.Num_Yellow_Key <= 0) {
    			return;
    		}
    		player.Num_Yellow_Key--;
    	}
    	else if (map[dsty][dstx] == 4) {   //下一步是蓝水晶
    		player.DEF += 2;
    	}
    	else if (map[dsty][dstx] == 5) {   //下一步是红水晶
    		player.ATT += 2;
    	}
    	else if (map[dsty][dstx] == 6) {   //下一步是蓝钥匙
    		player.Num_Blue_Key++;
    	}
    	else if (map[dsty][dstx] == 7) {   //下一步是黄钥匙
    		player.Num_Yellow_Key++;
    	}
    	else if (map[dsty][dstx] == 8) {   //下一步是红药水
    		player.HP += 50;
    	}
    	else if (map[dsty][dstx] == 9) {   //下一步是蓝药水
    		player.MP += 50;
    	}
    	else if (map[dsty][dstx] != 0) {   //下一步是怪物
    		if (!Combat(map[dsty][dstx])) {//不打怪物
    			return;
    		}
    	}
    	else {								//其他情况
    		return;
    	}
    	//移动
    	drawMapCell(player.x, player.y, 1);
    	drawMapCell(dstx, dsty, 17);
    	player.x = dstx;
    	player.y = dsty;
    }
    
    void drawMapCell(int x, int y, int type) {
    	putimage(x * 60, y * 60, &img[type]);
    	map[y][x] = type;
    }
    
    bool Combat(int monsterType){
    	int ID;
    	char msg[36];
    	sprintf(msg, "遇到%s,是否攻击?", monsters[monsterType].name);
    	ID = MessageBox(hwnd, msg, "战斗阶段", MB_YESNO);
    	if (ID == IDYES) {
    		if (VS(player.HP, player.ATT, player.DEF, monsters[monsterType].HP, monsters[monsterType].ATT, monsters[monsterType].DEF)) {	//胜利
    			//获得经验值
    			player.Exp += monsters[monsterType].Exp;
    			while (player.Exp >= exp_need) {	//升级
    				player.Lv++;
    				player.Exp -= exp_need;
    				exp_need *= player.Lv;
    			}
    			return true;
    		}
    		else {	//失败
    			MessageBox(hwnd, "你死了", "打不过", MB_ICONWARNING);
    			return false;
    		}
    	}
    	return false;
    }
    
    bool VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF){
    	while (playHP > 0 || monHP > 0){
    		monHP -= playATT < monDEF ? 0 : playATT - monDEF;
    		if (monHP < 0)
    			break;
    		playHP -= monATT < playDEF ? 0 : monATT - playDEF;
    	}
    	if (playHP > 0) {
    		player.HP = playHP;
    		return true;
    	}
    	return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
  • 相关阅读:
    workmanager导入android studio
    Linux基础命令4——Linux快捷键与帮助命令
    阿里云starrocks监控告发至钉钉群
    excel将文件夹下面的表格文件指定名称的sheet批量导出到指定文件中,并按照文件名保存在新文件的不同sheet中
    Tensorflow的GPU分配方法
    EDCircles: A real-time circle detector with a false detection control 翻译
    服务和协议的关系?
    SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.1 缓存的作用
    HackTheBox---Starting Point-- Tier 0---Meow
    模拟shell小程序
  • 原文地址:https://blog.csdn.net/ymxyld/article/details/124971192