问题是我想要'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;
}
作为开始,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,适用于Linuxsystem('clr');
Ps2:std::string
如果仅存储字符,为什么要使用而不是char?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句