如何用C语言编写一个长尾词形式的推箱子小游戏?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1617个文字,预计阅读时间需要7分钟。
本文以实例分享如何使用C++实现推箱子小游戏的整体代码,供大家参考。游戏效果简单易懂,适合初学者学习。
游戏效果:+ 推箱子到指定位置+ 简单的关卡设计+ 直观的界面操作
游戏代码:
cpp#include #include #include
using namespace std;
// 定义游戏关卡struct Level { vector grid; // 游戏地图 int playerX, playerY; // 玩家位置 int boxX, boxY; // 箱子位置 int targetX, targetY; // 目标位置};
// 初始化关卡void initLevel(Level& level) { // 关卡地图 level.grid={ W...W, .X...X, ......, ......, ...... }; // 玩家、箱子、目标位置 level.playerX=1; level.playerY=1; level.boxX=2; level.boxY=1; level.targetX=4; level.targetY=1;}
// 移动玩家void movePlayer(Level& level, int dx, int dy) { int newX=level.playerX + dx; int newY=level.playerY + dy;
// 检查新位置是否有效 if (newX >=0 && newX =0 && newY // 移动玩家 if (newCell=='.') { level.playerX=newX; level.playerY=newY; } else if (newCell=='X') { // 移动箱子 if (newX==level.boxX && newY==level.boxY) { level.boxX=newX + dx; level.boxY=newY + dy; } } }} // 检查游戏是否胜利bool checkWin(Level& level) { return level.boxX==level.targetX && level.boxY==level.targetY;} // 打印游戏地图void printLevel(const Level& level) { for (const string& row : level.grid) { cout < int main() { Level level; initLevel(level); while (true) { printLevel(level); // 获取玩家输入 int dx, dy; cout <> dx >> dy; // 移动玩家 movePlayer(level, dx, dy); // 检查游戏是否胜利 if (checkWin(level)) { cout << 恭喜,你赢了! < return 0;} 以上代码实现了一个简单的推箱子小游戏,包含游戏关卡、玩家移动、胜利条件检查等功能。希望对大家有所帮助! 本文实例为大家分享了C++实现推箱子小游戏的具体代码,供大家参考,具体内容如下 游戏效果 简单易懂的推箱子闯关小游戏。 游戏代码
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
#define VERSION "2.2"
#define M 55
int n, m, wall[M][M], hole[M][M], box[M][M];
int step, dct, query, cross, rx[233], ry[233];
char str[M][M], title[M], o;
char atlas[M][M][M] = {
{"...#@.","@..*..","#*##..","..##*#","..X.&.",".@#..."},
{"########...####","########..*####","########*....##","######.*..*..##"
,"@@..##.###.#...","@@.X......*..*.","@@..#.####.####","#####......####"},
{"####..#...##","##.*..*.#.##","...#.**#....","X*.....#*##.","#.*###**....","##..##.#*..."
,"###@@@.#.*#.","###@@@@@#.*.","####@@@@@...","#######.#*.#","#######....#","#######...##"},
{"..@*.##",".@*@*..","&*@*@X.",".@*@*.#","..@*..#"}
};
int A[M] = {6, 8, 12, 5}, B[M] = {6, 15, 12, 7};
struct pos {
int x, y;
} player;
struct node {
pos man;
int dct;
vector<pos> box;
node() {
box.clear ();
}
} rec[M * M * M];
void color (int x);
void clean ();
bool check (int x, int y, int cross);
bool forward (int rx, int ry);
bool win ();
void pt ();
void update ();
void playing ();
void in ();
void pass ();
void Init ();
void remain ();
int main() {
MessageBox (NULL, "欢迎来到推箱子游戏!", "温馨提示", MB_OK);
Init ();
while (true) {
remain ();
}
return 0;
}
void color (int x) {
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), x);
}
void clean () {
system("cls");
color(7);
}
bool check (int x, int y, int cross) {
if (!cross) {
return x < 1 || x > n || y < 1 || y > m || wall[x][y];
}
return x < 0 || x > n + 1 || y < 0 || y > m + 1;
}
bool forward (int rx, int ry) {
int x = player.x + rx, y = player.y + ry, X = x + rx, Y = y + ry;
if (check (x,y,cross)) {
return false;
}
if(box[x][y]) {
if (check (X, Y, 0) || box[X][Y]) {
return false;
}
}
return true;
}
bool win () {
for (int i = 0; i < rec[step].box.size (); i++) {
if (!hole[rec[step].box[i].x][rec[step].box[i].y]) {
return false;
}
}
return true;
}
void pt () {
memset (box, 0, sizeof (box));
for (int i = 0; i < rec[step].box.size (); i++) {
box[rec[step].box[i].x][rec[step].box[i].y] = 1;
}
player.x = rec[step].man.x;
player.y = rec[step].man.y;
dct = rec[step].dct;
clean ();
color (154);
puts ("按方向键进行移动,按删除键进行撤销");
puts ("按空格键查询步数。");
puts ("按0返回,按Esc键退出游戏");
color (7);
for (int i = 0; i <= n + 1; i++) {
printf(" ");
for (int j = 0; j <= m + 1; j++) {
if (i == player.x && j == player.y) {
color (15);
if (check (i, j, 0)) {
color (63);
}
printf ("♀");
color (7);
} else if (i == 0 || i == n + 1 || j == 0 || j == m + 1 || wall[i][j]) {
color (3);
printf ("■");
} else if(box[i][j]) {
color (14);
if (hole[i][j]) {
color (12);
}
printf ("▓");
} else if (hole[i][j]) {
color (3);
printf ("※");
} else {
printf (" ");
}
}
puts ("");
}
color (7);
}
void update () {
node temp;
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (box[i][j]) {
pos po;
po.x = i;
po.y = j;
temp.box.push_back (po);
}
}
}
temp.man.x = player.x;
temp.man.y = player.y;
temp.dct = dct;
rec[step] = temp;
}
void playing () {
dct = 72;
step = 0;
update ();
pt ();
int winstep = -1;
while (o = getch ()) {
int tp = 0;
if (o == 72 || o == 77 || o == 80 || o == 75) {
if (forward (rx[o],ry[o])) {
int x = player.x + rx[o], y = player.y + ry[o];
if (box[x][y]) {
box[x][y] = 0;
box[x + rx[o]][y + ry[o]] = 1;
}
player.x = x;
player.y = y;
step++;
} else {
tp = 1;
}
dct = o;
update ();
} else if (o == 8) {
tp = 3;
step = max (0, step - 1);
if (step <= winstep) {
winstep = -1;
}
} else if (o == 48) {
break;
}
else if (o == 27) {
exit (0);
}
else if (o == 32) {
query ^= 1;
}
else {
tp = 2;
}
pt ();
color (154);
if (query) {
printf ("当前步数为%d!\n", step);
}
if(win () || winstep != -1) {
if (winstep == -1) {
winstep = step;
}
printf ("恭喜您,您赢了!共用了%d步。\n", winstep);
MessageBox (NULL, "恭喜您,您赢了!", "温馨提示", MB_OK);
} else if (tp == 1) {
color (207);
puts("对不起,您无法推动这个方块!");
} else if (tp == 2) {
color (207);
} else if (tp == 3) {
puts ("撤销成功!");
}
color (7);
}
}
void in () {
memset (wall, 0, sizeof (wall));
memset (hole, 0, sizeof(hole));
memset (box, 0, sizeof(box));
clean ();
puts ("第一行输入两个整数n和m,表示地图的大小");
puts ("接下来n行,每行m个元素。");
puts ("'.'表示空地");
puts ("'#'表示墙");
puts ("'*'表示箱子");
puts ("'@'表示洞");
puts ("'X'表示人" );
puts ("'&'表示箱子已在洞上");
scanf ("%d %d", &n, &m);
int i,j;
for (i = 1; i <= n; i++) {
scanf ("%s", str[i] + 1);
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
o = str[i][j];
if (o == 'X') {
player.x = i;
player.y = j;
}
if (o == '#') {
wall[i][j] = 1;
}
if (o == '@' || o == '&') {
hole[i][j] = 1;
}
if (o == '*' || o =='&') {
box[i][j] = 1;
}
}
}
playing ();
}
void pass () {
memset (wall, 0, sizeof (wall));
memset (hole, 0, sizeof (hole));
memset (box, 0, sizeof (box));
clean ();
puts ("1.第一关");
puts ("2.第二关");
puts ("3.第三关");
puts ("4.第四关");
puts ("\n0.返回");
puts ("Esc.退出游戏");
while (o = getch ()) {
if (o >= '1' && o <= '4') {
int id = o - 48 - 1;
n = A[id];
m = B[id];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char o = atlas[id][i - 1][j - 1];
if (o == 'X') {
player.x = i;
player.y = j;
}
if (o == '#') {
wall[i][j] = 1;
}
if (o == '@' || o == '&') {
hole[i][j] = 1;
}
if (o == '*' || o =='&') {
box[i][j] = 1;
}
}
}
playing ();
break;
} else if (o == 48) {
break;
}
}
}
void Init () {
system ("mode con cols=40 lines=20");
SetConsoleTitle ("推箱子");
rx[72] = -1;
rx[80] = 1;
ry[77] = 1;
ry[75] = -1;
}
void remain () {
clean ();
puts ("1.闯关模式");
puts ("2.输入模式");
puts ("Esc.退出游戏");
while (o = getch ()) {
if (o=='1') {
pass ();
break;
} else if (o == '2') {
in ();
break;
} else if (o == 27) {
exit (0);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计1617个文字,预计阅读时间需要7分钟。
本文以实例分享如何使用C++实现推箱子小游戏的整体代码,供大家参考。游戏效果简单易懂,适合初学者学习。
游戏效果:+ 推箱子到指定位置+ 简单的关卡设计+ 直观的界面操作
游戏代码:
cpp#include #include #include
using namespace std;
// 定义游戏关卡struct Level { vector grid; // 游戏地图 int playerX, playerY; // 玩家位置 int boxX, boxY; // 箱子位置 int targetX, targetY; // 目标位置};
// 初始化关卡void initLevel(Level& level) { // 关卡地图 level.grid={ W...W, .X...X, ......, ......, ...... }; // 玩家、箱子、目标位置 level.playerX=1; level.playerY=1; level.boxX=2; level.boxY=1; level.targetX=4; level.targetY=1;}
// 移动玩家void movePlayer(Level& level, int dx, int dy) { int newX=level.playerX + dx; int newY=level.playerY + dy;
// 检查新位置是否有效 if (newX >=0 && newX =0 && newY // 移动玩家 if (newCell=='.') { level.playerX=newX; level.playerY=newY; } else if (newCell=='X') { // 移动箱子 if (newX==level.boxX && newY==level.boxY) { level.boxX=newX + dx; level.boxY=newY + dy; } } }} // 检查游戏是否胜利bool checkWin(Level& level) { return level.boxX==level.targetX && level.boxY==level.targetY;} // 打印游戏地图void printLevel(const Level& level) { for (const string& row : level.grid) { cout < int main() { Level level; initLevel(level); while (true) { printLevel(level); // 获取玩家输入 int dx, dy; cout <> dx >> dy; // 移动玩家 movePlayer(level, dx, dy); // 检查游戏是否胜利 if (checkWin(level)) { cout << 恭喜,你赢了! < return 0;} 以上代码实现了一个简单的推箱子小游戏,包含游戏关卡、玩家移动、胜利条件检查等功能。希望对大家有所帮助! 本文实例为大家分享了C++实现推箱子小游戏的具体代码,供大家参考,具体内容如下 游戏效果 简单易懂的推箱子闯关小游戏。 游戏代码
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
#define VERSION "2.2"
#define M 55
int n, m, wall[M][M], hole[M][M], box[M][M];
int step, dct, query, cross, rx[233], ry[233];
char str[M][M], title[M], o;
char atlas[M][M][M] = {
{"...#@.","@..*..","#*##..","..##*#","..X.&.",".@#..."},
{"########...####","########..*####","########*....##","######.*..*..##"
,"@@..##.###.#...","@@.X......*..*.","@@..#.####.####","#####......####"},
{"####..#...##","##.*..*.#.##","...#.**#....","X*.....#*##.","#.*###**....","##..##.#*..."
,"###@@@.#.*#.","###@@@@@#.*.","####@@@@@...","#######.#*.#","#######....#","#######...##"},
{"..@*.##",".@*@*..","&*@*@X.",".@*@*.#","..@*..#"}
};
int A[M] = {6, 8, 12, 5}, B[M] = {6, 15, 12, 7};
struct pos {
int x, y;
} player;
struct node {
pos man;
int dct;
vector<pos> box;
node() {
box.clear ();
}
} rec[M * M * M];
void color (int x);
void clean ();
bool check (int x, int y, int cross);
bool forward (int rx, int ry);
bool win ();
void pt ();
void update ();
void playing ();
void in ();
void pass ();
void Init ();
void remain ();
int main() {
MessageBox (NULL, "欢迎来到推箱子游戏!", "温馨提示", MB_OK);
Init ();
while (true) {
remain ();
}
return 0;
}
void color (int x) {
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), x);
}
void clean () {
system("cls");
color(7);
}
bool check (int x, int y, int cross) {
if (!cross) {
return x < 1 || x > n || y < 1 || y > m || wall[x][y];
}
return x < 0 || x > n + 1 || y < 0 || y > m + 1;
}
bool forward (int rx, int ry) {
int x = player.x + rx, y = player.y + ry, X = x + rx, Y = y + ry;
if (check (x,y,cross)) {
return false;
}
if(box[x][y]) {
if (check (X, Y, 0) || box[X][Y]) {
return false;
}
}
return true;
}
bool win () {
for (int i = 0; i < rec[step].box.size (); i++) {
if (!hole[rec[step].box[i].x][rec[step].box[i].y]) {
return false;
}
}
return true;
}
void pt () {
memset (box, 0, sizeof (box));
for (int i = 0; i < rec[step].box.size (); i++) {
box[rec[step].box[i].x][rec[step].box[i].y] = 1;
}
player.x = rec[step].man.x;
player.y = rec[step].man.y;
dct = rec[step].dct;
clean ();
color (154);
puts ("按方向键进行移动,按删除键进行撤销");
puts ("按空格键查询步数。");
puts ("按0返回,按Esc键退出游戏");
color (7);
for (int i = 0; i <= n + 1; i++) {
printf(" ");
for (int j = 0; j <= m + 1; j++) {
if (i == player.x && j == player.y) {
color (15);
if (check (i, j, 0)) {
color (63);
}
printf ("♀");
color (7);
} else if (i == 0 || i == n + 1 || j == 0 || j == m + 1 || wall[i][j]) {
color (3);
printf ("■");
} else if(box[i][j]) {
color (14);
if (hole[i][j]) {
color (12);
}
printf ("▓");
} else if (hole[i][j]) {
color (3);
printf ("※");
} else {
printf (" ");
}
}
puts ("");
}
color (7);
}
void update () {
node temp;
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (box[i][j]) {
pos po;
po.x = i;
po.y = j;
temp.box.push_back (po);
}
}
}
temp.man.x = player.x;
temp.man.y = player.y;
temp.dct = dct;
rec[step] = temp;
}
void playing () {
dct = 72;
step = 0;
update ();
pt ();
int winstep = -1;
while (o = getch ()) {
int tp = 0;
if (o == 72 || o == 77 || o == 80 || o == 75) {
if (forward (rx[o],ry[o])) {
int x = player.x + rx[o], y = player.y + ry[o];
if (box[x][y]) {
box[x][y] = 0;
box[x + rx[o]][y + ry[o]] = 1;
}
player.x = x;
player.y = y;
step++;
} else {
tp = 1;
}
dct = o;
update ();
} else if (o == 8) {
tp = 3;
step = max (0, step - 1);
if (step <= winstep) {
winstep = -1;
}
} else if (o == 48) {
break;
}
else if (o == 27) {
exit (0);
}
else if (o == 32) {
query ^= 1;
}
else {
tp = 2;
}
pt ();
color (154);
if (query) {
printf ("当前步数为%d!\n", step);
}
if(win () || winstep != -1) {
if (winstep == -1) {
winstep = step;
}
printf ("恭喜您,您赢了!共用了%d步。\n", winstep);
MessageBox (NULL, "恭喜您,您赢了!", "温馨提示", MB_OK);
} else if (tp == 1) {
color (207);
puts("对不起,您无法推动这个方块!");
} else if (tp == 2) {
color (207);
} else if (tp == 3) {
puts ("撤销成功!");
}
color (7);
}
}
void in () {
memset (wall, 0, sizeof (wall));
memset (hole, 0, sizeof(hole));
memset (box, 0, sizeof(box));
clean ();
puts ("第一行输入两个整数n和m,表示地图的大小");
puts ("接下来n行,每行m个元素。");
puts ("'.'表示空地");
puts ("'#'表示墙");
puts ("'*'表示箱子");
puts ("'@'表示洞");
puts ("'X'表示人" );
puts ("'&'表示箱子已在洞上");
scanf ("%d %d", &n, &m);
int i,j;
for (i = 1; i <= n; i++) {
scanf ("%s", str[i] + 1);
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
o = str[i][j];
if (o == 'X') {
player.x = i;
player.y = j;
}
if (o == '#') {
wall[i][j] = 1;
}
if (o == '@' || o == '&') {
hole[i][j] = 1;
}
if (o == '*' || o =='&') {
box[i][j] = 1;
}
}
}
playing ();
}
void pass () {
memset (wall, 0, sizeof (wall));
memset (hole, 0, sizeof (hole));
memset (box, 0, sizeof (box));
clean ();
puts ("1.第一关");
puts ("2.第二关");
puts ("3.第三关");
puts ("4.第四关");
puts ("\n0.返回");
puts ("Esc.退出游戏");
while (o = getch ()) {
if (o >= '1' && o <= '4') {
int id = o - 48 - 1;
n = A[id];
m = B[id];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char o = atlas[id][i - 1][j - 1];
if (o == 'X') {
player.x = i;
player.y = j;
}
if (o == '#') {
wall[i][j] = 1;
}
if (o == '@' || o == '&') {
hole[i][j] = 1;
}
if (o == '*' || o =='&') {
box[i][j] = 1;
}
}
}
playing ();
break;
} else if (o == 48) {
break;
}
}
}
void Init () {
system ("mode con cols=40 lines=20");
SetConsoleTitle ("推箱子");
rx[72] = -1;
rx[80] = 1;
ry[77] = 1;
ry[75] = -1;
}
void remain () {
clean ();
puts ("1.闯关模式");
puts ("2.输入模式");
puts ("Esc.退出游戏");
while (o = getch ()) {
if (o=='1') {
pass ();
break;
} else if (o == '2') {
in ();
break;
} else if (o == 27) {
exit (0);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

