在处理移动的时候有太多种情况了:

1、有空位

2、在推箱子,推到了空地

3、推箱子推到了目标,

4、推目标位的箱子推到另一个目标

5、推目标位的箱子推到空地

首先记录目标位置,在每次推动之后会再绘画中自动修正目标位置的情况

修改地图之后发现不能将上述情况的5实现,还要再改。

接下来应该尝试如何实现多个关卡或者从文件中读取地图信息

#include<iostream>
#include<graphics.h>
#include<conio.h>
#include<vector>
using namespace std;
//0空白,1墙,2人,3箱子,4目标,5箱子与目标,6人与目标
int x, y,temp;
vector<int>goalx, goaly;
int mapp[][] = {,,,,, ,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,,, ,
,,,,, ,,,,,,,,,,,,,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,,,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,, ,
,,,, ,,,,,,,, ,,,, ,,,, ,,,, ,,, ,,,, ,,,, ,,,,
};
void scann() {
int i, j;
for (i = ; i < ; i++) {
for (j = ; j < ; j++) {
if (mapp[i][j] >= ) {
goalx.push_back(i);
goaly.push_back(j);
}
}
}
}
void drawmapp() {
int i, j,k;
for (i = ; i < ; i++) {
for (j = ; j < ; j++) {
for (k = ; k < goalx.size(); k++) {
if (i == goalx[k] && j == goaly[k]) {
if (mapp[i][j] == ) {
mapp[i][j] = ;
}
if (mapp[i][j] == ) {
mapp[i][j] = ;
}
if (mapp[i][j] == ) {
mapp[i][j] = ;
} }
}
switch (mapp[i][j]) {
case :
setlinecolor(RGB(,,));
setfillcolor(RGB(, , ));
fillrectangle(j * , i * , j * + , i * + );
break;
case :
x = i;
y = j;
setlinecolor(RGB(, , ));
setfillcolor(RGB(, , ));
fillrectangle(j * , i * , j * + , i * + );
break;
case :
setlinecolor(RGB(, , ));
setfillcolor(RGB(, , ));
fillrectangle(j * , i * , j * + , i * + );
break;
case :
setlinecolor(RGB(, , ));
setfillcolor(RGB(, , ));
fillrectangle(j * , i * , j * + , i * + );
break;
case :
setlinecolor(RGB(, , ));
setfillcolor(RGB(, , ));
fillrectangle(j * , i * , j * + , i * + );
break;
case :
x = i;
y = j;
setlinecolor(RGB(, , ));
setfillcolor(RGB(, , ));
fillrectangle(j * , i * , j * + , i * + );
break;
}
}
}
}
void keyDown() {
char ch = '\0';
ch = _getch();
switch (ch)
{
case 'w':
case 'W':
if (mapp[x - ][y] == ) {
temp = mapp[x][y];
mapp[x][y] = mapp[x - ][y];
mapp[x - ][y] =;
}
if (mapp[x - ][y] == ) {
mapp[x][y] = ;
mapp[x - ][y] = ;
}
if (mapp[x - ][y] == && mapp[x-][y]==) {
mapp[x-][y] = ;
mapp[x - ][y] = ;
mapp[x][y] = ;
}
if (mapp[x - ][y] == && mapp[x - ][y] == ) {
mapp[x - ][y] = ;
mapp[x - ][y] = ;
mapp[x][y] = ;
}
if (mapp[x - ][y] == && mapp[x - ][y] == ) {
mapp[x - ][y] = ;
mapp[x - ][y] = ;
mapp[x][y] = ;
}
if (mapp[x - ][y] == && mapp[x - ][y] == ) {
mapp[x - ][y] = ;
mapp[x - ][y] = ;
mapp[x][y] = ;
} break;
case 'a':
case 'A':
if (mapp[x][y-] == ) {
temp = mapp[x][y];
mapp[x][y] = mapp[x][y-];
mapp[x][y-] =;
}
if (mapp[x][y-] == ) {
mapp[x][y] = ;
mapp[x][y-] = ;
}
if (mapp[x][y-] == && mapp[x][y-] == ) {
mapp[x][y-] = ;
mapp[x][y-] = ;
mapp[x][y] = ;
}
if (mapp[x][y - ] == && mapp[x][y - ] == ) {
mapp[x][y - ] = ;
mapp[x][y - ] = ;
mapp[x][y] = ;
}
if (mapp[x][y - ] == && mapp[x][y - ] == ) {
mapp[x][y - ] = ;
mapp[x][y - ] = ;
mapp[x][y] = ;
}
if (mapp[x][y - ] == && mapp[x][y - ] ==) {
mapp[x][y - ] = ;
mapp[x][y - ] = ;
mapp[x][y] = ;
}
break;
case 's':
case 'S':
if (mapp[x + ][y] == ) {
temp = mapp[x][y];
mapp[x][y] = mapp[x + ][y];
mapp[x + ][y] =;
}
if (mapp[x + ][y] == ) {
mapp[x][y] = ;
mapp[x + ][y] = ;
}
if (mapp[x + ][y] == && mapp[x + ][y] == ) {
mapp[x + ][y] = ;
mapp[x + ][y] = ;
mapp[x][y] = ;
}
if (mapp[x + ][y] == && mapp[x + ][y] == ) {
mapp[x + ][y] = ;
mapp[x + ][y] = ;
mapp[x][y] = ;
}
if (mapp[x + ][y] == && mapp[x + ][y] == ) {
mapp[x + ][y] = ;
mapp[x + ][y] = ;
mapp[x][y] = ;
}
if (mapp[x + ][y] == && mapp[x + ][y] == ) {
mapp[x + ][y] = ;
mapp[x + ][y] = ;
mapp[x][y] = ;
}
break;
case 'd':
case 'D':
if (mapp[x][y+] == ) {
mapp[x][y] = mapp[x][y+];
mapp[x][y+] =;
}
if (mapp[x][y+] == ) {
mapp[x][y] = ;
mapp[x][y+] = ;
}
if (mapp[x][y+] == && mapp[x][y+] == ) {
mapp[x][y+] = ;
mapp[x][y+] = ;
mapp[x][y] = ;
}
if (mapp[x][y + ] == && mapp[x][y + ] == ) {
mapp[x][y + ] = ;
mapp[x][y + ] = ;
mapp[x][y] = ;
}
if (mapp[x][y + ] == && mapp[x][y + ] == ) {
mapp[x][y + ] = ;
mapp[x][y + ] = ;
mapp[x][y] = ;
}
if (mapp[x][y + ] == && mapp[x][y + ] ==) {
mapp[x][y + ] = ;
mapp[x][y + ] = ;
mapp[x][y] = ;
}
break;
default:
break;
}
}
int main() {
int kl;
bool checkk;
HWND hwnd=initgraph(, );
setbkcolor(RGB(,,));
cleardevice();
drawmapp();
scann();
while (true) {
checkk = true;
settextcolor(BLACK);
outtextxy(, , "WASD控制");
keyDown();
cleardevice();
drawmapp();
for (kl = ; kl < goalx.size(); kl++) {
if (mapp[goalx[kl]][goaly[kl]] != ) checkk = false;
}
if (checkk) {
MessageBox(hwnd, "Finish", "Finish!", );
break;
}
}
system("pause");
return ; }

C++简单项目--推箱子的更多相关文章

  1. 用C写一个简单的推箱子游戏(二)

    下面接着上一篇随笔<用C写一个简单的推箱子游戏(一)>来写 tuidong()函数是用来判断游戏人物前方情况的函数,是推箱子游戏中非常重要的一个函数,下面从它开始继续介绍推箱子的小程序怎么 ...

  2. 用C写一个简单的推箱子游戏(一)

    我现在在读大二,我们有一门课程叫<操作系统>,课程考查要求我们可以写一段程序或者写Windows.iOS.Mac的发展历程.后面我结合网上的资料参考,就想用自己之前简单学过的C写一关的推箱 ...

  3. C++学习(九)(C语言部分)之 项目 推箱子游戏

    游戏制作 推箱子 步骤分析 1.模板 2.模板分析 组成元素: 空地 墙 人 目的地 箱子 背景 3.如何操作 通过WASD键盘操作人,推着箱子,到达目的地,游戏结束,如果箱子卡在死角则游戏失败 4. ...

  4. c++、c实现推箱子小游戏

    经过四次的修改和优化,终于将推箱子这个游戏完整的写出来了,今天就像大家分享一下这个游戏的编写. 这个游戏界面的编写总的来说不困难,主要是推动箱子的算法. (1)利用数组和windows api 即可写 ...

  5. c#部分---网吧充值系统;简易的闹钟;出租车计费;简单计算器;对战游戏;等额本金法计算贷款还款利息等;随机生成10个不重复的50以内的整数;推箱子;

    网吧充值系统namespace ConsoleApplication1 { class Program { struct huiyuan { public string name; public st ...

  6. C/C++编程笔记:C语言写推箱子小游戏,大一学习C语言练手项目

    C语言,作为大多数人的第一门编程语言,重要性不言而喻,很多编程习惯,逻辑方式在此时就已经形成了.这个是我在大一学习 C语言 后写的推箱子小游戏,自己的逻辑能力得到了提升,在这里同大家分享这个推箱子小游 ...

  7. 每个人都可以用C语言写的推箱子小游戏!今天你就可以写出属于自己项目~

    C语言,作为大多数人的第一门编程语言,重要性不言而喻,很多编程习惯,逻辑方式在此时就已经形成了.这个是我在大一学习 C语言 后写的推箱子小游戏,自己的逻辑能力得到了提升,在这里同大家分享这个推箱子小游 ...

  8. 完整版本的推箱子小游戏,最简单的纯C语言打造

    /* 推箱子小游戏 1.定义绘制样式 用二维数组的方式 2.绘制图像 3.找出当前位置 4.逻辑判断,制造动作 根据数学xy轴的规律,这里使用ij 上移,行轴上升,行数减少 下移,行数下降,函数增加 ...

  9. c语言推箱子 扫雷项目

    推箱子 两关的推箱子用到一个三维数组 用到的图片数据如下 https://pan.baidu.com/s/1IDE4GQLo46cxNywDqwxmlQ 密码:jdel 代码如下: #include& ...

随机推荐

  1. Laravel引入第三方库的方法

    https://blog.csdn.net/will5451/article/details/52472695 1.首先在app目录下创建一个新的文件夹,命名libs(可自定义) 2.(可选)考虑到后 ...

  2. poj 1039 Pipe (Geometry)

    1039 -- Pipe 理解错题意一个晚上._(:з」∠)_ 题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里. 正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直 ...

  3. ccf-201403-3有趣的命令行

    傻逼题,要是考试只能得0分.. 提供几组傻逼数据,这要是在真实的生活中一定是错的... 还是要好好读题吧,全凭自己的感觉就得0分 albw:x 4 ls -a docu Case 1: -a ls - ...

  4. Element-ui学习笔记1

    1.col,row布局注意事项 el-row el-col gutter就是css,span的时候宽度是按boder-box来计算. 将 type 属性赋值为 'flex',可以启用 flex 布局, ...

  5. hdu 3466 Proud Merchants(有排序的01背包)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  6. 2019-10-5-dotnet-core-获取-MacAddress-地址方法

    title author date CreateTime categories dotnet core 获取 MacAddress 地址方法 lindexi 2019-10-05 10:44:10 + ...

  7. tf.contrib.layers.xavier_initializer

    https://blog.csdn.net/yinruiyang94/article/details/78354257xavier_initializer( uniform=True, seed=No ...

  8. python基础十一之装饰器进阶

    函数的双下划线方法 def hahahha(): """测试函数""" print('zxc') print(hahahha.__name_ ...

  9. UVA 437 "The Tower of Babylon" (DAG上的动态规划)

    传送门 题意 有 n 种立方体,每种都有无穷多个. 要求选一些立方体摞成一根尽量高的柱子(在摞的时候可以自行选择哪一条边作为高): 立方体 a 可以放在立方体 b 上方的前提条件是立方体 a 的底面长 ...

  10. 关于git命令

    1在自己电脑上生成一个密钥然后给老大,老大在github上设置后,你这台电脑才可以访问他的项目ssh-keygen -t rsa -C <邮件名称> ------->获取秘钥 cat ...