当前位置:三九宝宝网 → 宝宝教育 → 智力培养 → 正文

如何用C语言编写控制台小游戏

更新:01-04 整理:39baobao.com
字体:

[如何编写vb拼音打字游戏]加10个label1控件组,一个timer,操作:运行后,回车即可进行游戏,代码:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As IntegerDim m As IntegerPri...+阅读

//C语言实例:推箱子小游戏#include#include#include#include//行和列 #define ROW 10 #define COL 11 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ /** ** */ //地图 char map[ROW][COL] = { "##########",//0 "### ##",//1 "### ##",//2 "##AX # ##",//3 "### ## ",//4 "##### #",//5 "## #",//6 "# ####",//7 "### ",//8 "##########" //9 //A:人 , X:箱子 } ; //打印地图 void showMap(); //接收小人的方向 char enterDirection(); //小人向上移动的方法 void moveToUp(); //小人向下移动的方法 void moveToDown(); //小人向右移动的方法 void moveToRight(); //小人向左移动的方法 void moveToLeft(); //当前小人的坐标 int currentPersonRow = 3; int currentPersonCol = 2; //当前箱子的坐标 int currentBoxRow = 3; int currentBoxCol = 3; int main(int argc, char *argv[]) { //system("clear"); printf("点击回车键开始游戏 ^_^\n\n"); //1代表运行 0停止 int flag = 1; while(flag==1){ //显示地图 showMap(); //接收小人的方向 char dir = enterDirection(); switch(dir){ //小人向上移动 case 'w': case 'W': moveToUp(); break; //小人向下移动 case 's': case 'S': moveToDown(); break; //小人向右移动 case 'd': case 'D': moveToRight(); break; //小人向左移动 case 'a': case 'A': moveToLeft(); break; //停止运行 case 'q': case 'Q': printf("你的智商真低!T_T\n"); flag = 0; break; } showMap(); if(currentBoxRow==8¤tBoxCol==9){ printf("你的智商真高^_^!!!"); flag = 0; } } } /* 方法的实现 */ //打印地图 void showMap(){ int i; for(i = 0;i

Q:退出"); printf("\n\n\n\n\n"); } //接收小人的方向 char enterDirection(){ //清除SCANF中的缓冲区 rewind(stdin); char dir; dir = getch(); //scanf("%c",&dir); return dir; } //小人向上移动的方法 void moveToUp(){ //小人的下一个坐标 int nextPersonCol = currentPersonCol; int nextPersonRow = currentPersonRow - 1; //箱子的下一个坐标 int nextBoxRow = currentBoxRow - 1; int nextBoxCol = currentBoxCol; //如果小人的下一个坐标是路 if(map[nextPersonRow][nextPersonCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; } //如果小人的下一个坐标是墙 if(map[nextPersonRow][nextPersonCol]=='#'){ //什么也不做 } //如果小人的下一个坐标是箱子 if(map[nextPersonRow][nextPersonCol]=='X'){ if(map[nextBoxRow][nextBoxCol] == ' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; map[nextBoxRow][nextBoxCol] = 'X'; map[currentBoxRow][currentBoxCol] = 'A'; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; currentBoxRow = nextBoxRow; currentBoxCol = nextBoxCol; } } } //小人向下移动的方法 void moveToDown(){ //小人的下一个坐标 int nextPersonCol = currentPersonCol; int nextPersonRow = currentPersonRow + 1; //箱子的下一个坐标 int nextBoxRow = currentBoxRow + 1; int nextBoxCol = currentBoxCol; //如果小人的下一个坐标是路 if(map[nextPersonRow][nextPersonCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; } //如果小人的下一个坐标是墙 if(map[nextPersonRow][nextPersonCol]=='#'){ //什么也不做 } //如果小人的下一个坐标是箱子 if(map[nextPersonRow][nextPersonCol]=='X'){ if(map[nextBoxRow][nextBoxCol] == ' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; map[nextBoxRow][nextBoxCol] = 'X'; map[currentBoxRow][currentBoxCol] = 'A'; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; currentBoxRow = nextBoxRow; currentBoxCol = nextBoxCol; } } } //小人向右移动的方法 void moveToRight(){ //小人的下一个坐标 int nextPersonCol = currentPersonCol + 1; int nextPersonRow = currentPersonRow; //箱子的下一个坐标 int nextBoxRow = currentBoxRow; int nextBoxCol = currentBoxCol + 1; //如果小人的下一个坐标是路 if(map[nextPersonRow][nextPersonCol]==' '){ map[nextPersonRow][nextPersonCol] = 'A'; map[currentPersonRow][currentPersonCol] = ' '; currentPersonRow = nextPersonRow; currentPersonCol = nextPersonCol; } //如果小人的下一个坐标是墙 if(map[nextPersonRow][nextPersonCol]=='#'){ //什么也不做 } //如果小人的下一个坐标是箱子 if(map[nextPersonRow][nextPersonCol]=='X'){ if(map[nextBoxRow][nextBoxCol]==' '){ map[...

本文地址:https://www.39baobao.com/show/32_30556.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

vb编写打字游戏的源代码11月11日 00:46 1、 Dim n As Integer, s As Integer n = InputBox("请输入一个偶数n") s = 0 Do While n >= 2 s = s + ((n - 1) * 10 + 2) - (n * 10 + 2) n = n - 2 Loop Msg...

如何编写科研课题年度工作总结主要写一下工作内容,取得的成绩,以及不足,最后提出合理化的建议或者新的努力方向。。。。。。 转载:总结,就是把一个时间段的情况进行一次全面系统的总检查、总评价、总分析、总...

编写一个童话故事介绍森林里一群动物举行音乐会的盛况要写出参森林音乐会 夏天的傍晚,才下过阵雨.空气中充满了泥土和青草的芳香.一年一度的森林音乐会就在今晚隆重举行.动物们都聚集在半山腰的自由湖畔,低声的窃窃私语,焦急而兴奋的等主持...

编写一个童话故事介绍森林里一群动物举行音乐会的盛况要写出参加森林音乐会 夏天的傍晚,才下过阵雨.空气中充满了泥土和青草的芳香.一年一度的森林音乐会就在今晚隆重举行.动物们都聚集在半山腰的自由湖畔,低声的窃窃私语,焦急而兴奋的等主持...

怎样制定和编写管理与工作标准一.目的和作用在实施HSH管理体系过程中,文件和实施程序内容较多,不便于岗位人员学习,因此,单一岗位需要一份比较系统的指导文件。岗位作业指导书就是结合传统安全管理方法和HSH...

如何编写android拼图游戏源码主界面开始之前,可以在该按钮选择附加选项,代表意义是:是否开始声音、是否继续上次最后玩的拼图、是否随机选择一幅拼图。 菜单,可分类管理:在列表右侧,可上下拖动排序;在列表左侧,...

如何编写软件测试计划有完整的模板么?一下是系统测试计划模板: 1)范围 标识、系统概述、文档概述 2)引用文档 3)总体测评要求 测试要求、测试技术和方法、测试任务结束条件、被测软件评价准则和方法、数据采集要求 4...

编写一个简单VB程序可能字数太多,发不上来,我分开发。这是第一部分。关键函数在第2部分。 楼上的方法都不可用,因为计算结果太大。用双精度无法显示最后0的位数。所以,需要使用String类型来进行单...

怎样编写公章管理制度转载 公司公章管理制度 一、公司印章指公司行政章、合同章及财务章,公章由公司财务办公室负责管理,未经总经理批准不得随意交由他人管理和使用。因故需临时交接,须经总经理批准...