#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define X 30
#define Y 20
HANDLE hOut;
struct
{
int x;
int y;
char c;
} ch[X]= {0};
SHORT _rand(SHORT N)
{
return (SHORT)((double)rand()/(RAND_MAX+1.0)*N);
}
void _say(SHORT x, SHORT y, char c)
{
COORD pos = {x, y};
SetConsoleCursorPosition(hOut, pos);
putchar(c);
}
void _setch(SHORT i)
{
ch[i].x = _rand(X)*2;
ch[i].y = 0;
ch[i].c = _rand(94)+33; //33--126
}
main()
{
srand((unsigned)time(0));
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hOut, &cursor_info);
SHORT i, x, y;
for (i=0; i<X; ++i)
_setch(i);
char c;
while (1)
{
if (kbhit())
{
c = getch();
if (c==27)
//ESC
break;
for (i=0; i<X; ++i)
{
if (ch[i].c == c)
{
_say(ch[i].x, ch[i].y, ' ');
_setch(i);
}
}
}
i = _rand(X);
_say(ch[i].x, ch[i].y++, ' ');
if (ch[i].y >= Y)
_setch(i);
_say(ch[i].x, ch[i].y, ch[i].c);
Sleep(100);
//难度
}
cursor_info.bVisible = 1;
SetConsoleCursorInfo(hOut, &cursor_info);
}
[此贴子已经被作者于2017-12-27 22:18编辑过]