问题是我想要'o',不要重复......我试图找到但我失败了! 还要解释一下这是怎么发生的。我是一名新的学习程序员!
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
using namespace std;
int main()
{
//all in-game variables here
string o = "o";
int oH = 0;
int oW = 0;
//variables ending point...
bool run = true;
bool frameShow = true;
char input;
int width = 53;
int height = 22;
string px[width][height];
while (run)
{
for (int xEmp=0; xEmp<height; xEmp++){
for (int yEmp=0; yEmp<width; yEmp++)
{
px[xEmp][yEmp]= " ";
}
if (frameShow)
{
clrscr(); // Must be at start
px[oH][oW] = o;
for (int x=0; x<height; x++)
{
for (int y=0; y<width; y++)
{
cout << px[x][y];
}
cout << "\n";
frameShow = false; // Must be at end
}
}
if (kbhit())
{
input = getch();
// Most Used ones are:
// char 119 for "w"
// char 97 for "a"
// char 115 for "s"
// char 100 for "d"
// char 32 for "space"
// char 27 for ESC
if (input == 119)
{
oH--;
frameShow = true;
}
else if (input == 115)
{
oH++;
frameShow = true;
}
else if (input == 97)
{
oW--;
frameShow = true;
}
else if (input == 100)
{
oW++;
frameShow = true;
}
else if (input == 27)
{
// Game exits...
// To terminate, use:
run = false;
}
if(oH > height)
{
oH = height;
}
else if(oH < 0)
{
oH = 0;
}
if(oW > width - 1)
{
oW = width - 1;
}
else if(oW < 0)
{
oW = 0;
}
}
}
}
// Output for confiming program termination
clrscr();
cout << "\n - Terminated! - \n";
return 0;
}
答案 0 :(得分:1)
首先,width
和height
的使用正在改变。 px[width][height];
,px[xEmp][yEmp]
,xEmp<height
,yEmp<width
。有一次将其用作px[width][height];
,然后px[height][width];
。保持你的代码连贯!
if(oH > height) { oH = height; }
也是错误的。从高处减去一个。
这也可能不符合您的要求:
for (int x=0; x<height; x++)
for (int y=0; y<width; y++)
{
cout << px[x][y];
}
cout << "\n";
正确使用括号,如果您不知道如何使用它们,请始终使用它们!
同样,我认为你没有正确使用括号:
for (int xEmp=0; xEmp<height; xEmp++){
for (int yEmp=0; yEmp<width; yEmp++)
{
px[xEmp][yEmp]= " ";
}
... // do other things
}
我想你想马上把它关闭,把所有东西都放回太空,然后做其他工作。就像现在一样,它会将一行重新设置为空格,打印所有内容并运行代码,并且仅在空白下一行之后。
for (int xEmp=0; xEmp<height; xEmp++){
for (int yEmp=0; yEmp<width; yEmp++)
{
px[xEmp][yEmp]= " ";
}
}
... // do other things
Ps:clrscr()
是一个非标准功能,我认为只适用于Windows,适用于Linux system('clr');
Ps2:如果只存储字符,为什么使用std::string
代替字符?