自己在Linux中用C++寫了個選擇難度的打字游戲
按理來說理想輸出應該為這樣

實際輸出成了這個鬼樣,求幫忙!

代碼如下:
#define TTY_PATH "/dev/tty"
#define STTY_US "stty raw -echo -F"
#define STTY_DEF "stty -raw echo -F"
void delay(int );
char putc();
int puti();
void skin_up();
void skin_down();
int devil(int );
int get_char();
int main()
{
using namespace std;
int h;
cout << "please input your difficulty: h = " << endl;
cin >> h;
while(enginer(h));
cout << "game over" << endl;
system(STTY_DEF TTY_PATH);
return 0;
}
char putc()//生成隨機字母
{
char a;
srand((int)time(0));
a = char(rand()%26 + 97);
return a;
}
int puti()//生成隨機橫向位置
{
int a;
srand((int)time(0));
a = rand()%20;
return a;
}
void delay(int t)//延時
{
clock_t now = clock();
while(clock()-now < t);
}
int get_char()//不堵塞按鍵監測,回傳按鍵值
{
fd_set rfds;
struct timeval tv;
int ch = 0;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 10;
if(select(1, &rfds, NULL, NULL, &tv) > 0)
ch = getchar();
return ch;
}
void skin_up()
{
std::cout << "打 字 游 戲" ;
std::cout << std::endl;
std::cout << "____________________";
std::cout << std::endl;
}
void skin_down()
{
std::cout << "____________________" << std::endl;
std::cout << "enter ':'to pause, and then enter 'e' to quit." << std::endl;
}
int devil(int h)//字母運動主要函式,回傳0,1供主函式判斷
{
using namespace std;
char ele = putc();
int lenth = puti();
/*字母輸出等時下落*/
for(int i = 0; i <= 20; i++)
{
system("clear");
skin_up();
int w;
/*字母縱向位置*/
for(int i = 0; i <= 20; i++)
{
system("clear");
skin_up();
int w;
/*字母縱向位置*/
for(int j = 0; j <= i; j++)
{
cout << endl;
w = 20 - j;
}
/*字母橫向位置*/
for(int j = 0; j <= lenth; j++)
cout << ' ';
cout << ele ;
/*補行*/
for(int i = 0; i <= w; i++ ) cout << endl;
skin_down();
delay(15625*50/h);
system(STTY_US TTY_PATH);
char a;
/*監測鍵盤,判斷按鍵值*/
if(a = get_char())
{
if(a == ele)
{ system("clear");
break;
}
else if(a == ':')
{
char b = getchar();
if(b == 'e')
return 0;
}
}
}
return 1;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/104443.html
標籤:C++ 語言
上一篇:百度飛槳目標檢測7日打卡營
