C++實(shí)現(xiàn)推箱子游戲
一、項(xiàng)目簡(jiǎn)介
用兩天閑余時(shí)間回顧了推箱子這款經(jīng)典的小游戲,目前設(shè)置了5關(guān),只能實(shí)現(xiàn)基本的人物移動(dòng)。判斷勝利條件,其他功能還未實(shí)現(xiàn)(例:撤回到上一步,自由選擇關(guān)卡等),也順便復(fù)習(xí)了C++的相關(guān)知識(shí)。
二、 代碼區(qū)
Class Map(地圖類(lèi))
Map.h:
#pragma once
#define N 10
#define M 10
//地圖類(lèi)
class Map
{
public:
Map();
~Map();
void Init();
void ReadMapFile(int map[M][N], int size,const char* filename );
void WriteMapFile(int map[M][N], int size, const char* filename);
private:
};
Map.cpp:
#include "Map.h"
#include<iostream>
#include<fstream>
using namespace std;
Map::Map()
{
}
//地圖初始化方法
void Map::Init()
{
int Map[10][10] =
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 4, 3, 0, 1, 1, 1, 1 },
{ 1, 0, 4, 3, 4, 3, 0, 0, 1, 1 },
{ 1, 7, 3, 4, 3, 4, 2, 0, 1, 1 },
{ 1, 0, 4, 3, 4, 3, 0, 1, 1, 1 },
{ 1, 0, 0, 4, 3, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
};
WriteMapFile(Map, 10, "map/map_05.txt");
}
//讀取地圖文件
void Map::ReadMapFile(int map[M][N], int size, const char* filename)
{
FILE* pfile = nullptr;
fopen_s(&pfile, filename, "rb");
fread(map, 10 * size * 4, 1, pfile);
fclose(pfile);
}
//寫(xiě)入地圖文件
void Map::WriteMapFile(int map[M][N], int size, const char* filename)
{
FILE* pfile = nullptr;
fopen_s(&pfile, filename, "wb");
fwrite(map, 10 * size * 4, 1, pfile);
fclose(pfile);
}
Map::~Map()
{
}
Class Game (游戲類(lèi))
Game.h:
#define _GAEM_H__
#ifdef _GAEM_H__
#include <iostream>
using namespace std;
#include <string.h>
#include <conio.h>
#pragma warning (disable:4996)
#define N 10
#define M 10
/***************************建立一個(gè)推箱子相關(guān)操作的類(lèi)***********************/
/*--------------------------Game類(lèi)編寫(xiě)-----------------------------------*/
/****************************************************************************/
class Game
{
public:
int Move(int map[M][N], char ch);
void Drop(int map[M][N],int c);
int juide(int map[M][N]);
private:
int push(int map[M][N], int offsetX,int offsetY);
void Postion(int map[M][N]);
int posX;
int posY;
};
#endif /*_GAME_H__*/
Game.cpp:
#include "Game.h"
//按鍵控制人物移動(dòng)
int Game::Move(int map[M][N], char ch)
{
static int step = 0;
int offsetx = 0;
int offsety = 0;
switch (ch)
{
//向上移動(dòng)
case 'w':case 'W':
offsetx = -1;
offsety = 0;
if (push(map, offsetx, offsety) == 1)
step++;
break;
//向下移動(dòng)
case 's':case 'S':
offsetx = 1;
offsety = 0;
if (push(map, offsetx, offsety) == 1)
step++;
break;
//向左移動(dòng)
case 'a':case 'A':
offsetx = 0;
offsety = -1;
if (push(map, offsetx, offsety) == 1)
step++;
break;
//向右移動(dòng)
case 'd':case 'D':
offsetx = 0;
offsety = 1;
if (push(map, offsetx, offsety) == 1)
step++;
break;
default:
break;
}
return step;
}
//界面打印
void Game::Drop(int map[M][N], int c)
{
cout <<"\t\t"<<"**********************第 "<<c<<" 關(guān)**************************" << endl;
cout <<"\t\t"<<"***************W-w:向上 S-s:向下*****************" << endl;
cout <<"\t\t"<<"***************A-a:向左 D-d:向右*****************" << endl;
cout << endl;
for (int i = 0; i < M; i++)
{
cout << " ";
for (int j = 0; j < N; j++)
{
switch (map[i][j])
{
//打印空地
case 0:
cout << " ";
break;
//打印墻壁
case 1:
cout << "■";
break;
//打印玩家
case 2:
cout << "♀";
posX = i;
posY = j;
break;
//打印箱子
case 3:
cout << "□";
break;
//打印終點(diǎn)
case 4:
cout << "○";
break;
//人 + 終點(diǎn)
case 6:
cout << "★";
posX = i;
posY = j;
break;
//箱子 + 終點(diǎn)
case 7:
cout << "●";
break;
default:
break;
}
}
cout << endl; //換行
}
}
//判斷游戲勝利條件
int Game::juide(int map[M][N])
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (4 == map[i][j] || 6 == map[i][j]) //地圖中還存在終點(diǎn)/終點(diǎn)+人
return 1;
}
}
return 0;
}
//更新游戲
int Game::push(int map[M][N], int offsetX, int offsetY)
{
Postion(map); //確定人物坐標(biāo)
if (map[posX + offsetX][posY + offsetY] == 0) //下一格是空地
{
map[posX][posY] -= 2; //上一格變?yōu)榭盏鼗蚪K點(diǎn)
map[posX + offsetX][posY + offsetY] += 2; //下一格變?yōu)槿嘶蛉?終點(diǎn)
//改變?nèi)说淖鴺?biāo)
posX += offsetX;
posY += offsetY;
}
else if (map[posX + offsetX][posY + offsetY] == 3) //下一格是箱子
{
if (map[posX + offsetX * 2][posY + offsetY * 2] == 0
|| map[posX + offsetX * 2][posY + offsetY * 2] == 4) //下兩格是空地/終點(diǎn)
{
map[posX][posY] -= 2; //上一格變?yōu)榭盏?終點(diǎn)
map[posX + offsetX][posY + offsetY] = 2; //下一格變?yōu)槿?
map[posX + offsetX * 2][posY + offsetY * 2] += 3; //下兩格變?yōu)橄渥?箱子+終點(diǎn)
posX += offsetX;
posY += offsetY;
}
}
else if (map[posX + offsetX][posY + offsetY] == 4) //下一格是終點(diǎn)
{
map[posX][posY] -= 2; //上一格變?yōu)榭盏?終點(diǎn)
map[posX + offsetX][posY + offsetY] = 6; //下一格變?yōu)槿?終點(diǎn)
posX += offsetX;
posY += offsetY;
}
else if (map[posX + offsetX][posY + offsetY] == 7) //下一格是箱子+終點(diǎn)
{
if (map[posX + offsetX * 2][posY + offsetY * 2] == 0
|| map[posX + offsetX * 2][posY + offsetY * 2] == 4) //下兩格是空地/終點(diǎn)
{
map[posX][posY] -= 2; //上一格變?yōu)榭盏?終點(diǎn)
map[posX + offsetX][posY + offsetY] = 6; //下一格變?yōu)槿?終點(diǎn)
map[posX + offsetX * 2][posY + offsetY * 2] += 3; //下兩格變?yōu)橄渥?箱子+終點(diǎn)
posX += offsetX;
posY += offsetY;
}
}
else //人物不能移動(dòng)
return 0;
return 1;
}
//找到人物坐標(biāo)
void Game::Postion(int map[M][N])
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (2 == map[i][j] || 6 == map[i][j]) //地圖中存在終點(diǎn)/終點(diǎn)+人
{
//給人物坐標(biāo)賦值
posX = i;
posY = j;
}
}
}
}
Main:
#include<iostream>
#include<string.h>
using namespace std;
#pragma warning (disable:4996)
#define M 10
#define N 10
//定義一個(gè)10*10地圖,1表示墻,0表示空地,2表示人
//3表示箱子,4表示成功點(diǎn)
//1.人物可以站到成功點(diǎn)中,顯示人
//2.箱子推入成功點(diǎn)后,可以推出來(lái)
//3.記錄步數(shù),顯示在控制臺(tái)上
//4.界面:提示(■代表墻....)/游戲開(kāi)始界面
//5.最終提示全部推入,提示成功
//周?chē)际菈?中間都是空地
#include"Map.h"
#include"Game.h"
int main()
{
Map _map;
//_map.Init();
int map[M][N];
char filename[] = "map/map_0";
int custom = 2;
while (custom <= 5)
{
char buffer[80];
sprintf(buffer, "%s%d", filename, custom); //連接filename和custom,以字符串保存到buffer中
strcat(buffer, ".txt"); //字符串連接
_map.ReadMapFile(map, N, buffer);
Game game;
int step = 0;
while (game.juide(map)) //游戲勝利,跳出循環(huán)
{
system("cls");
game.Drop(map, custom);
char ch = _getch(); //按鍵輸入
step = game.Move(map, ch);
system("cls");
}
custom++; //關(guān)卡+1
cout << "你贏了!" << endl;
cout << "共走:" << step << "步" << endl;;
system("pause");
}
return 0;
}
三、實(shí)現(xiàn)效果
項(xiàng)目目錄圖片

地圖文件圖片

實(shí)現(xiàn)效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入學(xué)習(xí)C語(yǔ)言中的函數(shù)指針和左右法則
這篇文章主要介紹了深入學(xué)習(xí)C語(yǔ)言中的函數(shù)指針和左右法則,左右法則是一種常用的C指針聲明,需要的朋友可以參考下2015-08-08
VisualStudio 使用Visual Leak Detector檢查內(nèi)存泄漏
這篇文章主要介紹了VisualStudio 使用Visual Leak Detector檢查內(nèi)存泄漏的相關(guān)資料,需要的朋友可以參考下2015-07-07
C語(yǔ)言實(shí)現(xiàn)倒置字符串的兩種方法分享
這篇文章主要和大家詳細(xì)介紹了利用C語(yǔ)言實(shí)現(xiàn)倒置字符串的兩種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-08-08
編寫(xiě)C++程序使DirectShow進(jìn)行視頻捕捉
這篇文章主要介紹了如何編寫(xiě)C++程序來(lái)使DirectShow進(jìn)行視頻捕捉的方法,DirectShow是微軟公司在ActiveMovie和Video for Windows的基礎(chǔ)上推出的新一代基于COM(Component Object Model)的流媒體處理的開(kāi)發(fā)包,要的朋友可以參考下2016-03-03
Recommended C Style and Coding Standards中文翻譯版
本文翻譯自Recommended C Style and Coding Standards(C語(yǔ)言編碼風(fēng)格和標(biāo)準(zhǔn)),需要的朋友可以參考下2014-04-04
Qt中QtWebEngine加載本地網(wǎng)頁(yè)跨域問(wèn)題的總結(jié)
本文主要介紹了Qt中QtWebEngine加載本地網(wǎng)頁(yè)跨域問(wèn)題的總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)單鏈表接口函數(shù)全面講解教程
這篇文章主要為大家介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)單鏈表所有接口函數(shù)的全面講解教程,有需要朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-10-10

