...我刚接触到这里,我有一个问题,我的代码逻辑似乎没有错误(它确实如此),当我在调试器模式下运行它时(使用microsoft visual express C ++)一切都按照它应该通过监视变量工作然而,当我在发布模式下运行它时,奇怪的是没有任何工作......或者改变
我想要做的是在黑色窗口(800x600)中移动一个精灵,将其X坐标增加1直到它到达屏幕的末尾(spriteX +24> 798)(24是宽度)精灵的边界框)然后当它发生时它移动到屏幕的另一端然后停止....
精灵在屏幕上的任意点随机产生然后它向右移动然后向左移动然后停止并且在调试器模式下会发生什么...但是在释放模式下它总是在屏幕的左边缘产生甚至不移动到它右边或左边只是呆在那里......
这是精灵移动的代码
void Game::PooMove()
{
/*for (int index = 0; index < npoo; index++)*/
bool iamfree = true;
bool iamfree2 = false;
while (iamfree)
{
poox[1]++;
if (poox[1] + 24 > 799)
{
iamfree = false;
iamfree2 = true;
}
}
while (iamfree2)
{
poox[1]--;
if (poox[1] < 1)
{
iamfree2 = false;
}
}
}
那就是精灵随机生成的代码
srand((unsigned int)time(NULL));
for (int index = 0; index < npoo; index++)
{
poox[index] = rand() % (800 - 24);
pooy[index] = rand() % (600 - 24);
pooIsEaten[index] = false;
}
考虑到它是一个更大的程序的一部分一切正常,但我的问题是在那些只在调试器中无法工作的情况,我很想知道解决方案以及为什么会发生这种情况 *我可以上传完整的代码/文件,如果这将使事情更清楚*
这是绘图代码及其调用
void Game::ComposeFrame()
{
for (int index = 0; index < npoo; index++)
{
if (!(pooIsEaten[index]))
Drawpoo(poox[index], pooy[index]);
}
}
void Game::Go()
{
UpDateFace();
UpDatePoo();
UpDateDot();
gfx.BeginFrame();
ComposeFrame();
PooMove();
gfx.EndFrame();
}
go function是调用所有内容的函数,有些函数在某些特定函数中调用,如compose frame
这就是drawpoo函数如何工作,希望帖子不会因为这段代码而被关闭
void Game::Drawpoo(int x,int y)
{
gfx.PutPixel(14 + x, 0 + y, 138, 77, 0);
gfx.PutPixel(7 + x, 1 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 1 + y, 138, 77, 0);
gfx.PutPixel(20 + x, 1 + y, 138, 77, 0);
gfx.PutPixel(6 + x, 2 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 2 + y, 138, 77, 0);
gfx.PutPixel(20 + x, 2 + y, 138, 77, 0);
gfx.PutPixel(6 + x, 3 + y, 138, 77, 0);
gfx.PutPixel(6 + x, 4 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 4 + y, 138, 77, 0);
gfx.PutPixel(21 + x, 4 + y, 138, 77, 0);
gfx.PutPixel(7 + x, 5 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 5 + y, 138, 77, 0);
gfx.PutPixel(21 + x, 5 + y, 138, 77, 0);
gfx.PutPixel(7 + x, 6 + y, 138, 77, 0);
gfx.PutPixel(20 + x, 6 + y, 138, 77, 0);
gfx.PutPixel(6 + x, 7 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 7 + y, 51, 28, 0);
gfx.PutPixel(15 + x, 7 + y, 51, 28, 0);
gfx.PutPixel(20 + x, 7 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 8 + y, 51, 28, 0);
gfx.PutPixel(13 + x, 8 + y, 51, 28, 0);
gfx.PutPixel(14 + x, 8 + y, 51, 28, 0);
gfx.PutPixel(7 + x, 9 + y, 138, 77, 0);
gfx.PutPixel(11 + x, 9 + y, 51, 28, 0);
gfx.PutPixel(12 + x, 9 + y, 102, 57, 0);
gfx.PutPixel(13 + x, 9 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 9 + y, 138, 77, 0);
gfx.PutPixel(15 + x, 9 + y, 51, 28, 0);
gfx.PutPixel(21 + x, 9 + y, 138, 77, 0);
gfx.PutPixel(10 + x, 10 + y, 51, 28, 0);
gfx.PutPixel(11 + x, 10 + y, 51, 28, 0);
gfx.PutPixel(12 + x, 10 + y, 102, 57, 0);
gfx.PutPixel(13 + x, 10 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 10 + y, 138, 77, 0);
gfx.PutPixel(15 + x, 10 + y, 51, 28, 0);
gfx.PutPixel(20 + x, 10 + y, 138, 77, 0);
gfx.PutPixel(9 + x, 11 + y, 51, 28, 0);
gfx.PutPixel(10 + x, 11 + y, 138, 77, 0);
gfx.PutPixel(11 + x, 11 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 11 + y, 51, 28, 0);
gfx.PutPixel(13 + x, 11 + y, 51, 28, 0);
gfx.PutPixel(14 + x, 11 + y, 51, 28, 0);
gfx.PutPixel(15 + x, 11 + y, 51, 28, 0);
gfx.PutPixel(16 + x, 11 + y, 51, 28, 0);
gfx.PutPixel(9 + x, 12 + y, 51, 28, 0);
gfx.PutPixel(10 + x, 12 + y, 138, 77, 0);
gfx.PutPixel(11 + x, 12 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 12 + y, 111, 62, 0);
gfx.PutPixel(13 + x, 12 + y, 102, 57, 0);
gfx.PutPixel(14 + x, 12 + y, 102, 57, 0);
gfx.PutPixel(15 + x, 12 + y, 102, 57, 0);
gfx.PutPixel(16 + x, 12 + y, 51, 28, 0);
gfx.PutPixel(9 + x, 13 + y, 51, 28, 0);
gfx.PutPixel(10 + x, 13 + y, 109, 61, 0);
gfx.PutPixel(11 + x, 13 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 13 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 13 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 13 + y, 138, 77, 0);
gfx.PutPixel(15 + x, 13 + y, 138, 77, 0);
gfx.PutPixel(16 + x, 13 + y, 51, 28, 0);
gfx.PutPixel(5 + x, 14 + y, 51, 28, 0);
gfx.PutPixel(7 + x, 14 + y, 51, 28, 0);
gfx.PutPixel(8 + x, 14 + y, 51, 28, 0);
gfx.PutPixel(9 + x, 14 + y, 51, 28, 0);
gfx.PutPixel(10 + x, 14 + y, 51, 28, 0);
gfx.PutPixel(11 + x, 14 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 14 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 14 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 14 + y, 138, 77, 0);
gfx.PutPixel(15 + x, 14 + y, 138, 77, 0);
gfx.PutPixel(16 + x, 14 + y, 51, 28, 0);
gfx.PutPixel(4 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(5 + x, 15 + y, 138, 77, 0);
gfx.PutPixel(6 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(7 + x, 15 + y, 116, 65, 0);
gfx.PutPixel(8 + x, 15 + y, 138, 77, 0);
gfx.PutPixel(9 + x, 15 + y, 138, 77, 0);
gfx.PutPixel(10 + x, 15 + y, 138, 77, 0);
gfx.PutPixel(11 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(12 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(13 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(14 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(15 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(16 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(17 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(18 + x, 15 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 16 + y, 51, 28, 0);
gfx.PutPixel(2 + x, 16 + y, 51, 28, 0);
gfx.PutPixel(3 + x, 16 + y, 51, 28, 0);
gfx.PutPixel(4 + x, 16 + y, 51, 28, 0);
gfx.PutPixel(5 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(6 + x, 16 + y, 51, 28, 0);
gfx.PutPixel(7 + x, 16 + y, 116, 65, 0);
gfx.PutPixel(8 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(9 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(10 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(11 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 16 + y, 109, 61, 0);
gfx.PutPixel(14 + x, 16 + y, 51, 28, 0);
gfx.PutPixel(15 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(16 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(17 + x, 16 + y, 138, 77, 0);
gfx.PutPixel(18 + x, 16 + y, 123, 69, 0);
gfx.PutPixel(19 + x, 16 + y, 51, 28, 0);
gfx.PutPixel(0 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 17 + y, 87, 49, 0);
gfx.PutPixel(2 + x, 17 + y, 87, 49, 0);
gfx.PutPixel(3 + x, 17 + y, 87, 49, 0);
gfx.PutPixel(4 + x, 17 + y, 87, 49, 0);
gfx.PutPixel(5 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(6 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(7 + x, 17 + y, 43, 24, 0);
gfx.PutPixel(8 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(9 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(10 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(11 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(12 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(13 + x, 17 + y, 40, 22, 0);
gfx.PutPixel(14 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(15 + x, 17 + y, 138, 77, 0);
gfx.PutPixel(16 + x, 17 + y, 138, 77, 0);
gfx.PutPixel(17 + x, 17 + y, 138, 77, 0);
gfx.PutPixel(18 + x, 17 + y, 123, 69, 0);
gfx.PutPixel(19 + x, 17 + y, 51, 28, 0);
gfx.PutPixel(0 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 18 + y, 138, 77, 0);
gfx.PutPixel(2 + x, 18 + y, 138, 77, 0);
gfx.PutPixel(3 + x, 18 + y, 138, 77, 0);
gfx.PutPixel(4 + x, 18 + y, 138, 77, 0);
gfx.PutPixel(5 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(6 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(7 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(8 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(9 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(10 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(11 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(12 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(13 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(14 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(15 + x, 18 + y, 138, 77, 0);
gfx.PutPixel(16 + x, 18 + y, 138, 77, 0);
gfx.PutPixel(17 + x, 18 + y, 138, 77, 0);
gfx.PutPixel(18 + x, 18 + y, 123, 69, 0);
gfx.PutPixel(19 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(20 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(21 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(22 + x, 18 + y, 51, 28, 0);
gfx.PutPixel(0 + x, 19 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(2 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(3 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(4 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(5 + x, 19 + y, 51, 28, 0);
gfx.PutPixel(6 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(7 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(8 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(9 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(10 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(11 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(15 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(16 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(17 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(18 + x, 19 + y, 123, 69, 0);
gfx.PutPixel(19 + x, 19 + y, 51, 28, 0);
gfx.PutPixel(20 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(21 + x, 19 + y, 138, 77, 0);
gfx.PutPixel(22 + x, 19 + y, 65, 36, 0);
gfx.PutPixel(23 + x, 19 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 20 + y, 51, 28, 0);
gfx.PutPixel(2 + x, 20 + y, 51, 28, 0);
gfx.PutPixel(3 + x, 20 + y, 51, 28, 0);
gfx.PutPixel(4 + x, 20 + y, 51, 28, 0);
gfx.PutPixel(5 + x, 20 + y, 51, 28, 0);
gfx.PutPixel(6 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(7 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(8 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(9 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(10 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(11 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(12 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(13 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(14 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(15 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(16 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(17 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(18 + x, 20 + y, 123, 69, 0);
gfx.PutPixel(19 + x, 20 + y, 51, 28, 0);
gfx.PutPixel(20 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(21 + x, 20 + y, 138, 77, 0);
gfx.PutPixel(22 + x, 20 + y, 65, 36, 0);
gfx.PutPixel(23 + x, 20 + y, 51, 28, 0);
gfx.PutPixel(0 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 21 + y, 138, 77, 0);
gfx.PutPixel(2 + x, 21 + y, 138, 77, 0);
gfx.PutPixel(3 + x, 21 + y, 138, 77, 0);
gfx.PutPixel(4 + x, 21 + y, 138, 77, 0);
gfx.PutPixel(5 + x, 21 + y, 138, 77, 0);
gfx.PutPixel(6 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(7 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(8 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(9 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(10 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(11 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(12 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(13 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(14 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(15 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(16 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(17 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(18 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(20 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(21 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(22 + x, 21 + y, 51, 28, 0);
gfx.PutPixel(0 + x, 22 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 22 + y, 65, 36, 0);
gfx.PutPixel(2 + x, 22 + y, 65, 36, 0);
gfx.PutPixel(3 + x, 22 + y, 65, 36, 0);
gfx.PutPixel(4 + x, 22 + y, 65, 36, 0);
gfx.PutPixel(5 + x, 22 + y, 65, 36, 0);
gfx.PutPixel(6 + x, 22 + y, 51, 28, 0);
gfx.PutPixel(1 + x, 23 + y, 51, 28, 0);
gfx.PutPixel(2 + x, 23 + y, 51, 28, 0);
gfx.PutPixel(3 + x, 23 + y, 51, 28, 0);
gfx.PutPixel(4 + x, 23 + y, 51, 28, 0);
gfx.PutPixel(5 + x, 23 + y, 51, 28, 0);
gfx.PutPixel(6 + x, 23 + y, 51, 28, 0);
}
如何宣布poox和pooy
#define NPOO 3
class Game{
/*many functions and variables*/
int poox[NPOO];
int pooy[NPOO];
}
答案 0 :(得分:0)
如果发布构建破坏了您可能有未定义的行为(可能是竞争条件)。并且错误不在您提供的代码中。
答案 1 :(得分:0)
PooMove
功能的效果是将对象移动到屏幕右侧,然后再移回到左侧。但是,PooMove
函数中没有代码可以查看对象的位置。所以没有办法真正看到物体移动。在PooMove
完成其工作之后,绘图代码才会发生。这意味着您只能在屏幕左侧看到您的对象,因为PooMove
放置了它们。
事实上,就像现在一样,PooMove
也可以这样写:
void Game::PooMove()
{
poox[1] = 0;
}
效果将完全相同,只是没有浪费时间将物体向左移动。
答案 2 :(得分:0)
不是&#34;优先级&#34;当你在其中一个评论中写道时,它是关于控制流的。
您可能需要做的是让PooMove
每次调用时只移动一个(假设您在循环中反复调用Go()
)。
因此,您希望保持其bool iamfree = true; bool iamfree2 = false;
变量的方向状态,因此请将它们添加为Game
类的状态。虽然我们正在使用它,但是让它只是一个bool并将其重命名
class Game
{
bool goingRight;
//...
};
现在你可以在函数中访问该状态并在那里进行更改并在调用之间保持这种状态。
void Game::PooMove()
{
if (goingRight)
{
poox[1]++;
if (poox[1] + 24 > 799)
{
goingRight = false;
}
}
else
{
poox[1]--;
if (poox[1] < 1)
{
goingRight = true;
}
}
}
现在它来回走动,但你可以通过引入另一个布尔值来阻止它,但我会把它作为练习留给读者。