如何用C语言编写推箱子游戏程序?

2026-04-29 17:594阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计2909个文字,预计阅读时间需要12分钟。

如何用C语言编写推箱子游戏程序?

本例展示了如何用C语言实现推箱子游戏的总体代码,供大家参考。代码在VS2013上测试运行。

想法:1)地图使用二维数组实现,箱子和墙壁等不同物品用不同的数字表示。代码如下:

c#include #include

#define MAXROW 10#define MAXCOL 10

// 地图初始化函数void InitMap(char map[][MAXCOL], int rows, int cols) { // 空地图 for (int i=0; i

// 打印地图函数void PrintMap(char map[][MAXCOL], int rows, int cols) { for (int i=0; i

int main() { char map[MAXROW][MAXCOL]; int rows=MAXROW; int cols=MAXCOL;

InitMap(map, rows, cols); PrintMap(map, rows, cols);

return 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语言编写推箱子游戏程序?

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

本文共计2909个文字,预计阅读时间需要12分钟。

如何用C语言编写推箱子游戏程序?

本例展示了如何用C语言实现推箱子游戏的总体代码,供大家参考。代码在VS2013上测试运行。

想法:1)地图使用二维数组实现,箱子和墙壁等不同物品用不同的数字表示。代码如下:

c#include #include

#define MAXROW 10#define MAXCOL 10

// 地图初始化函数void InitMap(char map[][MAXCOL], int rows, int cols) { // 空地图 for (int i=0; i

// 打印地图函数void PrintMap(char map[][MAXCOL], int rows, int cols) { for (int i=0; i

int main() { char map[MAXROW][MAXCOL]; int rows=MAXROW; int cols=MAXCOL;

InitMap(map, rows, cols); PrintMap(map, rows, cols);

return 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语言编写推箱子游戏程序?

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