请任何人帮助我:C ++ 2D阵列迷宫游戏,移动物体。 [英] Please can anybody help me with : C++ 2D array maze game , moving objects .

查看:63
本文介绍了请任何人帮助我:C ++ 2D阵列迷宫游戏,移动物体。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助:C ++ 2D阵列迷宫游戏,移动物体。





我有这个迷宫游戏,我想制作,

在这个迷宫中'@'符号应该能够在网格中移动一个角色

,所以这基本上就像移动巨石游戏一样你可以使用@来移动对象
迷宫中的人物。

但现在的问题是,代码中写了如何在整个迷宫中移动@

符号,但是你如何实际放置另一个角色

在迷宫中用@符号推动它移动。

有人可以帮帮我吗?



---------------------------------- -------







Help : C++ 2D array maze game , moving objects .


Hi , I have got this maze game that I want to make ,
in this maze the '@' symbol should be able to move a character
in the grid , so this is basically like moving boulders game where you can
move object by using @ and pushing some characters in the maze .
But the problem now is that there is the code written how to move the @
symbol throughout the maze , however how do you actually put another character
in the maze and make it move by pushing it with @ symbol .
Can anybody help me out ?

-----------------------------------------



//include standard libraries
#include <iostream>	//for output and input: cout <<, cin >>
#include <iomanip> 	//for output manipulators
#include <conio.h> 	//for getch()
#include <string>	//for string
using namespace std;

//include our own libraries
#include "ConsoleUtils.h"	//for Clrscr, Gotoxy, etc.

//---------------------------------------------------------------------------
//----- define constants
//---------------------------------------------------------------------------

//defining the size of the grid

const int  SIZEY(6);	//vertical dimension
const int  SIZEX(10);    //horizontal dimension
//defining symbols used for display of the grid & content
const char MOUSE('@');   //mouse
const char TUNNEL(' ');  //tunnel
const char WALL('#');    //border
//defining the command letters to move the mouse on the maze
const int  UP(72);       //up arrow
const int  DOWN(80);     //down arrow
const int  RIGHT(77);	//right arrow
const int  LEFT(75);	//left arrow
//defining the other command letters
const char QUIT('Q');	//to end the game

struct Item {
	int x, y;
	const char symbol;
};

//---------------------------------------------------------------------------
//----- run game
//---------------------------------------------------------------------------

int main()
{
	//function declarations (prototypes)
	void initialiseGame(char g[][SIZEX + 1], char m[][SIZEX + 1], Item& mouse);
	void paintGame(const char g[][SIZEX + 1], string& mess);
	bool wantsToQuit(int key);
	bool isArrowKey(int k);
	int  getKeyPress();
	void moveMouse(const char g[][SIZEX + 1], Item& mouse, int key, string& mess);
	void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);
	void endProgram();

//local variable declarations 
char grid[SIZEY + 1][SIZEX + 1];	//grid for display
char maze[SIZEY + 1][SIZEX + 1];	//structure of the maze
Item mouse = { (SIZEX / 2) + 1, (SIZEY / 2) + 1, MOUSE }; //mouse's position and symbol
string message("LET'S START...");	//current message to player

	//action...
	Clrscr();
	initialiseGame(grid, maze, mouse);	//initialise grid (incl. walls & mouse)
	paintGame(grid, message);	//display game info, modified grid & messages
	int key(getKeyPress()); 	//read in  selected key: arrow or letter command
	while (!wantsToQuit(key))		//while user does not want to quit
	{
		if (isArrowKey(key))
		{
			int dx(0), dy(0);
	moveMouse(grid, mouse, key, message);	//move mouse in that direction
	updateGrid(grid, maze, mouse);	//update grid information
		}
		else
	message = "INVALID KEY!";	//set 'Invalid key' message
	paintGame(grid, message);	//display game info, modified grid & messages
	key = getKeyPress(); 	//display menu & read in next option
	}
	endProgram();		//display final message
	return 0;
}

//---------------------------------------------------------------------------
//----- initialise game state
//---------------------------------------------------------------------------

void initialiseGame(char grid[][SIZEX + 1], char maze[][SIZEX + 1], Item& mouse)
{ //initialise grid & place mouse in middle
	void setInitialMazeStructure(char g[][SIZEX + 1]);
	void setInitialMouseCoordinates(Item& mouse);
	void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);

	setInitialMazeStructure(maze);	//initialise maze
	setInitialMouseCoordinates(mouse);	//initialise mouse's position
	updateGrid(grid, maze, mouse); 	//prepare grid
}


void setInitialMazeStructure(char maze[][SIZEX + 1])


{ //set the position of the walls in the maze
  //initialise maze configuration

char initialMaze[SIZEY + 1][SIZEX + 1] 	//local array to store the maze structure

		= { { 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' },
		{ 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
		{ 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
		{ 'X', '#', ' ', ' ', '#', ' ', ' ', '#', ' ', ' ', '#' },
		{ 'X', '#', ' ', ' ', '#', '#', ' ', '#', ' ', '#', '#' },
		{ 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
		{ 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
	// with '#' for wall, ' ' for tunnel and 'X' for unused part of array
	//copy into maze structure
	for (int row(1); row <= SIZEY; ++row)	//for each row (vertically)
	for (int col(1); col <= SIZEX; ++col)	//for each column (horizontally)
		maze[row][col] = initialMaze[row][col];
}

void setInitialMouseCoordinates(Item& mouse)
{ //calculate mouse's coordinates at beginning of game
	mouse.y = 3;   	//y-coordinate: vertically
	mouse.x = 5; 	//x-coordinate: horizontally
}

//---------------------------------------------------------------------------
//----- update grid state
//---------------------------------------------------------------------------

void updateGrid(char grid[][SIZEX + 1], const char maze[][SIZEX + 1], Item mouse)
{ //update grid configuration after each move
	void setMaze(char g[][SIZEX + 1], const char m[][SIZEX + 1]);
	void placeMouse(char g[][SIZEX + 1], Item mouse);

	setMaze(grid, maze);	//reset the empty maze configuration into grid
	placeMouse(grid, mouse);	//set mouse in grid
}

void setMaze(char grid[][SIZEX + 1], const char maze[][SIZEX + 1])
{ //reset the empty/fixed maze configuration into grid
	for (int row(1); row <= SIZEY; ++row)	//for each row (vertically)
	for (int col(1); col <= SIZEX; ++col)	//for each column (horizontally)
		grid[row][col] = maze[row][col];
}

void placeMouse(char g[][SIZEX + 1], Item m)
{ //place mouse at its new position in grid
	g[m.y][m.x] = m.symbol;
}

//---------------------------------------------------------------------------
//----- move the mouse
//---------------------------------------------------------------------------
void moveMouse(const char g[][SIZEX + 1], Item& m, int key, string& mess)
{ //move mouse in required direction
	void setKeyDirection(int k, int& dx, int& dy);
	//calculate direction of movement required by key - if any
	int dx(0), dy(0);
	setKeyDirection(key, dx, dy); 	//find direction indicated by key
	//check new target position in grid & update mouse coordinates if move is possible
	switch (g[m.y + dy][m.x + dx])
	{			//...depending on what's on the target position in grid...
	case TUNNEL:		//can move
		m.y += dy;		//go in that Y direction
		m.x += dx;		//go in that X direction
		break;

	case WALL:  		//hit a wall & stay there
		cout << '\a';	//beep the alarm
		mess = "CANNOT GO THERE!";
		break;
	}
}

//---------------------------------------------------------------------------
//----- process key
//---------------------------------------------------------------------------
void setKeyDirection(int key, int& dx, int& dy)
{ //
	switch (key)		//...depending on the selected key...
	{
	case LEFT:  	//when LEFT arrow pressed...
		dx = -1;	//decrease the X coordinate
		dy = 0;
		break;
	case RIGHT: 	//when RIGHT arrow pressed...
		dx = +1;	//increase the X coordinate
		dy = 0;
		break;
	case UP: 		//when UP arrow pressed...
		dx = 0;
		dy = -1;	//decrease the Y coordinate
		break;
	case DOWN: 		//when DOWN arrow pressed...
		dx = 0;
		dy = +1;	//increase the Y coordinate
		break;
	}
}

int getKeyPress()
{ //get key or command selected by user
	int keyPressed;
	keyPressed = getch();	//read in the selected arrow key or command letter
	while (keyPressed == 224) 	//ignore symbol following cursor key
		keyPressed = getch();
	return(toupper(keyPressed));	//return it in uppercase 
}

bool isArrowKey(int key)
{	//check if the key pressed is an arrow key (also accept 'K', 'M', 'H' and 'P')
	return ((key == LEFT) || (key == RIGHT) || (key == UP) || (key == DOWN));
}
bool wantsToQuit(int key)
{	//check if the user wants to quit (when key is 'Q' or 'q')
	return (toupper(key) == QUIT);
}

//---------------------------------------------------------------------------
//----- display info on screen
//---------------------------------------------------------------------------
void paintGame(const char gd[][SIZEX + 1], string& mess)
{ //display game title, messages, maze, mouse & apples on screen
	void paintGrid(const char g[][SIZEX + 1]);

	//clear screen
	Clrscr();

	//display game title
	SelectTextColour(clYellow);
	Gotoxy(0, 0);
	cout << "___MOUSE AND APPLES GAME___\n" << endl;
	SelectBackColour(clWhite);
	SelectTextColour(clRed);
	Gotoxy(40, 0);
	cout << "Pascale Vacher: March 14";  //PUT YOUR GROUP NUMBER AND NAMES HERE

	// display grid contents
	paintGrid(gd);

	//display menu options available
	SelectBackColour(clRed);
	SelectTextColour(clYellow);
	Gotoxy(40, 3);
	cout << "TO MOVE USE KEYBOARD ARROWS ";
	Gotoxy(40, 4);
	cout << "TO QUIT ENTER 'Q'           ";

	//print auxiliary messages if any
	SelectBackColour(clBlack);
	SelectTextColour(clWhite);
	Gotoxy(40, 8);
	cout << mess;	//display current message
	mess = "";		//reset message to blank
}


void paintGrid(const char g[][SIZEX + 1])
{ 
//display grid content on screen
SelectBackColour(clBlack);
SelectTextColour(clWhite);
Gotoxy(0, 2);
for (int row(1); row <= SIZEY; ++row)	//for each row (vertically)
{
 for (int col(1); col <= SIZEX; ++col)	//for each column (horizontally)
 cout << g[row][col];	//output cell content
 cout << endl;
  }
}

void endProgram()
{
	SelectBackColour(clRed);
	SelectTextColour(clYellow);
	Gotoxy(40, 8);
	//hold output screen until a keyboard key is hit
	cout << "\n";
	system("pause");
}

推荐答案

It looks like you have a CASE statement looking at the square your mouse is moving into.



in that case statement, if the character is the boulder, then you want to move the boulder in the same direction the mouse is about to move.



Adding the boulder character is as simple as defining which character you want to use (let’s say ’O’) then putting it in your maze somewhere there isn’t already another item.



Quick and Dirty way to do this:



Choose a random X & Y

While (maze(X,Y) is not empty

{

Add 1 to X

If X > width, X=0; Y+=1;

If Y > height, Y = 0

}

Put your boulder at (X,Y)
It looks like you have a CASE statement looking at the square your mouse is moving into.

in that case statement, if the character is the boulder, then you want to move the boulder in the same direction the mouse is about to move.

Adding the boulder character is as simple as defining which character you want to use (let's say 'O') then putting it in your maze somewhere there isn't already another item.

Quick and Dirty way to do this:

Choose a random X & Y
While (maze(X,Y) is not empty
{
Add 1 to X
If X > width, X=0; Y+=1;
If Y > height, Y = 0
}
Put your boulder at (X,Y)


这篇关于请任何人帮助我:C ++ 2D阵列迷宫游戏,移动物体。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆