【发布时间】:2017-09-17 11:46:56
【问题描述】:
我正在制作一个“点击游戏”。这是我和学校一起做的第一个真正的游戏。我完成了所有游戏代码,但我希望在后台有一个循环,每秒添加 geldps(money per second)。
我尝试了线程,但我并不真正理解它,我们要到明年才能了解它,所以我问,是否有人可以告诉我一种更好的方法来在后台创建一个独立于主线程运行的循环程序,并且可以每秒添加geldps 到凝胶。谢谢。
PS:我很抱歉德语变量。如果您不知道 sth 是什么意思,请问我,它可能没有很好的组织和一切。
#include <stdio.h>
int geldps=0,geld=0;
int main()
{
int stand=0, oil=0, Mine=0, Bank=0,standzahl=100, Minezahl=500, Bankzahl=1000, oilzahl=10000, Werkzeug=0, Werkzeugzahl=10;
char input, input2;
float faktor;
do
{
system("cls");
faktor=1+Werkzeug/10;
printf("%c%c%c%c%c%c%c%c%c%c%c\n",201,205,205,205,205,205,205,205,205,205,187);
printf(" %d$\n",geld);
printf("%c%c%c%c%c%c%c%c%c%c%c\n",200,205,205,205,205,205,205,205,205,205,188);
printf(" Space to get money\n U to go to Upgrades\n Escape to leave");
input=getch();
if(input==32)
{
geld=geld+faktor;
continue;
}
if(input == 117 || input == 85)
{
system("cls");
do
{
system("cls");
printf(" 0 - Tools(10 for 1 more Money)(%d)(%d$)\n 1 - Lemon Stands(%d)(%d$)\n 2 - Mines(%d)(%d$)\n 3 - Banks(%d)(%d$)\n 4 - Oil Refinerys(%d)(%d$)\nBackspace to go back", Werkzeug, Werkzeugzahl, stand, standzahl, Mine, Minezahl, Bank, Bankzahl, oil, oilzahl);
input2=getch();
if(input2== 48)
{
if(geld<Werkzeugzahl)
{
system("cls");
printf("Not enough money(%d/%d$)\n",Werkzeugzahl,geld);
system("pause");
continue;
}
geld=geld-Werkzeugzahl;
Werkzeug++;
Werkzeugzahl=Werkzeugzahl+Werkzeugzahl/10;
}
if(input2== 49)
{
if(geld<standzahl)
{
system("cls");
printf("Not enough money(%d/%d$)\n",standzahl,geld);
system("pause");
continue;
}
geld=geld-standzahl;
stand++;
standzahl=standzahl+standzahl/10;
}
if(input2== 50)
{
if(geld<Minezahl)
{
system("cls");
printf("Not enough money(%d/%d$)\n",Minezahl,geld);
system("pause");
continue;
}
geld=geld-Minezahl;
Mine++;
Minezahl=Minezahl+Minezahl/10;
geldps=geldps+1;
}
if(input2== 51)
{
if(geld<Bankzahl)
{
system("cls");
printf("Not enough money(%d/%d$)\n",Bankzahl,geld);
system("pause");
continue;
}
geld=geld-Bankzahl;
Bank++;
Bankzahl=Bankzahl+Bankzahl/10;
geldps=geldps+10;
}
if(input2== 52)
{
if(geld<oilzahl)
{
system("cls");
printf("Not enough money(%d/%d$)\n",oilzahl,geld);
system("pause");
continue;
}
geld=geld-oilzahl;
oil++;
oilzahl=oilzahl+oilzahl/10;
geldps=geldps+100;
}
}
while(input2!=8);
}
}
while(input!=27);
return 0;
}
【问题讨论】:
-
看看
alarm()函数。尽管如此,这仍会增加使用信号的需求,这最终可能会比学习线程更复杂。 -
编译时,始终启用警告,然后修复这些警告。 (对于
gcc,至少使用:-Wall -Wextra -pedantic -Werror -Wconversion -std=gnu11 -
函数:
system在 stdlib.h 头文件中找到。所以代码需要有声明:#include <stdlib.h>。函数:getch()是一个非标准函数(可以在 ch50.h 和/或 ncurses.h 中找到)但是,建议使用在 stdio.h 中找到的getchar() -
为了便于阅读和理解:1) 遵循公理:每行只有一个语句,并且(最多)每个语句一个变量声明。 2) 单独的代码块: (for、if、else、while、do...while、switch、case、default)通过一个空行。对 ascii 字符使用
int值会使代码变得晦涩难懂。建议使用实际的 ascii 值。 Ilke 'u' 而不是 117 等 -
shell 命令
cls和pause是特定于Windows 操作系统的。建议使用 ASCII 转义命令(即使它们不是通用的)来清除屏幕并使用getchar()(在清空标准输入流之后)使程序等待用户输入击键,注意:escape不退出程序。也许你的意思是<ctrl><c>
标签: c