Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 做蛇游戏。I';检测到移动后,我无法更改屏幕。C++;_C++_Matrix - Fatal编程技术网

C++ 做蛇游戏。I';检测到移动后,我无法更改屏幕。C++;

C++ 做蛇游戏。I';检测到移动后,我无法更改屏幕。C++;,c++,matrix,C++,Matrix,很抱歉我对代码的拙劣翻译。我不是以英语为母语的人,所以我对如何翻译成英语没有明确的想法。我想做的是一个C++游戏中的蛇游戏。首先,我想知道如何在地图上移动字符(一个“=”)。我尝试了这个,我成功地掌握了这个动作,我很确定我会在整场比赛中使用它。因此,游戏首先给n一个值,它表示矩阵的列数和行数。在那之后,我将显示矩阵,这样玩家就有了一个想法,它看起来是什么样子。但是,在按下一个键后,我必须显示新的矩阵,旧的矩阵也将显示在它的上方,因此,最后我将有一个满是矩阵的屏幕。按下移动键后,我想用新矩阵替换旧

很抱歉我对代码的拙劣翻译。我不是以英语为母语的人,所以我对如何翻译成英语没有明确的想法。我想做的是一个C++游戏中的蛇游戏。首先,我想知道如何在地图上移动字符(一个“=”)。我尝试了这个,我成功地掌握了这个动作,我很确定我会在整场比赛中使用它。因此,游戏首先给n一个值,它表示矩阵的列数和行数。在那之后,我将显示矩阵,这样玩家就有了一个想法,它看起来是什么样子。但是,在按下一个键后,我必须显示新的矩阵,旧的矩阵也将显示在它的上方,因此,最后我将有一个满是矩阵的屏幕。按下移动键后,我想用新矩阵替换旧矩阵。我怎样才能做到?这是我的当前代码:

#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
    char a[100][100];
    unsigned i, j, k, punct, n, m, x, y;
    int c;
    cout << "The game is represented by a matrix of n rows and n columns. Give a value to 'n'";
    cin >> n;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            a[i][j] = ' '; // I'm creating the matrix and I'm filling it with empty spaces.
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++) //Now I'm creating the border lines.
        {
            a[1][j] = '/';
            a[i - 1][1] = '/';
            a[n][j] = '/';
            a[i - 1][n] = '/';
            a[3][3] = '=';
        }
    }
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++) //I'm showing the matrix.
            cout << a[i][j];
        cout << endl;
    }
    while (1)    //Infinite loop.
    {
        x = 3;
        y = 3;
        if (kbhit())  //I'm checking if a key is pressed.
        {
            c = getch();
            if (c == 72)  //If a key is pressed make the character which is represented by a '=' to move upward by a space.
            {
                x = x - 1;
                y = y - 0;
                a[x][y] = '=';
                a[x + 1][y] = ' ';

            }
            for (i = 1; i <= n; i++) //Again I'm showing the matrix in order to see the difference and where the character has moved.
            {
                for (j = 1; j <= n; j++)
                    cout << a[i][j];
                cout << endl;
            }

        }
    }


    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符a[100][100];
无符号i,j,k,点,n,m,x,y;
INTC;
cout>n;

对于(i=1;i我可以从你的问题中推断出,你想要显示新矩阵,并且想要隐藏旧矩阵,而不是放在它的正上方。如果我误解了什么,请纠正我

一种简单(但不是很优雅)的方法是打印一百行空的新行,然后打印下一个矩阵。这样,只会显示新矩阵。此外,控制台只会存储有限的行(通常),因此这甚至不会占用太多空间(即使console保留了所有线路,我也怀疑这会太多)


您可以为您的问题插入
cout:由于您已经在使用conio.h,请使用
clrsc()
system(“cls”)
。但请不要使用conio.h

对于您的代码:您需要更好地组织它。将您编程的不同任务在不同的功能中分开。具体来说,对于游戏,尝试将您的游戏逻辑与其在屏幕上的显示分开,这样您就可以更轻松地进行推理(也许你会发现你的矩阵根本不需要)。我冒昧地对你的代码做了一些调整(实际上是很多)

#include <conio.h>
#include <iostream>
using namespace std;

bool update(unsigned& x, unsigned& y, unsigned n) {
  if (kbhit()) {  //I'm checking if a key is pressed.
    switch (getch()) { // get real value for the arrow
    case 75:
      y--;
      if (y==1) y++; // Cancel if in wall;
      break;
    case 77:
      y++;
      if (y==n) y--; // Cancel if in wall;
      break;
    case 80:
      x++;
      if (x==n) x--; // Cancel if in wall;
      break;
    case 72:
      x--;
      if (x==1) x++; // Cancel if in wall;
      break;
    case 27: // ESC to quit
      return true; // exit
    }
  }
  return false; // Do not exit, continue the game.
}

void draw(unsigned x, unsigned y, unsigned n) {
  // MAYBE your conio.h has clrscr(). Mine doesn't. So I use system.
  system("cls"); // clear screen before drawing. WILL PRODUCE FLICKERING. A LOT.
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      // No need for a matrix for drawing, you can infer each character quite
      // easily.
      if (i==1 || j==1 || i==n || j==n) {
        putch('/');
      }
      else {
        if (i == x && j == y) {
          putch('=');
        }
        else {
          putch(' ');
        }
      }
    }
    cout << endl;
  }
}

int main() {
  unsigned n, x, y;
  cout << "The game is represented by a matrix of n rows and n columns. Give a value to 'n': ";
  cin >> n;
  x = 3;
  y = 3;
  bool exit = false;
  while (!exit) { // Don't do an infinite loop. Your user will want an exit.
      exit = update(x,y,n);
      draw(x,y,n);
  }
  return 0;
}
#包括
#包括
使用名称空间std;
布尔更新(无符号&x、无符号&y、无符号n){
if(kbhit()){//我正在检查是否按下了一个键。
开关(getch()){//获取箭头的实际值
案例75:
y--;
if(y==1)y++;//如果在墙中,则取消;
打破
案例77:
y++;
如果(y==n)y--;//如果在墙中,则取消;
打破
案例80:
x++;
如果(x==n)x--;//如果在墙中,则取消;
打破
案例72:
x--;
如果(x==1)x++;//如果在墙中,则取消;
打破
案例27://ESC退出
返回true;//退出
}
}
return false;//不退出,继续游戏。
}
无效绘图(无符号x、无符号y、无符号n){
//也许你们的conio.h有CLRSC(),我的没有。所以我使用这个系统。
系统(“cls”);//绘图前清除屏幕。会产生闪烁。很多。

对于(inti=1;i如果我处在你的位置,我会从图形库/游戏引擎开始,并尝试在此基础上构建游戏。类似于SMFL()

回到你的问题,我记得Borland C++有一个叫CONIO的库,它有清除屏幕的功能,把一个字符放在控制台上的一个给定的坐标上……但是我认为这只是DOS。GNU有一个类似的库:ncCuess()它适用于Linux或类似的操作系统。对于windows,您可以使用类似的东西:PDcurses()

这些库具有将字符放置在给定坐标的函数,因此您需要做的是在启动程序时首先初始化屏幕(在ncurses中是
initscr()
),然后使用类似于
mvaddch(row,col,ch)的东西在您想要的坐标中绘制蛇;
它将移动光标到坐标
(行,列)
,然后打印
ch
给出的字符。然后按下一个键,在旧位置打印一个空白字符
,然后将蛇头放在新位置,删除旧位置的蛇头。例如:

//add this header to include the library, but you need to download and install the library before you can use it
#include <ncurses.h>

//inside the program, let's say in main:
....
//initialize
initscr();

/initial location of snakes head
int row = ..., col = ...;
int oldrow, oldcol;
//game loop
while(...)
{
    oldrow = row;
    oldcol = col;
    ch = getch();
    switch(ch)
    {
         case KEY_RIGHT:
             col++;
             break;
         //rest of game logics
         ...
    }
    mvaddch(oldrow, oldcol, ' ');
    mvaddch(row, col, '=');
    refresh();
}
endwin();
//添加此标头以包含库,但您需要下载并安装库才能使用它
#包括
//在程序内部,让我们主要说:
....
//初始化
initscr();
/蛇头的初始位置
int行=…,列=。。。;
int oldrow,oldcol;
//游戏循环
而(…)
{
oldrow=行;
oldcol=col;
ch=getch();
开关(ch)
{
案例编号:右:
col++;
打破
//游戏逻辑的其余部分
...
}
mvaddch(oldrow,oldcol,”);
mvaddch(行,列,'=');
刷新();
}
endwin();
显然,这不是蛇游戏的内容。在真正的游戏中,你应该在每一个时钟滴答声时改变
,然后当按下一个键时,你就改变运动方向


最后,我再次建议您选择一个图形库/游戏引擎,并使用它,而不是在文本模式下工作。以后您可以更轻松地扩展/修改游戏。即使您想要一个具有经典风格的游戏,您也可以让游戏具有复古的文本风格。

您的代码格式是一场噩梦。我很抱歉我知道这一点,抱歉,伙计。但是修复它只需要不到一分钟的时间。如果你付出了努力,而不是其他人试图维护网站的标准,那就太好了。@Therainmaker,那么我想我误解了他说的话。你想让我在代码中修改什么?请告诉我,我很乐意这么做!我以为他说这不够有效,而且