当我按下向上箭头键a星向上移动2次,但我想在第1次和第2次移动恒星之间添加一个延迟,这是代码:
#include <iostream>
#include <windows.h>
using namespace std;
char Map[10][20] = {"###################",
"#### #########!#",
"#### # ######### #",
"####* # ######### #",
"# # ######### #",
"# ##### ######### #",
"# ##### ######### #",
"# ##### ######### #",
"#@##### #",
"###################" };
int Gamespeed = 100;
int Level = 1;
bool stopgame = false;
int Hp = 100;
int MaxHp = 100;
int main()
{
while(stopgame == false && Level == 1)
{
system("cls");
for (int y = 0; y < 10; y++)
{
cout << Map[y] << endl;
}
cout << "Hp: "<< Hp << "/" << MaxHp << endl;
for (int y = 0; y<10; y++)
{
for(int x = 0; x<20; x++)
{
switch(Map[y][x])
{
case '#':
{
Map[y][x] = 219;
}
break;
case '*':
{
if (GetAsyncKeyState(VK_UP) != 0)
{
if (y = 3)
{
Map[y][x] = ' ';
y -= 1;
Map[y][x] = '*';
}
Sleep(1000);
if (y = 2)
{
Map[y][x] = ' ';
y -= 1;
Map[y][x] = '*';
}
}
}
break;
case '@':
{
if (GetAsyncKeyState(VK_UP) != 0)
{
int y2 = (y-1);
switch(Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = '@';
}break;
case '!':
{
Level = 2;
}break;
case '*':
{
Hp -= 20;
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = '@';
}break;
}
}
if (GetAsyncKeyState(VK_DOWN) != 0)
{
int y2 = (y + 1);
switch(Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y += 1;
Map[y2][x] = '@';
}break;
case '!':
{
Level = 2;
}break;
case '*':
{
Hp -= 20;
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = '@';
}break;
}
}
if (GetAsyncKeyState(VK_RIGHT) != 0)
{
int x2 = (x + 1);
switch(Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x += 1;
Map[y][x2] = '@';
}break;
case '!':
{
Level = 2;
}break;
case '*':
{
Hp -= 20;
Map[y][x] = ' ';
x -= 1;
Map[y][x2] = '@';
}break;
}
}
if (GetAsyncKeyState(VK_LEFT) != 0)
{
int x2 = (x - 1);
switch(Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x -= 1;
Map[y][x2] = '@';
}break;
case '!':
{
Level = 2;
}break;
case '*':
{
Hp -= 20;
Map[y][x] = ' ';
x -= 1;
Map[y][x2] = '@';
}break;
}
}
}
}
}
}
Sleep(Gamespeed);
}这是使恒星移动的代码
case '*':
{
if (GetAsyncKeyState(VK_UP) != 0)
{
if (y = 3)
{
Map[y][x] = ' ';
y -= 1;
Map[y][x] = '*';
}
Sleep(1000);
if (y = 2)
{
Map[y][x] = ' ';
y -= 1;
Map[y][x] = '*';
}
}
}
break;问题是睡眠暂停整个节目,而不是明星移动
的解决方案:
#include <iostream>
#include <windows.h>
#include <chrono>
using namespace std;
char Map[10][20] = {"###################",
"#### #########!#",
"#### # ######### #",
"####* # ######### #",
"# # ######### #",
"# ##### ######### #",
"# ##### ######### #",
"# ##### ######### #",
"#@##### #",
"###################" };
int Gamespeed = 100;
int Level = 1;
bool stopgame = false;
int Hp = 100;
int MaxHp = 100;
int main()
{
chrono::time_point<chrono::system_clock> current_time,last_time;
float delta_time,star_delay=0;
current_time=chrono::system_clock::now();
last_time=current_time;
while(stopgame == false && Level == 6)
{
current_time=chrono::system_clock::now();
delta_time=chrono::duration_cast<chrono::duration<float>>(current_time-last_time).count();
last_time=current_time;
system("cls");
cout << "Well done you made it to level 6\n\n";
for (int y = 0; y < 10; y++)
{
cout << Map6[y] << endl;
}
cout << "Hp: "<< Hp << "/" << MaxHp << endl;
for (int y = 0; y<10; y++)
{
for(int x = 0; x<20; x++)
{
switch(Map6[y][x])
{
case '#':
{
Map6[y][x] = 219;
}
break;
case '*':
{
if (y == 8 && x == 12)
{
Map6[y][x] = ' ';
y -= 1;
Map6[y][x] = '*';
star_delay=0;
}
if (y == 7 && x == 12)
{
Map6[y][x] = ' ';
y -= 1;
Map6[y][x] = '*';
star_delay=0;
}
if (y == 6 && x == 12)
{
Map6[y][x] = ' ';
x += 1;
Map6[y][x] = '*';
star_delay=0;
}
if (y == 6 && x == 13)
{
Map6[y][x] = ' ';
y += 1;
Map6[y][x] = '*';
star_delay=0;
}
if (y == 7 && x == 13)
{
Map6[y][x] = ' ';
y += 1;
Map6[y][x] = '*';
star_delay=0;
}
if (y == 8 && x == 13)
{
if(star_delay>1){
Map6[y][x] = ' ';
x -= 1;
Map6[y][x] = '*';
}else{
star_delay+=delta_time;
}
}
}
break;
case '@':
{
if (GetAsyncKeyState(VK_UP) != 0)
{
int y4 = (y-1);
switch(Map6[y4][x])
{
case ' ':
{
Map6[y][x] = ' ';
y -= 1;
Map6[y4][x] = '@';
}break;
case '!':
{
Level = 7;
}break;
case '*':
{
Hp -= 20;
Map6[y][x] = ' ';
y -= 1;
Map6[y4][x] = '@';
}break;
}
}
if (GetAsyncKeyState(VK_DOWN) != 0)
{
int y4 = (y + 1);
switch(Map6[y4][x])
{
case ' ':
{
Map6[y][x] = ' ';
y += 1;
Map6[y4][x] = '@';
}break;
case '!':
{
Level = 7;
}break;
case '*':
{
Hp -= 20;
Map6[y][x] = ' ';
y -= 1;
Map6[y4][x] = '@';
}break;
}
}
if (GetAsyncKeyState(VK_RIGHT) != 0)
{
int x5 = (x + 1);
switch(Map6[y][x5])
{
case ' ':
{
Map6[y][x] = ' ';
x += 1;
Map6[y][x5] = '@';
}break;
case '!':
{
Level = 7;
}break;
case '*':
{
Hp -= 20;
Map6[y][x] = ' ';
x -= 1;
Map6[y][x5] = '@';
}break;
}
}
if (GetAsyncKeyState(VK_LEFT) != 0)
{
int x5 = (x - 1);
switch(Map6[y][x5])
{
case ' ':
{
Map6[y][x] = ' ';
x -= 1;
Map6[y][x5] = '@';
}break;
case '!':
{
Level = 7;
}break;
case '*':
{
Hp -= 20;
Map6[y][x] = ' ';
x -= 1;
Map6[y][x5] = '@';
}break;
}
}
}
}
}
}
Sleep(Gamespeed);
while(Hp == 0)
{
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n you died on level " << Level << "\n\n better luck next time.";
}
}
return 0;
}
}
}
}
}
Sleep(Gamespeed);
}发布于 2014-11-15 08:24:54
让我猜猜,这是一个窗口程序(带有GetMessage/DispatchMessage循环),对吗?在这里,您不应该阻塞,无论是使用睡眠()还是任何其他长时间运行的操作。这冻结了整个UI,因为之间没有键盘输入或重绘。您能做什么取决于您的设计,但一个简单的方法是触发一个定时器,将调用窗口proc稍后,并在哪里您可以完成移动。
另一种方法是使用线程,但这些线程具有完全不同的复杂性。另一种选择是,我相信一个为消息队列提供信息的睡眠函数,这将避免冻结--您必须检查该函数的名称是什么文档。
https://stackoverflow.com/questions/26943282
复制相似问题