开发C++/地图中的控制台角色扮演游戏



我试图制作一款Dev C++ RPG游戏。我卡在地图上,它是一个二元数组,其尺寸是在"断线"之前可以容纳多少个字符来测量的。地图打印出来很好,但我想让玩家使用箭头键移动。 这是我到目前为止得到的(不起作用(:

#include <iostream>
#include <string>
#include <conio.h>
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
void MapPrint(int StartX, int StartY){
int Map[12][80]={0};
Map[StartX][StartY]=1;
for (int x=0; x<12; x++) 
{ 
for (int y=0; y<80; y++) 
{ 
if(Map[x][y]==0){
std::cout<<"=";
}
if (y==80){
std::cout<<"n";
continue;
}
if (Map[x][y]==1){
std::cout<<"#";
continue;
}
}   
}
}
int main(){
int Map[12][80]={0};
int StartX,StartY;
Map[StartX][StartY]=1;
int c = 0;
StartX=6;
StartY=40;
MapPrint(StartX,StartY);
while(1)
{
c=0;
switch((c=getch())) {
case KEY_UP:
system("CLS");
Map[StartX][StartY]=0;
Map[StartX][StartY+1]=1;
std::cout<<StartY; //remains of a fix attempt
MapPrint(StartX,StartY);
case KEY_DOWN:
system("CLS");
Map[StartX][StartY]=0;
Map[StartX][StartY-1]=1;
MapPrint(StartX,StartY);
case KEY_LEFT:
system("CLS");
Map[StartX][StartY]=0;
Map[StartX-1][StartY]=1;
MapPrint(StartX,StartY);
case KEY_RIGHT:
system("CLS");
Map[StartX][StartY]=0;
Map[StartX+1][StartY]=1;
MapPrint(StartX,StartY);
}
}
return 0;
}`

我已经修改了你的代码,并使其变得更好。如果您有什么要问的,请在下面发表评论。

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#define XSIZE 80
#define YSIZE 20
using namespace std;
int c_x=XSIZE/2,c_y=YSIZE/2; //the player will be in the middle of the map
int direction=1;//direction of the player
bool gameOver=false;
int tim=2000,tt;//that's how we set "speed"
void MapPrint(int newX, int newY)
{
//if the new position of the player if out of the map
//the game if over
if(c_x<0 || c_x>XSIZE || c_y<0 ||c_y>YSIZE)
{
system("CLS");
cout<<"GAME OVER!";
gameOver=true;
return;
}
//printing the map, without using an twodimesnional array
for (int y=0; y<YSIZE; y++)
{
for (int x=0; x<XSIZE; x++)
{
if(newX==x && newY==y)cout<<'#';
else cout<<' ';
}
cout<<'n';
}
}
int main()
{
char c;
MapPrint(c_x,c_y);
while(!gameOver)
{
//_kbhit() tell us if any key is pressed
if(_kbhit())
{
c=_getch();
//setting the direction according to key pressed
if(c=='w')direction=1;
else if(c=='d')direction=2;
else if(c=='s')direction=3;
else if(c=='a')direction=4;
}
tt++;
if(tt>=tim)
{
if(direction==1)
{
system("CLS");
c_y--;  // we go up, so y position of the player decrements
MapPrint(c_x,c_y);
}
else if(direction==3)
{
system("CLS");
c_y++;  // we go down, so y position of the player increments
MapPrint(c_x,c_y);
}
else if(direction==4)
{
system("CLS");
c_x--;  // we go to the left, so x position of the player decrements
MapPrint(c_x,c_y);
}
else if(direction==2)
{
system("CLS");
c_x++;  // we go to the right, so x position of the player increments
MapPrint(c_x,c_y);
}
tt=0;
}

}
return 0;
}

最新更新