一、效果图
二、代码
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
int AllCounter, RightCounter, WrongCounter;
AllCounter = 0;
RightCounter = 0;
WrongCounter = 0;
char kw[][12] = {
{'Q','W','E','R','T','Y','U','I','O','P','[',']'},
{'A','S','D','F','G','H','J','K','L',';','\''} ,
{'Z','X','C','V','B','N','M',',','.','/'}
};
void DelVscroll();
void BanMax();
void gotoxy(int x, int y);
int DrawMap();
void xy_Start();
int xy_Exit();
void xy_Pause();
int main()
{
DelVscroll();
BanMax();
DrawMap();
xy_Start();
while (1)
{
PlayGame();
}
return 0;
}
void DelVscroll()
{
HANDLE Hand;
CONSOLE_SCREEN_BUFFER_INFO Info;
Hand = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(Hand, &Info);
SMALL_RECT rect = Info.srWindow;
COORD size = { rect.Right + 1 ,rect.Bottom + 1 };
SetConsoleScreenBufferSize(Hand, size);
}
void BanMax()
{
HWND hWnd = GetConsoleWindow();
RECT rc;
GetWindowRect(hWnd, &rc);
SetWindowLongPtr(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX);
SetWindowPos(hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, NULL);
}
void gotoxy(int x, int y)
{
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
int DrawMap()
{
printf("All:%d\nRight:%d\nWrong:%d", AllCounter, RightCounter, WrongCounter);
gotoxy(72, 0);
printf("Press Esc to exit!");
gotoxy(72, 1);
printf("Enter to pause!");
gotoxy(24, 0);
for (int i = 0; i < 12; i++)
printf("%c ", kw[0][i]);
gotoxy(26, 2);
for (int i = 0; i < 11; i++)
printf("%c ", kw[1][i]);
gotoxy(28, 4);
for (int i = 0; i < 10; i++)
printf("%c ", kw[2][i]);
gotoxy(15, 29);
for (int i = 0; i < 60; i++)
printf("=");
}
void xy_Start()
{
gotoxy(25, 13);
printf("***********************************");
gotoxy(25, 14);
printf("***** Press any key to start! *****");
gotoxy(25, 15);
printf("***********************************");
gotoxy(40, 17);
int key = getch();
}
void xy_Pause()
{
gotoxy(25, 13);
printf("***********************************");
gotoxy(25, 14);
printf("*** Press any key to continue! ***");
gotoxy(25, 15);
printf("***********************************");
gotoxy(40, 17);
int key = getch();
}
int xy_Exit()
{
int key = '#';
system("cls");
gotoxy(25, 13);
printf("***********************************");
gotoxy(25, 14);
printf("***** Are you sure to exit? *****");
gotoxy(25, 15);
printf("***********************************");
gotoxy(40, 17);
printf("Y/N:");
while (key != 'Y'&&key != 'y'&&key != 'N'&&key != 'n')
{
key = getch();
if (key == 'Y' || key == 'y')
{
return 1;
}
if (key == 'N' || key == 'n')
{
return 0;
}
}
}
int PlayGame()
{
int x, y, i, j;
srand((unsigned)time(NULL));
char ch;
char cur;
i = rand() % 3;
j = rand() % (12 - i);
cur = kw[i][j];
x = 24 + i * 2 + j * 3;
y = 6;
while (y < 24)
{
system("cls");
DrawMap();
gotoxy(x, y++);
printf("%c", cur);
Sleep(500 - RightCounter * 5);
if (kbhit())
{
ch = getch();
if (ch == cur || ch == cur + 32)
{
RightCounter++;
break;
}
else if (ch == 27)
{
if (xy_Exit())
{
system("cls");
exit(0);
}
else
{
system("cls");
xy_Pause();
}
}
else if (ch == '\r')
{
system("cls");
xy_Pause();
}
else
{
WrongCounter++;
}
}
}
AllCounter++;
if (y >= 24)
{
WrongCounter++;
}
}