devc+中的控制台RPG+;/地图 我想制作一个DEV C++ RPG游戏。我被地图困住了,它是一个二维数组,在“打破”一条线之前,用多少个字符来测量尺寸。地图打印得很好,但我想让玩家用箭头键移动。 以下是我到目前为止得到的(不起作用): #包括 #包括 #包括 #定义键u UP 72 #定义向下80键 #定义左键75 #定义右键77 无效地图打印(int StartX、int StartY){ int Map[12][80]={0}; Map[StartX][StartY]=1; 对于(intx=0;x

devc+中的控制台RPG+;/地图 我想制作一个DEV C++ RPG游戏。我被地图困住了,它是一个二维数组,在“打破”一条线之前,用多少个字符来测量尺寸。地图打印得很好,但我想让玩家用箭头键移动。 以下是我到目前为止得到的(不起作用): #包括 #包括 #包括 #定义键u UP 72 #定义向下80键 #定义左键75 #定义右键77 无效地图打印(int StartX、int StartY){ int Map[12][80]={0}; Map[StartX][StartY]=1; 对于(intx=0;x,c++,c++11,C++,C++11,我已经修改了你的代码,并且做得更好。如果你有什么问题,请在下面留下评论 #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 ma

我已经修改了你的代码,并且做得更好。如果你有什么问题,请在下面留下评论

#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;
}
#包括
#包括
#包括
#定义XSIZE80
#第20页
使用名称空间std;
int cxx= xSea/ 2,Cyy=ySea/ 2;//玩家将在地图中间。
int direction=1;//播放器的方向
bool gameOver=false;
int tim=2000,tt;//这就是我们设置“速度”的方式
void映射打印(int-newX,int-newY)
{
//如果玩家的新位置不在地图上
//比赛结束了
如果(c|xXSIZE | c|yYSIZE)
{
系统(“CLS”);

这些行
int-Map[12][80]={0};int-StartX,StartY;Map[StartX][StartY]=1;
main
中,您是否访问了
Map
中可能超出范围的未知索引。在
MapPrint
函数中的
Map
main
函数中的
Map
不同。您是否使用了正确的箭头键ASCII值?@TusharR.这些ASCII值是什么?@TusharR。这些不是ASCII值。只需在切换之前调用getch()并重试。当您按箭头键时,第一个getch()返回0,以便您可以忽略它,然后下一个getch()返回call返回您用于箭头键的值。您设置的ASCII值没有问题。不需要更改。抱歉。代码工作正常,但一旦设置了方向,播放机就会继续朝该方向移动,我不知道您是有意这样做还是必须这样做。无论如何,感谢您的帮助非常感谢。是的,这是我的意图,如果你只想在按键时移动一次,你应该删除if(_kbhit())语句
#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;
}