基于C语言实现推箱子游戏

所属分类: 软件编程 / C 语言 阅读数: 98
收藏 0 赞 0 分享

本文实例为大家分享了C语言实现推箱子游戏的具体代码,供大家参考,具体内容如下

代码在vs2013上测试运行。

思想:

1):地图用二维数组实现,箱子、墙壁、人等事物用不同的数字表示,遍历二维数组,遇到不同的数字打印相应的图案即可。
2):按键移动原理:判断要移动的方向是怎样的障碍物,如果理论上可以移动的话,只需把对应位置的数字作相应更改即可。

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>

//二维数组:0表示地图上的空地,1代表墙壁,3代表箱子的目的地,4代表箱子,6代表人,7代表箱子与目的地重合;9代表人在箱子的目的地
int g_map[10][12] = 
{
 { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },

 { 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0 },

 { 1, 0, 4, 0, 1, 0, 1, 1, 1, 1, 1, 1 },

 { 1, 0, 4, 6, 1, 0, 1, 0, 0, 0, 3, 1 },

 { 1, 1, 1, 4, 1, 1, 1, 0, 0, 0, 3, 1 },

 { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 3, 1 },

 { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },

 { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },

 { 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },

 { 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 }
}; //g_开头代表全局变量 m_代表成员变量 n:整形

void drawMap(); //画地图
void up(); //上移
void down(); //下移
void left(); //左移
void right(); //右移
void gameOver(); //结束游戏

POINT GetGamerPostion();//获取玩家坐标

int main()
{
 //设置窗口标题
 SetConsoleTitleA("推箱子");
 //修改窗口大小
 system("mode con cols=30 lines=12");

 while (1)
 {
 //清屏
 system("cls");
 //打印地图
 drawMap();
 gameOver();
 char ch = _getch(); //从控制台获取输入 getchar()函数获取按键后要按enter确认,并且输入的字符要在控制台上显示
 switch (ch)
 {
 case 'w':case 72://上
 up();
 break;
 case 's':case 80://下
 down();
 break;
 case 'a':case 75://左
 left();
 break;
 case 'd':case 77://右
 right();
 break;
 }
 
 }
 //system("pause"); 
 return 0;
}

void drawMap()
{
 for (int i = 0; i < 10; i++)
 {
 for (int j = 0; j < 12; j++)
 {
 switch (g_map[i][j])
 {
 case 0://空地
 printf(" ");
 break;
 case 1://墙壁
 printf("■");
 break;
 case 3://箱子的目的地
 printf("☆");
 break;
 case 4://箱子
 printf("□");
 break;
 case 6://人
 printf("♀");
 break;
 case 7://箱子与目的地重合
 printf("★");
 break;
 case 9://人站在目的地
 printf("♀");
 break;
 }
 }
 printf("\n");
 }
}

void up()
{
 //获取玩家坐标
 POINT pos = GetGamerPostion();
 //1.人的前面是空地
 if (g_map[pos.x - 1][pos.y] == 0)
 {
 g_map[pos.x - 1][pos.y] = 6; //空地变为人
 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0; 
 }

 //2.人的前面是目的地
 if (g_map[pos.x - 1][pos.y] == 3)
 {
 g_map[pos.x - 1][pos.y] = 9; //原来目的地的位置变为人站在目的地
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0; 
 }

 //3.人的前面是箱子,
 if (g_map[pos.x - 1][pos.y] == 4)
 {
 //a.箱子前面是空地
 if (g_map[pos.x - 2][pos.y] == 0)
 {
 g_map[pos.x - 2][pos.y] = 4; //空地变为箱子
 g_map[pos.x - 1][pos.y] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子前面目的地
 if (g_map[pos.x - 2][pos.y] == 3)
 {
 g_map[pos.x - 2][pos.y] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x - 1][pos.y] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }
 
 //4.人的前面是箱子和目的地的重合
 if (g_map[pos.x - 1][pos.y] == 7)
 {
 //a.箱子和目的地的重合前面是空地
 if (g_map[pos.x - 2][pos.y] == 0)
 {
 g_map[pos.x - 2][pos.y] = 4; //空地变为箱子
 g_map[pos.x - 1][pos.y] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子和目的地的重合前面是另一个目的地
 if (g_map[pos.x - 2][pos.y] == 3)
 {
 g_map[pos.x - 2][pos.y] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x - 1][pos.y] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }
}

//下移
void down()
{
 //获取玩家坐标
 POINT pos = GetGamerPostion();
 //1.人的前面是空地
 if (g_map[pos.x + 1][pos.y] == 0)
 {
 g_map[pos.x + 1][pos.y] = 6; //空地变为人
 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }

 //2.人的前面是目的地
 if (g_map[pos.x + 1][pos.y] == 3)
 {
 g_map[pos.x + 1][pos.y] = 9; //原来目的地的位置变为人站在目的地
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }

 //3.人的前面是箱子,
 if (g_map[pos.x + 1][pos.y] == 4)
 {
 //a.箱子前面是空地
 if (g_map[pos.x + 2][pos.y] == 0)
 {
 g_map[pos.x + 2][pos.y] = 4; //空地变为箱子
 g_map[pos.x + 1][pos.y] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子前面目的地
 if (g_map[pos.x + 2][pos.y] == 3)
 {
 g_map[pos.x + 2][pos.y] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x + 1][pos.y] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }

 //4.人的前面是箱子和目的地的重合
 if (g_map[pos.x + 1][pos.y] == 7)
 {
 //a.箱子和目的地的重合前面是空地
 if (g_map[pos.x + 2][pos.y] == 0)
 {
 g_map[pos.x + 2][pos.y] = 4; //空地变为箱子
 g_map[pos.x + 1][pos.y] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子和目的地的重合前面是另一个目的地
 if (g_map[pos.x + 2][pos.y] == 3)
 {
 g_map[pos.x + 2][pos.y] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x + 1][pos.y] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }
}


//右移
void right()
{
 //获取玩家坐标
 POINT pos = GetGamerPostion();
 //1.人的前面是空地
 if (g_map[pos.x][pos.y + 1] == 0)
 {
 g_map[pos.x ][pos.y + 1] = 6; //空地变为人
 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }

 //2.人的前面是目的地
 if (g_map[pos.x][pos.y + 1] == 3)
 {
 g_map[pos.x][pos.y + 1] = 9; //原来目的地的位置变为人站在目的地
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }

 //3.人的前面是箱子,
 if (g_map[pos.x][pos.y + 1] == 4)
 {
 //a.箱子前面是空地
 if (g_map[pos.x][pos.y + 2] == 0)
 {
 g_map[pos.x][pos.y + 2] = 4; //空地变为箱子
 g_map[pos.x][pos.y + 1] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子前面目的地
 if (g_map[pos.x][pos.y + 2] == 3)
 {
 g_map[pos.x][pos.y + 2] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x][pos.y + 1] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }

 //4.人的前面是箱子和目的地的重合
 if (g_map[pos.x][pos.y + 1] == 7)
 {
 //a.箱子和目的地的重合前面是空地
 if (g_map[pos.x][pos.y + 2] == 0)
 {
 g_map[pos.x][pos.y + 2] = 4; //空地变为箱子
 g_map[pos.x][pos.y + 1] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子和目的地的重合前面是另一个目的地
 if (g_map[pos.x][pos.y + 2] == 3)
 {
 g_map[pos.x][pos.y + 2] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x][pos.y + 1] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }
}

//左移
void left()
{
 //获取玩家坐标
 POINT pos = GetGamerPostion();
 //1.人的前面是空地
 if (g_map[pos.x][pos.y - 1] == 0)
 {
 g_map[pos.x][pos.y - 1] = 6; //空地变为人
 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }

 //2.人的前面是目的地
 if (g_map[pos.x][pos.y - 1] == 3)
 {
 g_map[pos.x][pos.y - 1] = 9; //原来目的地的位置变为人站在目的地
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }

 //3.人的前面是箱子,
 if (g_map[pos.x][pos.y - 1] == 4)
 {
 //a.箱子前面是空地
 if (g_map[pos.x][pos.y - 2] == 0)
 {
 g_map[pos.x][pos.y - 2] = 4; //空地变为箱子
 g_map[pos.x][pos.y - 1] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子前面目的地
 if (g_map[pos.x][pos.y - 2] == 3)
 {
 g_map[pos.x][pos.y - 2] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x][pos.y - 1] = 6; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }

 //4.人的前面是箱子和目的地的重合
 if (g_map[pos.x][pos.y - 1] == 7)
 {
 //a.箱子和目的地的重合前面是空地
 if (g_map[pos.x][pos.y - 2] == 0)
 {
 g_map[pos.x][pos.y - 2] = 4; //空地变为箱子
 g_map[pos.x][pos.y - 1] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 //b.箱子和目的地的重合前面是另一个目的地
 if (g_map[pos.x][pos.y - 2] == 3)
 {
 g_map[pos.x][pos.y - 2] = 7; //目的地变为箱子和目的地重合
 g_map[pos.x][pos.y - 1] = 9; //原来箱子位置变为人
 if (g_map[pos.x][pos.y] == 9) //还原人的位置
 {
 g_map[pos.x][pos.y] = 3;
 }
 else
 g_map[pos.x][pos.y] = 0;
 }
 }
}

//结束游戏
void gameOver()
{
 if (g_map[3][10] == 7 && g_map[4][10]==7 && g_map[5][10]==7)
 {
 MessageBox(NULL, L"获得胜利", L"提示",0);
 }
}

//寻找玩家位置
POINT GetGamerPostion()
{
 POINT pos = { -1, -1 }; //表示没有找到玩家
 for (int i = 0; i < 10; i++)
 {
 for (int j = 0; j < 12; j++)
 {
 if (g_map[i][j] == 6 || g_map[i][j] == 9)
 {
 pos.x = i;
 pos.y = j;
 return pos;
 }
 }
 }
 return pos;
}

是不是觉得一点都不美观?和正常的推箱子游戏差太多了?其实很简单,只需要把箱子那些图片准备好,用贴图技术贴在这个框架里就OK啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

利用C语言来求最大连续子序列乘积的方法

这篇文章主要介绍了利用C语言来求最大连续子序列乘积的方法,基本的思路以外文中还附有相关ACM题目,需要的朋友可以参考下
收藏 0 赞 0 分享

用C语言判断一个二叉树是否为另一个的子结构

这篇文章主要介绍了用C语言判断一个二叉树是否为另一个的子结构,是数据结构学习当中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现的阶乘,排列和组合实例

这篇文章主要介绍了C语言实现的阶乘,排列和组合的方法,涉及C语言数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言查找数组里数字重复次数的方法

这篇文章主要介绍了C语言查找数组里数字重复次数的方法,涉及C语言针对数组的遍历与判断技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言简单实现计算字符个数的方法

这篇文章主要介绍了C语言简单实现计算字符个数的方法,涉及C语言针对字符串的简单遍历与判定技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

c实现linux下的数据库备份

本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

C++获得文件状态信息的方法

这篇文章主要介绍了C++获得文件状态信息的方法,包括文件状态信息、文件所在磁盘盘符、文件创建时间、访问时间及修改日期等,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言按关键字搜索文件夹中文件的方法

这篇文章主要介绍了C语言按关键字搜索文件夹中文件的方法,涉及C语言文件操作及字符串查找的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言之字符串模糊查询方法的实现

本篇文章主要为大家介绍字符串模糊查询的C语言程序编写方法,有需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现BMP转换JPG的方法

这篇文章主要介绍了C语言实现BMP转换JPG的方法,涉及C#图片格式转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多