代码
#include<iostream>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#include<vector>
#pragma comment(lib,"winmm.lib")
using namespace std;
HANDLE handle;
COORD crd;
const short GROUND=27;
const short SCR_WIDTH = 60;
const short HEALTH = 5;
class Vitality
{
private:
short vitality;
public:
Vitality() { vitality = HEALTH; }
void ShowVitality()
{
crd.X = SCR_WIDTH * 0.6; crd.Y = 0;
SetConsoleCursorPosition(handle, crd);
SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "HP:";
for (short i = 0; i < HEALTH; i++)
cout << (i < vitality ? "■" : "_");
SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
}
short GetWound(short n = -1)
{
vitality += n;
ShowVitality();
return vitality;
}
short GetRestore()
{
vitality = HEALTH;
ShowVitality();
return vitality;
}
};
Vitality vty;
struct Letter
{
char letter;
short x;
short y;
};
class LetterShower
{
private:
vector<Letter>letters[26];
short score;
short delay;
bool Ground(vector<Letter>::iterator l)
{
return l->y >= GROUND;
}
public:
LetterShower() { score = 0; }
void GenerateLetter()
{
Letter l = { 'A' + rand() % 26,rand() % SCR_WIDTH,1 };
letters[l.letter - 'A'].push_back(l);
}
short Fall()
{
short i;
vector<Letter>::iterator itr;
for (i = 0; i < 26; i++)
{
for (itr = letters[i].begin(); itr != letters[i].end();)
{
crd.X = itr->x; crd.Y = itr->y;
SetConsoleCursorPosition(handle, crd);
cout << ' ';
if (Ground(itr))
{
if (vty.GetWound() <= 0)
{
crd.X = 13; crd.Y = GROUND + 2;
SetConsoleCursorPosition(handle, crd);
SetConsoleTextAttribute(handle, BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
cout << "Game Over!";
SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
PlaySound(L".\\sound\\over.wav", NULL, SND_ASYNC | SND_FILENAME);
return -1;
}
itr=letters[i].erase(itr);
PlaySound(L".\\sound\\wound.wav", NULL, SND_ASYNC | SND_FILENAME);
continue;
}
itr->y++;
crd.Y = itr->y;
SetConsoleCursorPosition(handle, crd);
cout << itr->letter;
itr++;
}
}
return 0;
}
void ClearAll()
{
for (short i = 0; i < 26; i++)
while (!letters[i].empty())
letters[i].pop_back();
score = 0;
}
void ShowScore()
{
crd.X = 1; crd.Y = GROUND + 2;
SetConsoleCursorPosition(handle, crd);
cout << "Score:" << score << " ";
}
void SetDelay(short d) { delay = d; }
void Wait() { Sleep(delay); }
void Rain()
{
char ch;
vector<Letter>::iterator itr;
Repeat:
while (!_kbhit())
{
if (Fall() == -1)
return;
Wait();
if (rand() % 3 == 0)
GenerateLetter();
}
ch = _getch();
if (ch >= 'a' && ch <= 'z')
{
if (!letters[ch - 'a'].empty())
{
itr = letters[ch - 'a'].begin();
crd.X = itr->x;
crd.Y = itr->y;
SetConsoleCursorPosition(handle, crd);
cout << ' ';
letters[ch - 'a'].erase(itr);
PlaySound(L"./sound/erase.wav", NULL, SND_ASYNC | SND_FILENAME);
score++;
ShowScore();
}
}
goto Repeat;
}
};
void Pause()
{
}
void PrintLevel(short lv)
{
crd.X = 4; crd.Y = 0;
SetConsoleCursorPosition(handle, crd);
switch (lv)
{
case 0:cout << "Level:EASY"; break;
case 1:cout << "Level:INTERMEDIATE"; break;
default:cout << "Level:HARD"; break;
}
}
void Welcome()
{
crd.X = 13, crd.Y = 10;
SetConsoleCursorPosition(handle, crd);
cout << "Welcome to Type Game!";
crd.X = 10, crd.Y = 12;
SetConsoleCursorPosition(handle, crd);
cout << "Press any key to continue...";
}
short ShowMenu()
{
system("cls");
crd.X = 10; crd.Y = 10;
SetConsoleCursorPosition(handle, crd);
cout << "Which level do you want try?";
short result = 0;
char ch=0;
do
{
if (ch == 75) result = (result + 2) % 3;
if (ch == 77) result = (result + 1) % 3;
crd.X = 9; crd.Y = 12;
SetConsoleCursorPosition(handle, crd);
if (result == 0)
SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
else
SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout << " EASY ";
crd.X = 19; crd.Y = 12;
SetConsoleCursorPosition(handle, crd);
if (result == 1)
SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
else
SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout << " INTERMEDIATE ";
crd.X = 36; crd.Y = 12;
SetConsoleCursorPosition(handle, crd);
if (result == 2)
SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
else
SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout << " HARD ";
ch=_getch();
if (ch == 0)
ch = _getch();
} while (ch!=VK_RETURN);
SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
return result;
}
void DrawGround()
{
crd.X = 0; crd.Y = GROUND+1;
SetConsoleCursorPosition(handle, crd);
for (int i = 0; i < SCR_WIDTH; i++)
cout << '=';
}
int main()
{
system("mode con cols=60 lines=31");
srand(time(0));
handle = GetStdHandle(STD_OUTPUT_HANDLE);
Welcome();
_getch();
short level;
char choice;
LetterShower ls;
do
{
system("cls");
level = ShowMenu();
system("cls");
vty.GetRestore();
vty.ShowVitality();
PrintLevel(level);
DrawGround();
ls.ClearAll();
ls.ShowScore();
ls.SetDelay(300 - level * 100);
ls.Rain();
cout << "Try again?(y/n)";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
return 0;
}
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0d3d4242ce804a2c29e29db05a9013b9.png)