C++在减去 int 时向 int 添加一个额外的数字

C++ adds an extra number to an int when subtracting it

本文关键字:int 一个 数字 时向 C++ 添加      更新时间:2023-10-16

我正在C++做一个ASCII游戏。玩家四处移动,避开障碍物("X"(。玩家有生命值,并在遇到障碍物时失去生命值。我将运行状况声明为 int 并设置为 100:

int health = 100;

然后我做了一个开关盒来检测碰撞。 然后我输入:

health--;

并运行了该程序。我遇到了一个障碍,它降低了生命值,但增加了一个额外的数字(例如,它不是说 99,而是说 999(。 我尝试调试,但一无所获。我做错了什么?以下是完整的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define FPS 100
using namespace std;
char Map[21][33] =
{
"##############################",
"#@                           #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                            #",
"#                           !#",
"##############################",

};
int x = 1;
int y = 1;
int choice;
int level = 1;
int health = 100;
int lives = 5;
int score;
void clearScreen()
{
short posX = 0;
short posY = 0;
COORD coord = {posX,posY};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void clearMoney()
{
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 33; j++)
{
switch(Map[i][j])
{
case '$':
{
Map[i][j] = ' ';
}
}
}
}
}
void setupMoney()
{
clearMoney();
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 33; j++)
{
switch(Map[i][j])
{
case ' ':
{
choice = rand() % 30 + 1;
switch(choice)
{
case 2:
{
Map[i][j] = '$';
}
}
}
}
}
}
}
void clearObstacles()
{
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 33; j++)
{
switch(Map[i][j])
{
case 'X':
{
Map[i][j] = ' ';
}
}
}
}
}
void setupObstacles()
{
clearObstacles();
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 33; j++)
{
switch(Map[i][j])
{
case ' ':
{
choice = rand() % 15 + 1;
switch(choice)
{
case 2:
{
Map[i][j] = 'X';
}
}
}
}
}
}
}
void showMap()
{
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 33; j++)
{
printf("%c", Map[i][j]);
}
putchar('n');
}
}
void convertToAscii()
{
for(int g = 0; g < 20; g++)
{
for(int h = 0; h < 30; h++)
{
switch(Map[g][h])
{
case '#':
{
Map[g][h] = 219;
break;
}
case '@':
{
Map[g][h] = 254;
break;
}
}
}
}
}
int main()
{
srand(time(NULL));
printf("Loading...");
convertToAscii();
setupObstacles();
setupMoney();
system("cls");
for( ; ; )
{
if(GetAsyncKeyState(VK_UP))
{
switch(Map[y-1][x])
{
case ' ':
{
for(int i = 0; i < rand() % 5 + 2; i++)
{
switch(Map[y-1][x])
{
case ' ':
{
y--;
Map[y][x] = 254;
Map[y+1][x] = ' ';
showMap();
clearScreen();
Sleep(FPS);
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
level++;
break;
}
case '$':
{
score += 50;
y--;
Map[y][x] = 254;
Map[y+1][x] = ' ';
showMap();
clearScreen();
}
}
}
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
break;
}
case '$':
{
score += 50;
y--;
Map[y][x] = 254;
Map[y+1][x] = ' ';
}
}
}
else if(GetAsyncKeyState(VK_DOWN))
{
switch(Map[y+1][x])
{
case ' ':
{
for(int i = 0; i < rand() % 5 + 2; i++)
{
switch(Map[y+1][x])
{
case ' ':
{
y++;
Map[y][x] = 254;
Map[y-1][x] = ' ';
showMap();
clearScreen();
Sleep(FPS);
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
level++;
break;
}
case '$':
{
score += 50;
y++;
Map[y][x] = 254;
Map[y-1][x] = ' ';
showMap();
clearScreen();
}
}
}
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
break;
}
case '$':
{
score += 50;
y++;
Map[y][x] = 254;
Map[y-1][x] = ' ';
}
}
}
else if(GetAsyncKeyState(VK_LEFT))
{
switch(Map[y][x-1])
{
case ' ':
{
for(int i = 0; i < rand() % 5 + 2; i++)
{
switch(Map[y][x-1])
{
case ' ':
{
x--;
Map[y][x] = 254;
Map[y][x+1] = ' ';
showMap();
clearScreen();
Sleep(FPS);
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
level++;
break;
}
case '$':
{
score += 50;
x--;
Map[y][x] = 254;
Map[y][x+1] = ' ';
showMap();
}
}
}
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
level++;
break;
}
case '$':
{
score += 50;
x--;
Map[y][x] = 254;
Map[y][x+1] = ' ';
}
}
}
else if(GetAsyncKeyState(VK_RIGHT))
{
switch(Map[y][x+1])
{
case ' ':
{
for(int i = 0; i < rand() % 5 + 2; i++)
{
switch(Map[y][x+1])
{
case ' ':
{
x++;
Map[y][x] = 254;
Map[y][x-1] = ' ';
showMap();
clearScreen();
Sleep(FPS);
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
level++;
break;
}
case '$':
{
score += 50;
x++;
Map[y][x] = 254;
Map[y][x-1] = ' ';
showMap();
clearScreen();
}
}
}
break;
}
case 'X':
{
health--;
break;
}
case '!':
{
Map[y][x] = ' ';
x = 1;
y = 1;
Map[y][x] = 254;
setupObstacles();
setupMoney();
level++;
break;
}
case '$':
{
score += 50;
x++;
Map[y][x] = 254;
Map[y][x-1] = ' ';
}
}
}
showMap();
putchar('n');
printf("Health: %i", health);
putchar('n');
printf("Lives: ", lives);
putchar('n');
printf("Score: %i", score);
putchar('n');
printf("Level: %i", level);
Sleep(FPS);
clearScreen();
continue;
}
return 0;
}

我无法运行您的程序,但据我所知,您实际上从未清除屏幕,您只是在旧数据之上写入,但由于您没有写出整行,任何可能仍在屏幕上的旧数据都不会神奇地消失,除非您覆盖它。

在%i之后放一些空格,我敢打赌你的问题会消失。 试试这个:

printf("Health: %i         ", health);

您可能希望在每一行上执行此类操作,其中它并不总是写出相同数量的列。

另外,为什么不将换行符放在printf中而不是在后续putchar中?

printf("Health: %i         n", health);
相关文章: