当我按向上箭头键时,一颗星向上移动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;
问题是睡眠暂停整个程序而不是明星移动
Edward Alejandro Medina的解决方案:
#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);
}
答案 0 :(得分:0)
让我猜一下,这是一个窗口程序(带有GetMessage / DispatchMessage循环),对吧?在这里,你永远不应该使用Sleep()或任何其他长时间运行来阻止。这会冻结整个UI,因为没有键盘输入或重绘之间发生。你可以做什么取决于你的设计,但一个简单的方法是触发一个计时器,该计时器将在稍后调用窗口过程以及你可以完成移动的位置。
另一种方法是使用线程,但那些具有完全不同的复杂性。另一个替代方案是我相信一个睡眠函数可以提供消息队列,这可以避免冻结 - 你必须检查文档中该函数的名称是什么。
答案 1 :(得分:0)
你应该做这样的事情
#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 == 1)
{
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");
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 (y == 3)
{
Map[y][x] = ' ';
y -= 1;
Map[y][x] = '*';
star_delay=0;
}
if (y == 2)
{
if(star_delay>1){
Map[y][x] = ' ';
y -= 1;
Map[y][x] = '*';
}else{
star_delay+=delta_time;
}
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);
cout<<"dt:"<<delta_time<<endl;
cout<<"sd:"<<star_delay<<endl;
}
}