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í)有所幫助,也希望大家多多支持我們。
上一篇:c++11中regex正則表達(dá)式示例簡(jiǎn)述
欄 目:C語(yǔ)言
下一篇:C語(yǔ)言實(shí)現(xiàn)猜數(shù)字小游戲
本文標(biāo)題:C++實(shí)現(xiàn)推箱子游戲
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/140.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
 - 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
 - 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
 - 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
 - 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
 - 01-10C語(yǔ)言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
 - 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
 - 01-10c++中inline的用法分析
 - 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法
 - 01-10深入全排列算法及其實(shí)現(xiàn)方法
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
 - 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
 - 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
 - 04-02jquery與jsp,用jquery
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 


