Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ to realize the game of pushing boxes

Shulou Source: shulou.com Published: 2022-06-03 14:11:18 10月01日 Update

This article mainly explains "how to use C++ to push the box game". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to use C++ to achieve the game of pushing boxes.

I. Project introduction

With two days of spare time to review the push box this classic Mini Game, currently set 5 levels, can only achieve basic character movement. To judge the conditions for victory, other functions have not yet been realized (e.g. withdraw to the previous step, free choice of levels, etc.), but also review the relevant knowledge of C++ by the way.

II. Code area

Class Map (Map Class)

Map.h:

# pragma once#define N 10#define M 10amp / map class 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#includeusing namespace std Map::Map () {} / / Map initialization method 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, 1, 1, 1, 1, 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");} / read map file 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);} / write map file 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 (games)

Game.h:

# define _ GAEM_H__#ifdef _ GAEM_H__# include using namespace std # include # include # pragma warning (disable:4996) # define N 10#define M 10 / * * create a class for push box related operations * * / / *-- Game class writing-- -- * / * / 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" / / key control character movement int Game::Move (int map [M] [N], char ch) {static int step = 0 Int offsetx = 0; int offsety = 0; switch (ch) {/ / move up case 'w':case' walls: offsetx =-1; offsety = 0; if (push (map, offsetx, offsety) = = 1) step++; break; / / move down case 's':case' servers: offsetx = 1; offsety = 0; if (push (map, offsetx, offsety) = = 1) step++; break; / / move case 'a':case 'Aids: offsetx = 0 to the left; offsety =-1 If (push (map, offsetx, offsety) = = 1) step++; break; / / move case 'd':case' Downs to the right: offsetx = 0; offsety = 1; if (push (map, offsetx, offsety) = = 1) step++; break; default: break;} return step;} / / Interface print void Game::Drop (int map [M] [N], int c) {cout

Tags: Destination one grid open space box map people move push box Clippers success coordinates interface jackers learning hints victory levels content characters strings Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information NVidia Apple Linux