c++中的半秒计时器
Half second timer in c++
我想制作一个等待半秒的计时器,我已经搜索了很长时间,但只能找到等待一整秒的计时器这是整个程序:
//Draw map
char Map11[10][50] = {"#################################################",
"################H @ H################",
"# K########### ###########K #",
"# * ######### ######### * #",
"# #",
"# #",
"# #",
"# #",
"############################################* !#",
"#################################################" };
while(stopgame == false && Level == 11)
{
//Removes everything on the screen
system("cls");
//Print
cout << "Well done you made it to level 11nn";
//Prints the map
for (int y = 0; y < 10; y++)
{
cout << Map11[y] << endl;
}
//Tells you Hp
cout << "Hp: "<< Hp << "/" << MaxHp << endl;
for (int y = 0; y<10; y++)
{
for(int x = 0; x<50; x++)
{
switch(Map11[y][x])
{
//Sets the # sign to look like a line
case '#':
{
Map11[y][x] = 221;
}
break;
//Star moving
case '*':
{
if (y == 3 && x == 2 && Direction1 == 1)
{
Map11[y][x] = ' ';
x -= 1;
Map11[y][x] = '*';
}
else if (y == 3 && x == 1)
{
Map11[y][x] = ' ';
x += 1;
Direction1 = 0;
Map11[y][x] = '*';
}
else if (y == 3 && x == 2 && Direction1 == 0)
{
Map11[y][x] = ' ';
x += 1;
Map11[y][x] = '*';
}
else if (y == 3 && x == 3)
{
Map11[y][x] = ' ';
x -= 1;
Direction1 = 1;
Map11[y][x] = '*';
}
}
break;
case '@':
{
//When up arrow pressed @ sign move up
if (GetAsyncKeyState(VK_UP) != 0)
{
int y4 = (y-1);
switch(Map11[y4][x])
{
case ' ':
{
Map11[y][x] = ' ';
y -= 1;
Map11[y4][x] = '@';
yPos = y;
xPos = x;
}break;
//When touch ! sign set level to next level
case '!':
{
Level = 11;
}break;
case 'H':
{
if (Hp < 100)
{
Hp += 20;
}
Map11[y][x] = ' ';
y -= 1;
Map11[y][x] = '@';
}break;
//When touch star subtract 20 hp and remove star
case '*':
{
Hp -= 20;
Map11[y][x] = ' ';
y -= 1;
Map11[y4][x] = '@';
}break;
}
}
//When down arrow pressed @ sign move down
if (GetAsyncKeyState(VK_DOWN) != 0)
{
int y4 = (y + 1);
switch(Map11[y4][x])
{
case ' ':
{
Map11[y][x] = ' ';
y += 1;
Map11[y4][x] = '@';
yPos = y;
xPos = x;
}break;
//When touch ! sign set level to next level
case '!':
{
Level = 11;
}break;
case 'H':
{
if (Hp < 100)
{
Hp += 20;
}
Map11[y][x] = ' ';
y += 1;
Map11[y][x] = '@';
}break;
//When touch star subtract 20 hp and remove star
case '*':
{
Hp -= 20;
Map11[y][x] = ' ';
y += 1;
Map11[y4][x] = '@';
}break;
}
}
//When right arrow pressed @ sign move right
if (GetAsyncKeyState(VK_RIGHT) != 0)
{
int x5 = (x + 1);
switch(Map11[y][x5])
{
case ' ':
{
Map11[y][x] = ' ';
x += 1;
Map11[y][x5] = '@';
yPos = y;
xPos = x;
}break;
//When touch ! sign set level to next level
case '!':
{
Level = 11;
}break;
case 'H':
{
if (Hp < 100)
{
Hp += 20;
}
Map11[y][x] = ' ';
x +=1;
Map11[y][x] = '@';
}break;
//When touch star subtract 20 hp and remove star
case '*':
{
Hp -= 20;
Map11[y][x] = ' ';
x += 1;
Map11[y][x5] = '@';
}break;
}
}
//When left arrow pressed @ sign move left
if (GetAsyncKeyState(VK_LEFT) != 0)
{
int x5 = (x - 1);
switch(Map11[y][x5])
{
case ' ':
{
Map11[y][x] = ' ';
x -= 1;
Map11[y][x5] = '@';
yPos = y;
xPos = x;
}break;
//When touch ! sign set level to next level
case '!':
{
Level = 11;
}break;
case 'H':
{
if (Hp < 100)
{
Hp += 20;
}
Map11[y][x] = ' ';
x -= 1;
Map11[y][x] = '@';
}break;
//When touch star subtract 20 hp and remove star
case '*':
{
Hp -= 20;
Map11[y][x] = ' ';
x -= 1;
Map11[y][x5] = '@';
}break;
}
}
}
}
}
}
Sleep(Gamespeed);
while(Hp == 0)
{
system("cls");
cout << "nnnnnnnnnn You died on level " << Level << "nn Better luck next time.";
}
}
这是星星移动的部分
case '*':
{
if (y == 3 && x == 2 && Direction1 == 1)
{
Map11[y][x] = ' ';
x -= 1;
Map11[y][x] = '*';
}
else if (y == 3 && x == 1)
{
Map11[y][x] = ' ';
x += 1;
Direction1 = 0;
Map11[y][x] = '*';
}
else if (y == 3 && x == 2 && Direction1 == 0)
{
Map11[y][x] = ' ';
x += 1;
Map11[y][x] = '*';
}
else if (y == 3 && x == 3)
{
Map11[y][x] = ' ';
x -= 1;
Direction1 = 1;
Map11[y][x] = '*';
}
}
break;
我想让这个星号在移动前等待半秒
从c++ 11开始,你可以做
#include <thread>
#include <chrono>
std::this_thread::sleep_for(std::chrono::milliseconds(500));
this_thread
是引用当前运行线程的函数的命名空间。其中一个函数是sleep_for
。
对于情况
相关文章:
- Linux的Cpp上的计时器
- 提升 ASIO 无法识别计时器对象
- 提升 asio 并发计时器取消问题与链
- 使用单体计时器的pthread_cond_timedwait有时会比预期晚超时
- 窗口中的微秒计时器
- 计时器是否从另一个线程启动?
- 如何在 c++ 中创建计时器
- C++回调计时器实现
- 在计时器或主线程外部的命令上销毁/替换线程
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 保留计时器集合(对象与指针)的最佳方法
- 如何在 c++ 中为我的游戏制作计时器,每 30 秒工作一次
- 如果STD :: COUT发表评论,则计时器需要更长的时间才能达到10.f秒
- 计时计时器无法正确转换秒数
- 在Qt中运行计时器5秒
- C++11中带有chrono的计时器:从纳秒到毫秒
- Playsound 跳过文件的前半秒,除非音乐播放器程序恰好正在运行
- c++中的半秒计时器
- c++ /CLI system . timers .当设置Interval为100ms时,计时器以秒为单位经过20秒
- 创建一个计时器在X秒内调用一个函数