【发布时间】:2019-04-07 13:13:45
【问题描述】:
所以一旦用户按下 1 以启动地图和其他变量,我就会打印出我的迷宫。我已将玩家声明为笑脸或设置为等于 1。但是,我想让这个角色在整个迷宫中移动,但在这样做时遇到了麻烦。我创建了一个玩家移动作为空间,以便它在不覆盖其他代码的情况下移动。我正在考虑使用 switch 语句并检测用户输入,然后基于此更改 X 和 Y 位置,但我不知道如何将其放入我的 mapCreation 函数中。我还想知道你们是否可以帮助我在整个地图的随机位置生成诸如宝藏之类的变量,以及它是一堵墙还是“|”然后不要打印。我的宝藏产卵但在同一个位置。对我的任何问题的任何输入都会有帮助!谢谢!
** 忽略这些案例,因为它们以前只是被使用,但现在我没有使用它们。
**更新 如果有人在某处有链接会有所帮助,那也会有所帮助!谢谢!
// Header Files
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()
#define LEVEL_COUNT (2)
const char* maze[LEVEL_COUNT][12] =
{
{
"||||||||||||\n",
"| | |\n",
"| | |\n",
"| |||| |",
"| |",
"| |",
"|||||| |",
"| | |",
"| | | |",
"| | |",
"| | |",
"||||||||||||",
},
{
"||||||||||||",
"| | |",
"| ||||| |",
"| | |",
"| |",
"| ||||",
"| |",
"| | |",
"| | |",
"||||||| |",
"| |",
"||||||||||||",
},
};
// Function Prototypes
void titleScreen(); // Prints Title and instructions
void mapCreation( char arr[][12], int level);
void drawMap(char arr[][12]);
bool update(char arr[][12], int &level, int &lives, int &score);
void loadMap( char arr[][12], int level);
// Player Struct
struct Player{
int x;
int y;
char move;
};
// Main Program
int main ()
{
// initialize variables
int option;
char baseMap[12][12];
int level = 1;
int lives = 3;
int score = 0;
bool gameOver = false;
bool levelCompleted = false;
SetConsoleTextAttribute(console, 240); // change background to white
system("CLS");// clears screen in order to remove black background
titleScreen(); // Display Title
do // do-while loop starts
{
cin >> option; // take in input
if(option == 1) // temporary option to check for next screen
{
//Display Maze
system("CLS");// clears screen in order to remove black background
mapCreation( baseMap, 1 ); // Create map, this one is map 1
drawMap(baseMap); // iterate throup the map and print it out
// update(baseMap, level, lives, score);
}
if(option == 2)
{
//Display Maze
system("CLS");// clears screen in order to remove black background
mapCreation( baseMap, 2 ); // Create map, this one is map 1
drawMap(baseMap); // iterate throup the map and print it out
// update(baseMap, level, lives, score);
}
}
while( option !=1); // condition of do-while loop
system("pause"); // Pause for user, only temporary
return 0;
}
void titleScreen(){
cout << " Welcome to Treasure Hunter!\n\n";
cout << "In order to beat this game you must find the treasure\n";
cout << " that is located in the maze. You can move using the \n";
cout << " arrow keys or WASD.\n\n";
cout << " Warning! There are traps that will take life away as\n";
cout << " well as add life! However, they are hidden so be careful!\n ";
cout << " Goodluck and have fun!\n\n\n\n";
cout << " Press Ctrl+C to quit\n";
}
void mapCreation( char arr[12][12], int level )
{
int traps = 0;
int lives = 0;
int treasure = 0;
int x ;
int y ;
for(int i = 0; i < 12; i++)
{
for(int j = 0; j < 12; j++)
{
arr[i][j] = 0;
}
}
// load the map:
loadMap(arr,level);
switch (level)
{
case 1:
break;
case 2: // Level 2 Map
break;
}
}
void drawMap(char arr[12][12])
{
// Declare variables to be prtined out
Player player;
player.x = 1;
player.y = 1;
player.move = ' ';
int traps = 0;
int lives = 0;
int treasure = 0;
// Lives,Traps, and Treasure x and y positions
int Trap_Y,Trap_X,Lives_X,Lives_Y,Traps_X,Traps_Y;
// Randomly place variables
Trap_X = (rand() % 10);
Trap_Y = (rand() % 10);
Lives_X = (rand() % 12);
Lives_Y = (rand() % 12);
Traps_X = (rand() % 12);
Traps_Y = (rand() % 12);
for(int i = 0; i < 12; i++)
{
cout << endl; // print out new line
for(int j = 0; j < 12; j++ )
{
if(player.x == i && player.y == j)
arr[player.x][player.y] = 1;
// If variables are not equal to a wall character print it out
if(arr[Trap_X][Trap_Y] != '|' )
arr[Trap_X][Trap_Y] = 4; // treasure is 4
treasure++;
if(arr[Lives_X][Lives_Y] != '|')// traps are 2
arr[Lives_X][Lives_Y]= 2;
traps++;
if(arr[Traps_X][Traps_Y] != '|') // lives are 3
arr[Traps_X][Traps_Y]= 3;
lives++;
cout << arr[i][j];
}
}
cout << endl;
}
/*bool update(char arr[][12], int &level, int &lives, int &score)
{
bool levelCompleted = false;
bool gameOver = false;
}
*/
void loadMap( char arr[][12], int level)
{
if((level < 0) || (level >= LEVEL_COUNT))
return;
for(int i = 0; i < 12; i++)
{
const char* row = maze[level][i];
for(int j = 0; j < 12; j++)
{
if(row[j] == 0)
break; // end of string
if(row[j] == ' ')
arr[i][j] = 0; // set spaces to zero
else
arr[i][j] = row[j];
}
}
}
【问题讨论】:
-
我以前在 Linux 中做过类似的事情,最好使用 ncurses 库。看起来有一个类似的版本,称为 Windows 的 PDCurses。 curses 允许您将字符写入特定坐标,这听起来像您想要的。
-
我想知道如何使用诅咒,因为我将它包含在我的标题中但它没有检测到它。有什么想法可以包含或拥有 ncurses 吗?也感谢您的评论!
-
ncurses 是一个 Linux 库。由于您已包含
windows.h,因此我假设您没有它。 PDCurses 是一个第三方库。您必须下载并安装它。 -
好的,我安装了。有什么链接可以让我学习如何使用与玩家运动相关的内容吗?
-
我从未使用过 PDCurses。阅读documentation。如果您在“输入值”下查看,您将看到可以从
getch()获得的键。还要注意keypad()函数。您会感兴趣的另一个函数是mvaddch()。您还需要一些其他设置和拆卸功能。根据我对文档的阅读,它看起来与 ncurses 几乎相同,因此只需在谷歌周围搜索其中一个的基本示例并参考文档进行仔细检查。
标签: c++ user-controls