vc++6.0推箱子代码

时间: 2023-07-31 12:05:49 浏览: 42
以下是一个简单的 VC++6.0 推箱子代码示例: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; const int MAXN = 20; const int MAXM = 20; const int WALL = -1; const int BLANK = 0; const int BOX = 1; const int TARGET = 2; const int PLAYER = 3; const int MOVEX[4] = {-1, 0, 1, 0}; const int MOVEY[4] = {0, 1, 0, -1}; int map[MAXN][MAXM]; int n, m; int px, py; void init() { memset(map, WALL, sizeof(map)); cout << "Please input the size of the map (n m): "; cin >> n >> m; cout << "Please input the map (use # to represent wall, . to represent blank, * to represent target, and @ to represent player):" << endl; char ch; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> ch; if (ch == '#') map[i][j] = WALL; else if (ch == '.') map[i][j] = BLANK; else if (ch == '*') map[i][j] = TARGET; else if (ch == '@') map[i][j] = PLAYER, px = i, py = j; } } void print_map() { system("cls"); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) if (map[i][j] == WALL) cout << '#'; else if (map[i][j] == BLANK) cout << ' '; else if (map[i][j] == TARGET) cout << '*'; else if (map[i][j] == BOX) cout << '$'; else if (map[i][j] == PLAYER) cout << '@'; cout << endl; } } bool push_box(int x, int y, int dir) { int nx = x + MOVEX[dir], ny = y + MOVEY[dir]; if (map[nx][ny] == WALL || map[nx][ny] == BOX) return false; map[nx][ny] = BOX; map[x][y] = (map[x][y] == TARGET ? TARGET : BLANK); return true; } bool move(int dir) { int nx = px + MOVEX[dir], ny = py + MOVEY[dir]; if (map[nx][ny] == WALL) return false; if (map[nx][ny] == BLANK) { map[px][py] = BLANK; map[nx][ny] = PLAYER; px = nx, py = ny; return true; } if (map[nx][ny] == TARGET) { map[px][py] = BLANK; map[nx][ny] = PLAYER; px = nx, py = ny; return true; } if (map[nx][ny] == BOX) { bool success = push_box(nx, ny, dir); if (success) { map[px][py] = BLANK; map[nx][ny] = PLAYER; px = nx, py = ny; } return success; } } int main() { init(); print_map(); while (true) { int ch = getch(); if (ch == 27) break; bool success = false; if (ch == 'w' || ch == 'W') success = move(0); if (ch == 'd' || ch == 'D') success = move(1); if (ch == 's' || ch == 'S') success = move(2); if (ch == 'a' || ch == 'A') success = move(3); if (success) print_map(); } return 0; } ``` 该代码可以让用户自定义地图,并使用 WASD 键来控制人物移动,将箱子推到目标点即可胜利。

相关推荐

最新推荐

recommend-type

VC++ 6.0 C语言实现俄罗斯方块详细教程

主要为大家介绍了VC++ 6.0 C语言实现俄罗斯方块详细教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

VC++6.0开发环境学习指导手册

VC++6.0提供了可视化的集成开发环境,包括AppWizard、WorkSpace、ClassWizard和WizardBar等实用开发工具。学习了本章你将了解这些实用工具的使用,并熟悉集成开发平台的基本操作,学会一些简单的程序调试手段。
recommend-type

VC++6.0教程电子版-word

VC++6.0教程是大学授课教师的word电子版讲稿。对学习VC的人士来说,非常不错,欢迎大家支持。
recommend-type

项目从vc++6.0转换到vc++ 2008

两年以前用vc++6.0写的一个项目,把它迁移动到vc++ 2008开发环境下。直接用vs2008打开原来的.dsp文件,提示转换,重新生成时,需要保存.sln文件,表示新的ide按解决方案管理,而不仅仅是一个工程文件
recommend-type

Vc++6.0和MSDN的安装

这是我总结了三天的结果,网上很少有vc++6.0和MSDN放到一起的安装详细过程,有截图,有文字说明
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。