C语言魔塔游戏的实现代码

很早就很想写这个,今天终于写完了。

游戏截图:

编译环境: VS2017

游戏需要一些图片,如果有想要的或者对游戏有什么看法的可以加我的QQ 2985486630 讨论,如果暂时没有回应,可以在博客下方留言,到时候我会看到。觉得麻烦的直接下载C-mota_jb51.rar

解压后点击sln文件直接可以运行

下面我来介绍一下游戏的主要功能和实现方式

首先是玩家的定义,使用结构体,这个名字是可以自己改变的

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;
}player;

在游戏的右边显示任务的各项属性

函数:

void SetPlayer()
{
	putimage(60 * 13, 0, &Message);
	outtextxy(60 * 13 + 12, 100, player.name);
	outtextxy(60 * 13 + 12, 180, intToString(player.Lv));
	outtextxy(60 * 13 + 12, 235, intToString(player.Exp));
	outtextxy(60 * 13 + 12, 362, intToString(player.HP));
	outtextxy(60 * 13 + 12, 425, intToString(player.MP));
	outtextxy(60 * 13 + 12, 517, intToString(player.ATT));
	outtextxy(60 * 13 + 12, 567, intToString(player.DEF));
	outtextxy(60 * 13 + 12, 689, intToString(player.Num_Yellow_Key));
	outtextxy(60 * 13 + 12, 759, intToString(player.Num_Blue_Key));
}

由于这个函数要求属性必须是字符串,所以我写了一个把数字转化成字符串的函数

//整数转换为字符
char *intToString(int Number)
{
	int len = 0;

	if (Number == 0) {
		str[0] = '0';
		len++;
	}
	while (Number)
	{
		str[len++] = Number % 10+'0';
		Number /= 10;
	}
	for (int i = 0; i < len/2; i++) {
		char t = str[i];
		str[i] = str[len - i - 1];
		str[len - i - 1] = t;
	}
	str[len] = '\0';
	return str;
}

怪物属性的定义

struct monster
{
	int HP;	  //血量
	int ATT;  //攻击
	int DEF;	//防御
	int Exp;  //经验
};

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

void SetMap()
{
	for (int i = 0; i < 13; i++)
	{
		for (int j = 0; j < 13; j++)
		{
			switch (map[i][j])
			{

			case 0:
				putimage(j * 60, i * 60, &Wall);     //墙
				break;
			case 1:
				putimage(j * 60, i * 60, &Ground);    //地板
				break;
			case 2:
				putimage(j * 60, i * 60, &Blue_door);   //蓝门
				break;
			case 3:
				putimage(j * 60, i * 60, &Yellow_door);  //黄门
				break;
			case 4:
				putimage(j * 60, i * 60, &Blue_Cry);     //蓝水晶
				break;
			case 5:
				putimage(j * 60, i * 60, &Red_Cry);     //红水晶
				break;
			case 6:
				putimage(j * 60, i * 60, &Blue_Key);    //蓝钥匙
				break;
			case 7:
				putimage(j * 60, i * 60, &Yellow_Key);   //黄钥匙
				break;
			case 8:
				putimage(j * 60, i * 60, &Red_Med);     //红药水
				break;
			case 9:
				putimage(j * 60, i * 60, &Blue_Med);     //蓝药水
				break;
			case 10:
				putimage(j * 60, i * 60, &Small_Bat);    //小蝙蝠
				break;
			case 11:
				putimage(j * 60, i * 60, &Small_Wizard);   //小巫师
				break;
			case 12:
				putimage(j * 60, i * 60, &Small_Skull);   //小骷髅
				break;
			case 13:
				putimage(j * 60, i * 60, &Big_Skull);    //大骷髅
				break;
			case 14:
				putimage(j * 60, i * 60, &Green_Slime);   //绿史莱姆
				break;
			case 15:
				putimage(j * 60, i * 60, &Red_Slime);    //红史莱姆
				break;
			case 98:
				putimage(j * 60, i * 60, &Ladder);     //梯子
				break;
			case 99:
				putimage(j * 60, i * 60, &Player);     //玩家
				break;
			}

		}
	}
}

接下来就是人物的移动和战斗了,人物的移动我就是直接对上下左右四种情况分别讨论, 在人物走动的时候要判断能不能走,不能走就不处理,如果能走,就把走到的那个位置上变成人,把之前人的位置变成地板。

case 'w':
case 72:
if (map[playerx - 1][playery] == 1) {     //下一步是地板
	map[playerx - 1][playery] = 99;
	map[playerx][playery] = 1;
	playerx--;
}

需要处理的就是钥匙,门, 水晶, 药水, 怪物。

如果是钥匙,把对应的钥匙数量加1。

如果是门,判断一下对应颜色的钥匙是否足够,如果足够,钥匙数量减1,然后把对应位置上的门变为空地。

如果是药水,吃了之后会增加生命。

如果是水晶,根据水晶的颜色加对应的属性。

当遇到怪物的时候回产生战斗,对于不同的怪物分开处理,下面是小蝙蝠的处理

case 10:
    ID = MessageBox(hwnd, "小蝙蝠", "是否攻击?", MB_YESNO);
    if (ID == IDYES)
	{
	  if (VS(player.HP, player.ATT, player.DEF, Small_Bat_Pro.HP, Small_Bat_Pro.ATT, Small_Bat_Pro.DEF)) {
			player.Exp += Small_Bat_Pro.Exp;
			return 1;
		  }
	}
	break;

遇到怪物是会弹出对应的对话框

此处有一个VS函数,用于计算战斗是否成功,如果成功,会加相应的属性,如果失败,则会弹出打不过的窗口。

int VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF)
{
	while (playHP > 0 || monHP > 0)
	{
		monHP -= (playATT - monDEF);
		if (monHP < 0)
			break;
		playHP -= (monATT - playDEF);
	}
	if (playHP > 0) {
		player.HP = playHP;
		return 1;
	}

	else {
		MessageBox(hwnd, "", "打不过", MB_YESNO);
		return 0;
	}
}

在每一次敲击键盘后更新地图信息和人物信息 :

SetMap(); //重新显示地图
SetPlayer(); //重新显示角色信息

到了这里,游戏的内容也就说的差不多了,虽然我只写出了第一个地图。但是,只要添加地图即可有更多的玩法,有兴趣的同学可以自制关卡,实现更多内容。

最后,加上所有代码,注释上说的也比较清楚。

#include <stdlib.h>
#include <graphics.h>
#include <windows.h>
#include<conio.h>
#include<graphics.h>
#include<windows.h>
#include <stdio.h>

void initgamePicture();  //加载游戏图片
void SetPlayer();    //显示角色信息
void initPlayer();   //初始化游戏角色
void SetMap();     //加载游戏地图
char *intToString(int Number); //把整数转化成字符串
void playGame();     //开始游戏
int Combat(int x);
int VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF);

int playerx, playery;
char str[20] = "";
//地图1
int map[13][13] = {
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 0, 98, 1, 14, 15, 14, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
	{ 0, 8, 1, 1, 2, 1, 0, 5, 6, 1, 0, 1, 0 },
	{ 0, 1, 12, 1, 0, 1, 0, 4, 8, 1, 0, 1, 0 },
	{ 0, 0, 2, 0, 0, 1, 0, 0, 0, 14, 0, 1, 0 },
	{ 0, 6, 1, 1, 0, 1, 3, 10, 11, 10, 0, 1, 0 },
	{ 0, 1, 13, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
	{ 0, 0, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 0, 0, 14, 0, 0, 0, 14, 0, 0 },
	{ 0, 8, 1, 6, 0, 7, 1, 6, 0, 1, 10, 1, 0 },
	{ 0, 8, 1, 6, 0, 1, 99, 1, 0, 14, 9, 14, 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
IMAGE Wall, Ground, Green_Slime, Red_Slime, Blue_Cry, Red_Cry, Blue_Key, Yellow_Key,
Red_Med, Blue_Med, Ladder, Small_Skull, Big_Skull, Small_Bat, Small_Wizard,
Blue_door, Yellow_door, Player, Message;
HWND hwnd;
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;
}player;

struct monster
{
	int HP;	  //血量
	int ATT;  //攻击
	int DEF;	//防御
	int Exp;  //经验
};
struct monster Green_Slime_Pro = { 50,10,12,100 };  //绿史莱姆属性
struct monster Red_Slime_Pro = { 100, 50, 12, 500 }; //红史莱姆属性
struct monster Small_Wizard_Pro = { 100, 30, 9, 400 };//小巫师属性
struct monster Small_Bat_Pro = { 20, 10, 9, 50 };     //小蝙蝠属性
struct monster Small_Skull_Pro = {30, 20, 10, 200};  //小骷髅属性
struct monster Big_Skull_Pro = {60, 50, 25, 300};   //大骷髅属性

int main()
{
	initPlayer();
	hwnd = initgraph(60 * 14, 60 * 13);
	initgamePicture();

	while (1) {
		SetMap();
		SetPlayer();
		playGame();
	}

	return 0;
}

/*
*显示角色信息
*/
void SetPlayer()
{
	putimage(60 * 13, 0, &Message);
	outtextxy(60 * 13 + 12, 100, player.name);
	outtextxy(60 * 13 + 12, 180, intToString(player.Lv));
	outtextxy(60 * 13 + 12, 235, intToString(player.Exp));
	outtextxy(60 * 13 + 12, 362, intToString(player.HP));
	outtextxy(60 * 13 + 12, 425, intToString(player.MP));
	outtextxy(60 * 13 + 12, 517, intToString(player.ATT));
	outtextxy(60 * 13 + 12, 567, intToString(player.DEF));
	outtextxy(60 * 13 + 12, 689, intToString(player.Num_Yellow_Key));
	outtextxy(60 * 13 + 12, 759, intToString(player.Num_Blue_Key));
}

/*
*  加载游戏图片
*/
void initgamePicture()
{
	loadimage(&Wall, "墙.jpg", 60, 60);
	loadimage(&Ground, "地板.jpg", 60, 60);
	loadimage(&Green_Slime, "绿史莱姆.jpg", 60, 60);
	loadimage(&Red_Slime, "红史莱姆.jpg", 60, 60);

	loadimage(&Blue_Cry, "蓝水晶.jpg", 60, 60);
	loadimage(&Red_Cry, "红水晶.jpg", 60, 60);

	loadimage(&Blue_Key, "蓝钥匙.jpg", 60, 60);
	loadimage(&Yellow_Key, "黄钥匙.jpg", 60, 60);

	loadimage(&Red_Med, "小红药水.jpg", 60, 60);
	loadimage(&Blue_Med, "小蓝药水.jpg", 60, 60);

	loadimage(&Ladder, "梯子.jpg", 60, 60);
	loadimage(&Small_Bat, "小蝙蝠.jpg", 60, 60);
	loadimage(&Small_Wizard, "小巫师.jpg", 60, 60);
	loadimage(&Small_Skull, "骷髅兵.jpg", 60, 60);
	loadimage(&Big_Skull, "大骷髅兵.jpg", 60, 60);

	loadimage(&Blue_door, "蓝门.jpg", 60, 60);
	loadimage(&Yellow_door, "黄门.jpg", 60, 60);
	loadimage(&Player, "人.jpg", 60, 60);
	loadimage(&Message, "info.jpg");
}

/*
*初始化游戏角色
*/
void initPlayer()
{
	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;
	playerx = 11;
	playery = 6;
}

//整数转换为字符
char *intToString(int Number)
{
	int len = 0;

	if (Number == 0) {
		str[0] = '0';
		len++;
	}
	while (Number)
	{
		str[len++] = Number % 10+'0';
		Number /= 10;
	}
	for (int i = 0; i < len/2; i++) {
		char t = str[i];
		str[i] = str[len - i - 1];
		str[len - i - 1] = t;
	}
	str[len] = '\0';
	return str;
}

/*
*加载游戏地图
*
*/
void SetMap()
{
	for (int i = 0; i < 13; i++)
	{
		for (int j = 0; j < 13; j++)
		{
			switch (map[i][j])
			{

			case 0:
				putimage(j * 60, i * 60, &Wall);     //墙
				break;
			case 1:
				putimage(j * 60, i * 60, &Ground);    //地板
				break;
			case 2:
				putimage(j * 60, i * 60, &Blue_door);   //蓝门
				break;
			case 3:
				putimage(j * 60, i * 60, &Yellow_door);  //黄门
				break;
			case 4:
				putimage(j * 60, i * 60, &Blue_Cry);     //蓝水晶
				break;
			case 5:
				putimage(j * 60, i * 60, &Red_Cry);     //红水晶
				break;
			case 6:
				putimage(j * 60, i * 60, &Blue_Key);    //蓝钥匙
				break;
			case 7:
				putimage(j * 60, i * 60, &Yellow_Key);   //黄钥匙
				break;
			case 8:
				putimage(j * 60, i * 60, &Red_Med);     //红药水
				break;
			case 9:
				putimage(j * 60, i * 60, &Blue_Med);     //蓝药水
				break;
			case 10:
				putimage(j * 60, i * 60, &Small_Bat);    //小蝙蝠
				break;
			case 11:
				putimage(j * 60, i * 60, &Small_Wizard);   //小巫师
				break;
			case 12:
				putimage(j * 60, i * 60, &Small_Skull);   //小骷髅
				break;
			case 13:
				putimage(j * 60, i * 60, &Big_Skull);    //大骷髅
				break;
			case 14:
				putimage(j * 60, i * 60, &Green_Slime);   //绿史莱姆
				break;
			case 15:
				putimage(j * 60, i * 60, &Red_Slime);    //红史莱姆
				break;
			case 98:
				putimage(j * 60, i * 60, &Ladder);     //梯子
				break;
			case 99:
				putimage(j * 60, i * 60, &Player);     //玩家
				break;
			}

		}
	}
}

int Combat(int x)
{
	int ID;
	switch (x) {
		case 10:
			ID = MessageBox(hwnd, "小蝙蝠", "是否攻击?", MB_YESNO);
			if (ID == IDYES)
			{
				if (VS(player.HP, player.ATT, player.DEF, Small_Bat_Pro.HP, Small_Bat_Pro.ATT, Small_Bat_Pro.DEF)) {
					player.Exp += Small_Bat_Pro.Exp;
					return 1;
				}
			}
			break;
		case 11:
			ID = MessageBox(hwnd, "遇到小巫师", "是否攻击?", MB_YESNO);
			if (ID == IDYES)
			{
				if (VS(player.HP, player.ATT, player.DEF, Small_Wizard_Pro.HP, Small_Wizard_Pro.ATT, Small_Wizard_Pro.DEF)) {
					player.Exp += Small_Wizard_Pro.Exp;
					return 1;
				}
			}
			break;
		case 12:
			ID = MessageBox(hwnd, "遇到小骷髅", "是否攻击?", MB_YESNO);
			if (ID == IDYES)
			{
				if (VS(player.HP, player.ATT, player.DEF, Small_Skull_Pro.HP, Small_Skull_Pro.ATT, Small_Skull_Pro.DEF)) {
					player.Exp += Small_Skull_Pro.Exp;
					return 1;
				}
			}
			break;
		case 13:
			ID = MessageBox(hwnd, "遇到大骷髅", "是否攻击?", MB_YESNO);
			if (ID == IDYES)
			{
				if (VS(player.HP, player.ATT, player.DEF, Big_Skull_Pro.HP, Big_Skull_Pro.ATT, Big_Skull_Pro.DEF)) {
					player.Exp += Big_Skull_Pro.Exp;
					return 1;
				}
			}
			break;
		case 14:
			ID = MessageBox(hwnd, "遇到绿史莱姆", "是否攻击?", MB_YESNO);
			if (ID == IDYES)
			{
				if (VS(player.HP, player.ATT, player.DEF, Green_Slime_Pro.HP, Green_Slime_Pro.ATT, Green_Slime_Pro.DEF)) {
					player.Exp += Green_Slime_Pro.Exp;
					return 1;
				}
			}
			break;
		case 15:
			ID = MessageBox(hwnd, "遇到红史莱姆", "是否攻击?", MB_YESNO);
			if (ID == IDYES)
			{
				if (VS(player.HP, player.ATT, player.DEF, Red_Slime_Pro.HP, Red_Slime_Pro.HP, Red_Slime_Pro.HP)) {
					player.Exp += Green_Slime_Pro.Exp;
					return 1;
				}
			}
			break;

	}
	return 0;
}

int VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF)
{
	while (playHP > 0 || monHP > 0)
	{
		monHP -= (playATT - monDEF);
		if (monHP < 0)
			break;
		playHP -= (monATT - playDEF);
	}
	if (playHP > 0) {
		player.HP = playHP;
		return 1;
	}

	else {
		MessageBox(hwnd, "", "打不过", MB_YESNO);
		return 0;
	}
}
void playGame()
{
	while (1)
	{
		char ch = _getch();
		switch (ch) {
			case 'w':
			case 72:
				if (map[playerx - 1][playery] == 1) {     //下一步是地板
					map[playerx - 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx--;
				}
				else if (map[playerx-1][playery]== 6) {   //下一步是蓝钥匙
					player.Num_Blue_Key++;
					map[playerx - 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx--;
				}
				else if (map[playerx - 1][playery] == 7) {   //下一步是黄钥匙
					player.Num_Yellow_Key++;
					map[playerx - 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx--;
				}
				//下一步是怪物
				else if (map[playerx - 1][playery] == 10 || map[playerx - 1][playery] == 11 ||
						 map[playerx - 1][playery] == 12 || map[playerx - 1][playery] == 13 ||
						 map[playerx - 1][playery] == 14 || map[playerx - 1][playery] == 15)
				{
					int x = Combat(map[playerx - 1][playery]);
					if (x == 1) {
						map[playerx - 1][playery] = 99;
						map[playerx][playery] = 1;
						playerx--;
					}
				}
				//红蓝药水
				else if (map[playerx - 1][playery] == 8 || map[playerx - 1][playery] == 9) {
					if (map[playerx - 1][playery] == 8)
						player.HP += 200;
					else
						player.HP += 500;
					map[playerx - 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx--;
				}
				//红蓝门
				else if (map[playerx - 1][playery] == 2 || map[playerx - 1][playery] == 3) {
					if (map[playerx - 1][playery] == 2 && player.Num_Blue_Key) {
						player.Num_Blue_Key--;
						map[playerx - 1][playery] = 99;
						map[playerx][playery] = 1;
						playerx--;
					}
					if (map[playerx - 1][playery] == 3 && player.Num_Yellow_Key) {
						player.Num_Yellow_Key--;
						map[playerx - 1][playery] = 99;
						map[playerx][playery] = 1;
						playerx--;
					}
				}
				//红蓝水晶
				//红水晶+2攻击
				//蓝水晶+2防御
				else if (map[playerx - 1][playery] == 4 || map[playerx - 1][playery] == 5) {
					if (map[playerx - 1][playery] == 4)
						player.DEF += 2;
					else if (map[playerx - 1][playery] == 5)
						player.ATT += 2;
					map[playerx - 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx--;
				}
				break;
			case 's':
			case 80:
				if (map[playerx + 1][playery] == 1) {     //下一步是地板
					map[playerx + 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx++;
				}
				else if (map[playerx + 1][playery] == 6) {   //下一步是蓝钥匙
					player.Num_Blue_Key++;
					map[playerx + 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx++;
				}
				else if (map[playerx + 1][playery] == 7) {   //下一步是黄钥匙
					player.Num_Yellow_Key++;
					map[playerx + 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx++;
				}
				//下一步是怪物
				else if (map[playerx + 1][playery] == 10 || map[playerx + 1][playery] == 11 ||
					map[playerx + 1][playery] == 12 || map[playerx + 1][playery] == 13 ||
					map[playerx + 1][playery] == 14 || map[playerx + 1][playery] == 15)
				{
					int x = Combat(map[playerx + 1][playery]);
					if (x == 1) {
						map[playerx + 1][playery] = 99;
						map[playerx][playery] = 1;
						playerx++;
					}
				}
				//红蓝药水
				else if (map[playerx + 1][playery] == 8 || map[playerx + 1][playery] == 9) {
					if (map[playerx + 1][playery] == 8)
						player.HP += 200;
					else
						player.HP += 500;
					map[playerx + 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx++;
				}
				//红蓝门
				else if (map[playerx + 1][playery] == 2 || map[playerx + 1][playery] == 3) {
					if (map[playerx + 1][playery] == 2 && player.Num_Blue_Key) {
						player.Num_Blue_Key++;
						map[playerx + 1][playery] = 99;
						map[playerx][playery] = 1;
						playerx++;
					}
					if (map[playerx + 1][playery] == 3 && player.Num_Yellow_Key) {
						player.Num_Yellow_Key++;
						map[playerx + 1][playery] = 99;
						map[playerx][playery] = 1;
						playerx++;
					}
				}
				//红蓝水晶
				//红水晶+2攻击
				//蓝水晶+2防御
				else if (map[playerx + 1][playery] == 4 || map[playerx + 1][playery] == 5) {
					if (map[playerx + 1][playery] == 4)
						player.DEF += 2;
					else if (map[playerx + 1][playery] == 5)
						player.ATT += 2;
					map[playerx + 1][playery] = 99;
					map[playerx][playery] = 1;
					playerx++;
				}
				break;
			case 'a':
			case 75:
				if (map[playerx][playery - 1] == 1) {     //下一步是地板
					map[playerx][playery - 1] = 99;
					map[playerx][playery] = 1;
					playery--;
				}
				else if (map[playerx][playery - 1] == 6) {   //下一步是蓝钥匙
					player.Num_Blue_Key++;
					map[playerx][playery - 1] = 99;
					map[playerx][playery] = 1;
					playery--;
				}
				else if (map[playerx][playery - 1] == 7) {   //下一步是黄钥匙
					player.Num_Yellow_Key++;
					map[playerx][playery - 1] = 99;
					map[playerx][playery] = 1;
					playery--;
				}
				//下一步是怪物
				else if (map[playerx][playery - 1] == 10 || map[playerx][playery - 1] == 11 ||
					map[playerx][playery - 1] == 12 || map[playerx][playery - 1] == 13 ||
					map[playerx][playery - 1] == 14 || map[playerx][playery - 1] == 15)
				{
					int x = Combat(map[playerx][playery - 1]);
					if (x == 1) {
						map[playerx][playery - 1] = 99;
						map[playerx][playery] = 1;
						playery--;
					}
				}
				//红蓝药水
				else if (map[playerx][playery - 1] == 8 || map[playerx][playery - 1] == 9) {
					if (map[playerx][playery - 1] == 8)
						player.HP += 200;
					else
						player.HP += 500;
					map[playerx ][playery- 1] = 99;
					map[playerx][playery] = 1;
					playery--;
				}
				//红蓝门
				else if (map[playerx][playery - 1] == 2 || map[playerx][playery - 1] == 3) {
					if (map[playerx][playery - 1] == 2 && player.Num_Blue_Key) {
						player.Num_Blue_Key--;
						map[playerx][playery - 1] = 99;
						map[playerx][playery] = 1;
						playery--;
					}
					if (map[playerx][playery - 1] == 3 && player.Num_Yellow_Key) {
						player.Num_Yellow_Key--;
						map[playerx][playery - 1] = 99;
						map[playerx][playery] = 1;
						playery--;
					}
				}
				//红蓝水晶
				//红水晶+2攻击
				//蓝水晶+2防御
				else if (map[playerx][playery - 1] == 4 || map[playerx][playery - 1] == 5) {
					if (map[playerx][playery - 1] == 4)
						player.DEF += 2;
					else if (map[playerx][playery - 1] == 5)
						player.ATT += 2;
					map[playerx][playery - 1] = 99;
					map[playerx][playery] = 1;
					playery--;
				}
				break;
			case 'd':
			case 77:
				if (map[playerx][playery + 1] == 1) {     //下一步是地板
					map[playerx][playery + 1] = 99;
					map[playerx][playery] = 1;
					playery++;
				}
				else if (map[playerx][playery + 1] == 6) {   //下一步是蓝钥匙
					player.Num_Blue_Key++;
					map[playerx][playery + 1] = 99;
					map[playerx][playery] = 1;
					playery++;
				}
				else if (map[playerx][playery + 1] == 7) {   //下一步是黄钥匙
					player.Num_Yellow_Key++;
					map[playerx][playery + 1] = 99;
					map[playerx][playery] = 1;
					playery++;
				}
				//下一步是怪物
				else if (map[playerx][playery + 1] == 10 || map[playerx][playery + 1] == 11 ||
					map[playerx][playery + 1] == 12 || map[playerx][playery + 1] == 13 ||
					map[playerx][playery + 1] == 14 || map[playerx][playery + 1] == 15)
				{
					int x = Combat(map[playerx][playery + 1]);
					if (x == 1) {
						map[playerx][playery + 1] = 99;
						map[playerx][playery] = 1;
						playery++;
					}
				}
				//红蓝药水
				else if (map[playerx][playery + 1] == 8 || map[playerx][playery + 1] == 9) {
					if (map[playerx][playery + 1] == 8)
						player.HP += 200;
					else
						player.HP += 500;
					map[playerx][playery + 1] = 99;
					map[playerx][playery] = 1;
					playery++;
				}
				//红蓝门
				else if (map[playerx][playery + 1] == 2 || map[playerx][playery + 1] == 3) {
					if (map[playerx][playery + 1] == 2 && player.Num_Blue_Key) {
						player.Num_Blue_Key--;
						map[playerx][playery + 1] = 99;
						map[playerx][playery] = 1;
						playery++;
					}
					if (map[playerx][playery + 1] == 3 && player.Num_Yellow_Key) {
						player.Num_Yellow_Key--;
						map[playerx][playery + 1] = 99;
						map[playerx][playery] = 1;
						playery++;
					}
				}
				//红蓝水晶
				//红水晶+2攻击
				//蓝水晶+2防御
				else if (map[playerx][playery + 1] == 4 || map[playerx][playery + 1] == 5) {
					if (map[playerx][playery + 1] == 4)
						player.DEF += 2;
					else if (map[playerx][playery + 1] == 5)
						player.ATT += 2;
					map[playerx][playery + 1] = 99;
					map[playerx][playery] = 1;
					playery++;
				}
				break;
		}
		SetMap(); //重新显示地图
		SetPlayer(); //重新显示角色信息
	}
}

到此这篇关于C语言魔塔游戏的实现代码的文章就介绍到这了,更多相关C语言魔塔游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 原创的C语言控制台小游戏

    最开始左上色块被感染,通过切换颜色,不断感染同色色块.亮点是可以切换图案,设置方块个数和最大限制次数.整体还是比较满意,希望大神指教. #include <stdio.h> #include <windows.h> #include <conio.h> #include <time.h> #include <stdlib.h> int DIFFICULT=44; int count=0 ; int TYPE_SHAPE=2 ; int flag=

  • 基于C语言实现的迷宫游戏代码

    本文实例讲述了基于C语言实现迷宫游戏的方法,代码备有较为详尽的注释,便于读者理解.通过该游戏代码可以很好的复习C语言的递归算法与流程控制等知识,相信对于学习游戏开发的朋友有一定的借鉴价值. 完整的实例代码如下: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> #define N 20/*迷宫的大小,可改

  • C语言贪吃蛇经典小游戏

    一.贪吃蛇小游戏简介: 用上下左右控制蛇的方向,寻找吃的东西,每吃一口就能得到一定的积分,而且蛇的身子会越吃越长,身子越长玩的难度就越大,不能碰墙,也不能咬到自己的身体,等到了一定的分数,就能过关. 二.函数框架 三.数据结构 typedef struct Snake { size_t x; //行 size_t y; //列 struct Snake* next; }Snake, *pSnake; 定义蛇的结构体,利用单链表来表示蛇,每个结点为蛇身体的一部分. 四.代码实现(vs2010  c

  • 基于C语言实现的扫雷游戏代码

    本文详细讲述了基于C语言实现的扫雷游戏代码,代码中备有比较详细的注释,便于读者阅读和理解.希望对学习游戏开发的朋友能有一点借鉴价值. 完整的实例代码如下: /* 模拟扫雷游戏 */ #include <graphics.h> #include <math.h> #include <stdio.h> #include <dos.h> #include <stdlib.h> #include <conio.h> #include <

  • C语言实现的猜拳游戏代码分享

    这是一个简单的猜拳游戏(剪子包子锤),让你与电脑对决.你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负. 下面的代码会实现一个猜拳游戏,让你与电脑对决.你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负. 启动程序后,让用户出拳,截图: 用户出拳,显示对决结果:截图: 代码实现: #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { char gamer; // 玩家出拳 int

  • C语言开发简易版扫雷小游戏

    前言: 想起来做这个是因为那时候某天知道了原来黑框框里面的光标是可以控制的,而且又经常听人说起这个,就锻炼一下好了. 之前就完成了那1.0的版本,现在想放上来分享却发现有蛮多问题的,而且最重要的是没什么注释[果然那时候太年轻]!现在看了也是被那时候的自己逗笑了,就修改了一些小bug,增加了算是详尽而清楚的注释,嗯,MSDN上面对各种函数的解释很详细的[又锻炼一下英语],顺便让开头和结尾的展示"动"了起来,就当作1.5的版本好了. 这个只是给出了一个实现的思路,其中肯定也有很多不合理的地

  • 基于C语言实现的贪吃蛇游戏完整实例代码

    本文以实例的形式讲述了基于C语言实现的贪吃蛇游戏代码,这是一个比较常见的游戏,代码备有比较详细的注释,对于读者理解有一定的帮助. 贪吃蛇完整实现代码如下: #include <graphics.h> #include <conio.h> #include <stdlib.h> #include <dos.h> #define NULL 0 #define UP 18432 #define DOWN 20480 #define LEFT 19200 #defi

  • 2048小游戏C语言实现代码

    本文实例为大家分享了C语言实现2048游戏的具体代码,供大家参考,具体内容如下 大一时学c语言写的,写的不好但当时感觉还行. 环境运行 vc6.0 ,cpp文件. 基本上是c写的,但是改变字体颜色,在控制台移动光标等等好像不是c中的. 代码: #include<time.h> #include<stdlib.h> #include<conio.h> #include<stdio.h> #include<windows.h> #define x0

  • C语言实现汉诺塔游戏

    操作就是:A B 号码A的塔顶一层放在号码B的塔顶.如1(空格) 3 回车. 话说有人能把我这C的代码添加到QT界面框架上去么?  代码写的不好 ,维护性不够,只能玩8层的,写完以后发现很难拓展,软件工程,设计模式有待提高.... 里面提示输入等级的装B用了,没有实现,大家随便输入个个位数就可以玩了. stackfunc.c #include"STACK.h" #include<stdio.h> extern ceng CENG[SIZE]; //数据入栈 void pus

  • 基于C语言实现简单的走迷宫游戏

    本文实例讲述了C语言实现简单的走迷宫游戏的方法,代码完整,便于读者理解. 学数据结构时用"栈"写的一个走迷宫程序,实际上用到双向队列,方便在运行完毕后输出经过的点. #include <cstdio> #include <deque> #include <windows.h> using namespace std; class node { public: int x,y; int lastOpt; }; deque<node> sta

随机推荐