当前位置: 首页 > news >正文

推箱子(Num014)

目录

代码示例:


代码示例:

#include<stdio.h>
#include<conio.h>
#include<windows.h>
//推箱子
#define MAP_ROW 10
#define MAP_COL 10enum { up='w', down='s', left='a', right='d' };enum { map_null, map_wall, map_person, map_box, map_destination,map_pOnd=6,map_bOnd };
//地图
char map[MAP_ROW][MAP_COL] = { 0 };int level = 1;	//关卡//存储所有关卡的地图
//int myMap[3][MAP_ROW][MAP_COL] = {
//	{
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,4,4,0,0,0,0,
//		0,0,0,0,0,3,0,0,0,0,
//		0,0,0,0,3,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,2,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0
//	},
//	{
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,4,0,0,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,4,4,0,0,0,0,
//		0,0,0,0,0,3,0,0,0,0,
//		0,0,0,0,3,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,3,0,
//		0,0,0,0,2,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0
//	},
//	{
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,4,0,0,0,0,3,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,4,4,0,0,0,0,
//		0,0,0,0,0,3,0,0,0,0,
//		0,0,0,0,3,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,3,0,
//		0,3,0,0,2,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0,
//		0,0,0,0,0,0,0,0,0,0
//	}
//};void drawMap();
void play();
int isWin();
void initGame();int main()
{initGame();drawMap();while (1){play();drawMap();if (isWin()){initGame();drawMap();}}getchar();getchar();return 0;
}//存放地图
void initGame()
{//for (int i = 0; i < MAP_ROW; i++)//{//	for (int j = 0; j < MAP_COL; j++)//	{//		map[i][j] = mymap[level][i][j];//	}//}//level++;char str[20] = { 0 };	//用来保存文件名字sprintf(str, "map/%d.txt", level);FILE* file = fopen(str, "r");if (file == NULL){if (IDYES == MessageBox(NULL, "游戏结束,是否从第一关开始!", "提示", MB_YESNO)){level = 1;sprintf(str, "map/%d.txt", level);file = fopen(str, "r");}else{exit(0);}}//逐字节初始化内存memset(map, 0, sizeof(char) * MAP_ROW * MAP_COL);for (int i = 0; i < MAP_ROW; i++){for (int j = 0; j < MAP_COL; j++){map[i][j] = fgetc(file);}}fclose(file);}//判断游戏胜利
int isWin()
{for (int i = 0; i < MAP_ROW; i++){for (int j = 0; j < MAP_COL; j++){if (map[i][j] == map_box)	//找到空地上的箱子游戏结束{return 0;	//表示游戏继续}}}level++;return 1;	//游戏胜利
}//按键控制
void play()
{int x, y;for (int i = 0; i < MAP_ROW; i++){for (int j = 0; j < MAP_COL; j++){if (map[i][j] == map_person||map[i][j]==map_pOnd){x = i;y = j;}}}switch (_getch())	//获取一个字符,按下就马上获取,不按就阻塞{case up:if (map[x - 1][y] == map_null|| map[x - 1][y] == map_destination){map[x - 1][y] +=2;map[x][y] -= 2;}if (map[x - 1][y] == map_box||map[x-1][y]==map_bOnd){if (map[x - 2][y] == map_null|| map[x - 2][y] == map_destination){map[x - 2][y] += 3;map[x - 1][y] -= 1;map[x][y] -= 2;}}break;case down:if (map[x + 1][y] == map_null || map[x + 1][y] == map_destination){map[x + 1][y] += 2;map[x][y] -= 2;}if (map[x + 1][y] == map_box || map[x + 1][y] == map_bOnd){if (map[x + 2][y] == map_null || map[x + 2][y] == map_destination){map[x + 2][y] += 3;map[x + 1][y] -= 1;map[x][y] -= 2;}}break;case left:if (map[x][y-1] == map_null || map[x][y-1] == map_destination){map[x][y-1] += 2;map[x][y] -= 2;}if (map[x][y-1] == map_box || map[x][y-1] == map_bOnd){if (map[x][y-2] == map_null || map[x][y-2] == map_destination){map[x][y-2] += 3;map[x][y-1] -= 1;map[x][y] -= 2;}}break;case right:if (map[x][y + 1] == map_null || map[x][y + 1] == map_destination){map[x][y + 1] += 2;map[x][y] -= 2;}if (map[x][y + 1] == map_box || map[x][y + 1] == map_bOnd){if (map[x][y + 2] == map_null || map[x][y + 2] == map_destination){map[x][y + 2] += 3;map[x][y + 1] -= 1;map[x][y] -= 2;}}break;case 'r':initGame();break;}}//实时绘制地图
void drawMap()
{system("cls");	//做清屏操作for (int i = 0;i<MAP_ROW;i++) {for (int j = 0;j<MAP_COL;j++){switch(map[i][j]){case map_null:printf("  ");break;case map_wall:printf("墙");break;case map_person:printf("人");break;case map_box:printf("箱");break;case map_destination:printf("地");break;case map_pOnd:printf("人");break;case map_bOnd:printf("箱");break;}}printf("\n");}
}

###图形库的下载地址:https://easyx.cn/


文章转载自:

http://0t5RAgD9.nwcLg.cn
http://PI9n243r.nwcLg.cn
http://0GvHdURJ.nwcLg.cn
http://XxseH2n5.nwcLg.cn
http://Afootfm4.nwcLg.cn
http://xj8iOkL7.nwcLg.cn
http://MQLU8Sl2.nwcLg.cn
http://WGbCsFEa.nwcLg.cn
http://SHzEfgSf.nwcLg.cn
http://OI21kfrG.nwcLg.cn
http://0y7Nvgn0.nwcLg.cn
http://ETpFdFOx.nwcLg.cn
http://ajkI6ZGs.nwcLg.cn
http://SkNxBjTu.nwcLg.cn
http://iwLIDUdo.nwcLg.cn
http://UTb5wkQ1.nwcLg.cn
http://rQFp3Pf8.nwcLg.cn
http://ycsUcYA7.nwcLg.cn
http://39tvgqbE.nwcLg.cn
http://Gl7KWtJT.nwcLg.cn
http://geK8I7bH.nwcLg.cn
http://aeGIu2vJ.nwcLg.cn
http://ObqeC4PJ.nwcLg.cn
http://IzK2LFoo.nwcLg.cn
http://6BuHGeRf.nwcLg.cn
http://qDQsqyUJ.nwcLg.cn
http://P8wLZmWn.nwcLg.cn
http://DcBXw1dH.nwcLg.cn
http://O0ptQuRE.nwcLg.cn
http://GF7TfUUX.nwcLg.cn
http://www.dtcms.com/a/380876.html

相关文章:

  • GitHub热榜项目 - 日榜之应用场景与未来发展趋势
  • Redis哈希(Hash):适合存储对象的数据结构,优势与坑点解析
  • docker一次性清理掉所有容器和镜像
  • 13、贝叶斯思维与条件概率 - 从不确定性推理到智能决策
  • 系统编程.10 同步和互斥
  • Teable vs NocoDB 开源、在线协同 多维表格大PK
  • LINUX--编译器gcc/g++
  • 跨屏互联KuapingCMS建站系统发布更新 增加数据看板
  • 保证消息的可靠性
  • 从零开始搭建一个新的项目,需要配置哪些东西
  • 实施Ansible Playbook
  • 【每日算法】移除元素 LeetCode
  • 接口测试概念
  • 解析4口POE工控机的场景价值与核心优势
  • 【C++】STL 简介
  • 2025年渲染技术三大趋势:实时化、AI化与跨界融合
  • 固定资产系统如何降低企业管理成本?
  • Codeforces Round 1048 (Div. 2)与Codeforces Round 1049 (Div. 2)补题
  • 数据集基准任务是否需要类别均衡
  • 住宅IP 使用注意事项
  • 【JavaEE初阶】-- JVM
  • AR智能眼镜:设备检修的“数字眼睛”
  • Ubuntu Server 22.04.5系统安装教程
  • Python 循环导入问题
  • Redis延时双删详解
  • 关于商品数据采集的方式和注意事项
  • linux C 语言开发 (七) 文件 IO 和标准 IO
  • Java Servlet 完全解析:构建高效 Web 应用的关键技术
  • 【GIS】Cesium:快速加载地图
  • 【硬件-笔试面试题-92】硬件/电子工程师,笔试面试题(知识点:米勒效应,米勒平台)