C語(yǔ)言魔塔游戲的實(shí)現(xiàn)代碼
很早就很想寫這個(gè),今天終于寫完了。
游戲截圖:



編譯環(huán)境: VS2017
游戲需要一些圖片,如果有想要的或者對(duì)游戲有什么看法的可以加我的QQ 2985486630 討論,如果暫時(shí)沒有回應(yīng),可以在博客下方留言,到時(shí)候我會(huì)看到。覺得麻煩的直接下載C-mota_jb51.rar
解壓后點(diǎn)擊sln文件直接可以運(yùn)行
下面我來介紹一下游戲的主要功能和實(shí)現(xiàn)方式
首先是玩家的定義,使用結(jié)構(gòu)體,這個(gè)名字是可以自己改變的
struct gamerole
{
char name[20] = "黑蛋"; //玩家名字
int HP; //血量
int MP;
int DEF; //防御
int ATT; //攻擊
int Lv; //等級(jí)
int Exp; //經(jīng)驗(yàn)
int Num_Blue_Key; //藍(lán)鑰匙數(shù)量
int Num_Yellow_Key;
}player;
在游戲的右邊顯示任務(wù)的各項(xiàng)屬性

函數(shù):
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));
}
由于這個(gè)函數(shù)要求屬性必須是字符串,所以我寫了一個(gè)把數(shù)字轉(zhuǎn)化成字符串的函數(shù)
//整數(shù)轉(zhuǎn)換為字符
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; //經(jīng)驗(yàn)
};
接下來就是定義畫布,然后加載圖片,我用一個(gè)二維數(shù)組存下了地圖,不同的數(shù)字代表不同的圖片,然后根據(jù)二維數(shù)組的值把不同的地方貼上不同的圖片。
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); //藍(lán)門
break;
case 3:
putimage(j * 60, i * 60, &Yellow_door); //黃門
break;
case 4:
putimage(j * 60, i * 60, &Blue_Cry); //藍(lán)水晶
break;
case 5:
putimage(j * 60, i * 60, &Red_Cry); //紅水晶
break;
case 6:
putimage(j * 60, i * 60, &Blue_Key); //藍(lán)鑰匙
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); //藍(lán)藥水
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;
}
}
}
}
接下來就是人物的移動(dòng)和戰(zhàn)斗了,人物的移動(dòng)我就是直接對(duì)上下左右四種情況分別討論, 在人物走動(dòng)的時(shí)候要判斷能不能走,不能走就不處理,如果能走,就把走到的那個(gè)位置上變成人,把之前人的位置變成地板。
case 'w':
case 72:
if (map[playerx - 1][playery] == 1) { //下一步是地板
map[playerx - 1][playery] = 99;
map[playerx][playery] = 1;
playerx--;
}
需要處理的就是鑰匙,門, 水晶, 藥水, 怪物。
如果是鑰匙,把對(duì)應(yīng)的鑰匙數(shù)量加1。
如果是門,判斷一下對(duì)應(yīng)顏色的鑰匙是否足夠,如果足夠,鑰匙數(shù)量減1,然后把對(duì)應(yīng)位置上的門變?yōu)榭盏亍?/p>
如果是藥水,吃了之后會(huì)增加生命。
如果是水晶,根據(jù)水晶的顏色加對(duì)應(yīng)的屬性。
當(dāng)遇到怪物的時(shí)候回產(chǎn)生戰(zhàn)斗,對(duì)于不同的怪物分開處理,下面是小蝙蝠的處理
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;
遇到怪物是會(huì)彈出對(duì)應(yīng)的對(duì)話框

此處有一個(gè)VS函數(shù),用于計(jì)算戰(zhàn)斗是否成功,如果成功,會(huì)加相應(yīng)的屬性,如果失敗,則會(huì)彈出打不過的窗口。
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(); //重新顯示角色信息
到了這里,游戲的內(nèi)容也就說的差不多了,雖然我只寫出了第一個(gè)地圖。但是,只要添加地圖即可有更多的玩法,有興趣的同學(xué)可以自制關(guān)卡,實(shí)現(xiàn)更多內(nèi)容。
最后,加上所有代碼,注釋上說的也比較清楚。
#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); //把整數(shù)轉(zhuǎn)化成字符串
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; //等級(jí)
int Exp; //經(jīng)驗(yàn)
int Num_Blue_Key; //藍(lán)鑰匙數(shù)量
int Num_Yellow_Key;
}player;
struct monster
{
int HP; //血量
int ATT; //攻擊
int DEF; //防御
int Exp; //經(jīng)驗(yàn)
};
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, "藍(lán)水晶.jpg", 60, 60);
loadimage(&Red_Cry, "紅水晶.jpg", 60, 60);
loadimage(&Blue_Key, "藍(lán)鑰匙.jpg", 60, 60);
loadimage(&Yellow_Key, "黃鑰匙.jpg", 60, 60);
loadimage(&Red_Med, "小紅藥水.jpg", 60, 60);
loadimage(&Blue_Med, "小藍(lán)藥水.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, "藍(lán)門.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;
}
//整數(shù)轉(zhuǎn)換為字符
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); //藍(lán)門
break;
case 3:
putimage(j * 60, i * 60, &Yellow_door); //黃門
break;
case 4:
putimage(j * 60, i * 60, &Blue_Cry); //藍(lán)水晶
break;
case 5:
putimage(j * 60, i * 60, &Red_Cry); //紅水晶
break;
case 6:
putimage(j * 60, i * 60, &Blue_Key); //藍(lán)鑰匙
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); //藍(lán)藥水
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) { //下一步是藍(lán)鑰匙
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--;
}
}
//紅藍(lán)藥水
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--;
}
//紅藍(lán)門
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--;
}
}
//紅藍(lán)水晶
//紅水晶+2攻擊
//藍(lán)水晶+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) { //下一步是藍(lán)鑰匙
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++;
}
}
//紅藍(lán)藥水
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++;
}
//紅藍(lán)門
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++;
}
}
//紅藍(lán)水晶
//紅水晶+2攻擊
//藍(lán)水晶+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) { //下一步是藍(lán)鑰匙
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--;
}
}
//紅藍(lán)藥水
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--;
}
//紅藍(lán)門
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--;
}
}
//紅藍(lán)水晶
//紅水晶+2攻擊
//藍(lán)水晶+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) { //下一步是藍(lán)鑰匙
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++;
}
}
//紅藍(lán)藥水
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++;
}
//紅藍(lán)門
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++;
}
}
//紅藍(lán)水晶
//紅水晶+2攻擊
//藍(lán)水晶+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(); //重新顯示角色信息
}
}
到此這篇關(guān)于C語(yǔ)言魔塔游戲的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)C語(yǔ)言魔塔游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解memmove()與memcpy()的區(qū)別以及實(shí)現(xiàn)方法
本篇文章是對(duì)memmove()與memcpy()的區(qū)別以及實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言入門篇--四大常量(字面,const修飾,宏,枚舉)及標(biāo)識(shí)符
本篇文章是c語(yǔ)言基礎(chǔ)篇,主要講述一下常量,常量即不可被直接修改的量(const修飾的常變量可間接修改,后續(xù)文章會(huì)繼續(xù)說明)請(qǐng)大家持續(xù)關(guān)注腳本之家2021-08-08
C++?OpenCV實(shí)現(xiàn)物體尺寸測(cè)量示例詳解
本文主要介紹了利用OpenCV對(duì)物體的尺寸進(jìn)行測(cè)量,即先定位到待測(cè)物體的位置,然后測(cè)量物體的寬高。感興趣的同學(xué)可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2022-01-01
C語(yǔ)言 如何求兩整數(shù)的最大公約數(shù)與最小公倍數(shù)
這篇文章主要介紹了C語(yǔ)言中如何求兩整數(shù)的最大公約數(shù)與最小公倍數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

