欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎ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://mengdiqiu.com.cn/a1/Cyuyan/140.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有