C++在减去 int 时向 int 添加一个额外的数字
C++ adds an extra number to an int when subtracting it
我正在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);
相关文章:
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 包装一个对象并假装它是一个 int
- cin >> int 给定一个字符串将 int 赋值为 0
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- 我从int x[3]得到的一个非常奇怪的输出;
- 在 C++11 中利用 int*_t、int_fast*_t 和 int_least*_t 之间的差异的一个很好的例子是
- 为表示一个或多个操作的C++函数的int参数寻找类型安全的替换
- c++:如何打印出一个强制转换的 int 值?
- 通过将其中一个模板更改为 muliset 后,我可以在 PBDS 排序集上使用计数功能<int>less_equal<int>
- 查找不在标准中的第一个值::设置<int>最小-最大值
- 从 int 中剥离位时,编译器会警告一个转换,但不警告其他转换.有解决方法吗?
- 超过CPU时间限制:当MPI_Sent一个非常大的int*时
- 使用无符号int作为二进制来在c++中实现一个集
- 如何声明一个必须在C++中同时存储int和char值的2D数组
- 在int main()[c++]中实现一个递归函数
- 错误:"int pow(double,int)"与上一个声明int pov(double a,int
- CStringArray::GetAt(int index) 返回一个 const. 为什么?
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*