最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)推箱子游戲

 更新時(shí)間:2019年10月24日 17:16:08   作者:Amumu12138  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)推箱子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、項(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)文章

最新評(píng)論

商南县| 衡山县| 南乐县| 万山特区| 大余县| 怀柔区| 乐都县| 苏尼特左旗| 高雄县| 炎陵县| 青神县| 斗六市| 涿鹿县| 华阴市| 古田县| 海丰县| 萝北县| 苏州市| 廉江市| 瑞金市| 嵩明县| 高阳县| 红河县| 汶上县| 将乐县| 杂多县| 淄博市| 大足县| 监利县| 江阴市| 白山市| 永寿县| 广州市| 马鞍山市| 渭源县| 泰顺县| 密云县| 南安市| 全南县| 尖扎县| 聂拉木县|