世嘉新人培训---第一章 游戏示例 推箱子游戏

news/2025/3/19 23:52:20/

跟着《游戏开发:世嘉新人培训教材》一书中推箱子游戏的逻辑写的推箱子游戏,整体代码不长,也没有特别难以理解的逻辑,后续看看自己能否进行一定的改进,增加游戏难度和代码的运行速度。

#include<iostream>//长
const int gHeight = 5;
//宽
const int gWidth = 8;
//游戏场景
const char gStageData[] = "\
########\n\
# .. p #\n\
# oo   #\n\
#      #\n\
########\n";enum Object {OBJ_SPACE,	//无物体OBJ_WALL,	//墙壁OBJ_GOAL,	//目的地OBJ_BLOCK,	//箱子OBJ_BLOCK_ON_GOAL,	//箱子到达目的地OBJ_PLAYER,	//玩家OBJ_PLAYER_ON_GOAL,	//玩家到达目的地OBJ_UNKONW	//未知,错误位置
};//初始化函数
void initialize(Object* state, int height, int width, const char* stage);
//绘制初始游戏窗口函数
void draw(Object* state, int height, int width);
void findPlayer(Object* state, int& playerX, int& playerY, int height, int width);
//更新游戏窗口函数
void update(Object* state, int height, int width, char input);
//确认是否通关函数
bool checkClear(Object* state, int height, int width);int main() {Object* state = new Object[gHeight*gWidth];initialize(state, gHeight, gWidth, gStageData);//主循环while (true) {draw(state, gHeight, gWidth);//确认是否通关if (checkClear(state,gHeight,gWidth) == true) {break;}std::cout << "w:前,s:后,a:左,b:右 \n";//获取输入char input;std::cin >> input;update(state,gHeight,gWidth,input);}delete[] state;std::cout << "恭喜通关\n";
}void initialize(Object* state, int height, int width, const char* stageData) {const char* temp = stageData;	//读取地图数据int x = 0;	//行int y = 0;	//宽while (*temp != NULL) {Object t;switch (*temp) {case '#':t = OBJ_WALL; break;case ' ':t = OBJ_SPACE; break;case 'p':t = OBJ_PLAYER; break;case 'P':t = OBJ_PLAYER_ON_GOAL; break;case '.':t = OBJ_GOAL; break;case 'o':t = OBJ_BLOCK; break;case 'O':t = OBJ_BLOCK_ON_GOAL; break;case '\n':x++;  y = 0; t = OBJ_UNKONW; break;default:t = OBJ_UNKONW; break;}temp++;//传入数据if (t != OBJ_UNKONW) {state[x * width + y] = t;y++;}}
}void draw(Object* state, int height, int width) {const char block[] = { ' ','#', '.', 'o','O','p', 'P' };for (int x = 0; x < height; x++) {for (int y = 0; y < width; y++) {Object temp = state[x * width + y];std::cout << block[temp];}//换行std::cout << '\n';}
}void findPlayer(Object* state, int& playerX, int& playerY, int height, int width ) {for (int i = 0; i < gWidth * gHeight; i++) {if (state[i] == OBJ_PLAYER || state[i] == OBJ_PLAYER_ON_GOAL) {playerX = i % width;playerY = i / width;break;}}
}void update(Object* state, int height, int width, char input) {//移动量int dx = 0;int dy = 0;switch (input) {case 'a':dx = -1; break;case 'd':dx = 1; break;case 'w':dy = -1; break;case 's':dy = 1; break;}//找到玩家的位置int playerX = 0;int playerY = 0;findPlayer(state, playerX, playerY, height, width);//移动后的坐标位置int tx = dx + playerX;int ty = dy + playerY;//移动失败的情况if (tx < 0 || tx > width || ty < 0 || ty > height) {return;}//移动成功int player = width * playerY + playerX;	//玩家位置int tp = width * ty + tx;	//预计移动到的位置//目标位置是空位或者目的地,玩家可以移动if (state[tp] == OBJ_SPACE || state[tp] == OBJ_GOAL) {//预计移动的位置为目的地,将该点转换为玩家在目的地上的状态,否者仍是目的地状态state[tp] = (state[tp] == OBJ_GOAL) ? OBJ_PLAYER_ON_GOAL : OBJ_PLAYER;//玩家当前的位置为在目的地上,移动之后将这个点转换为目的地,否者为空位state[player] = (state[player] == OBJ_PLAYER_ON_GOAL) ? OBJ_GOAL : OBJ_SPACE;}//目标位置有箱子else if (state[tp] == OBJ_BLOCK || state[tp] == OBJ_BLOCK_ON_GOAL){//箱子的移动int boxX = tx + dx;int boxY = ty + dy;//到达游戏边界 不能移动if (boxX < 0 || boxX > width || boxY < 0 || boxY > height) {return;}int box = width * boxY + boxX;	//箱子预计移动的位置if (state[box] == OBJ_SPACE || state[box] == OBJ_GOAL) {state[box] = (state[box] == OBJ_GOAL) ? OBJ_BLOCK_ON_GOAL : OBJ_BLOCK;state[tp] = (state[tp] == OBJ_BLOCK_ON_GOAL) ? OBJ_PLAYER_ON_GOAL : OBJ_PLAYER;state[player] = (state[player] == OBJ_PLAYER_ON_GOAL) ? OBJ_GOAL : OBJ_SPACE;}}
}bool checkClear(Object* state, int height, int width) {for (int i = 0; i < height * width; i++) {if (state[i] == OBJ_BLOCK)return false;}return true;
}


http://www.ppmy.cn/news/450891.html

相关文章

r7 6800hs参数 锐龙r7 6800hs评测

R7 6800Hs 采用6纳米工艺 8 核 16 线程&#xff0c;主频 3.2GHz-4.7GHz&#xff0c;一级缓存 512KB二级缓存 4MB 三级缓存 16MB热设计功耗(TDP) 35W 内存参数&#xff0c;搭载了 DDR5 集成显卡AMD Radeon 680M r7 6800hs怎么样这些点很重要 http://www.adiannao.cn/dy

【Cocos游戏】《疾风勇者传》:内测7天强势突破100万用户

日式RPG中独有的东方式柔美&#xff0c;给人带来的心灵感动&#xff0c;相信年龄稍大的玩家们永远不会遗忘。而由触控科技带来的《疾风勇者传》正是一款有着怀旧风的日式动漫ARPG手游&#xff0c;凭借过硬质量和独特玩法已经成为了一匹强劲黑马。 自6月16日《疾风勇者传》安卓不…

头皮发麻之win10宽带拨号错误797

温馨提示&#xff1a; 本人耗费数小时&#xff0c;查阅资料无数&#xff0c;可惜都不能解决我这个问题&#xff0c;我无意间的一次尝试&#xff0c;让我重获新生&#xff0c;故此写此文章&#xff0c;希望能帮助到各位&#xff0c;注释&#xff1a;本人windows规格如下图&…

TA游戏推荐:精选iOS游戏大作 智器X7玩酷无压力

智器X7一样可以爽玩IOS平台大作 伴随着硬件性能的不断升级与系统的完善与优化&#xff0c;在高端机型上流畅运行大型游戏自然不在话下&#xff0c;iOS的经典游戏大作不再只是独角戏&#xff0c;搭载智卓系统的智器X7一样可以爽玩iOS大作。今天小编特地整理了一些智器X7酷…

1月29日服务器例行维护公告,《高能手办团》1月29日例行维护公告

各位亲爱的手办收藏家&#xff1a; 为了提升您在手办世界的游戏体验&#xff0c;《高能手办团》将于1月29日4:00进行例行维护。维护期间&#xff0c;收藏家们将无法进入原服务器进行游戏。维护结束后&#xff0c;我们将以邮件的方式向各位收藏家送上补偿奖励。维护期间请大家留…

Cocos精品 | 《猫和老鼠官方手游》带你重返童年6点钟!

网易游戏与华纳联手打造的童年跑酷大作《猫和老鼠官方手游》&#xff0c;已经与我们见面有一段时间了。这款官方正版游戏基于cocos引擎开发&#xff0c;上线仅24小时下载量即突破60万。在获得App Store“优秀新游戏”推荐后&#xff0c;更登顶iPad免费总榜第一&#xff0c;iPho…

《Sanmill 直棋游戏》创作之旅

前言 大家好&#xff0c;我是 Calcitem 方解石&#xff0c;一名程序员&#xff0c;开源业余爱好者。 Calcitem 后面多加了一个 m&#xff1f;是的&#xff0c;不是笔误哈&#xff0c;Calcite 这个单词很容易被先占&#xff0c;所以加了一个 m。 去年对自己而言是特殊的一年&am…

棋牌娱乐盛宴推动全民游戏健康发展

2011年2月20日&#xff0c;国家体育总局棋牌运动管理中心和中国移动通信集团公司共同主办&#xff0c;中国移动游戏基地承办的首届“掌上对决&#xff0c;棋开得胜”全国棋牌网络争霸赛吹响了正式比赛的号角。首先开展的是省级突围赛&#xff0c;从2011年2月20日开始&#xff0…