控制台之打字母游戏
视频链接:
https://edu.csdn.net/course/play/3397/57829
环境:
windows+vs+控制台程序
游戏介绍:
游戏开始后,落下一个字母,在掉到底部之前输出相应字母,加分;输出其他字母,扣分;无输入,当作错误输入,扣分。
继续落下字母,直到游戏结束
升级:分数增加,游戏变难,下降速度变快
升级:字母曲线下落
升级:界面
截图
代码
#include <iostream>
#include <time.h>
#include <Windows.h>
#include <conio.h>
using namespace std;
void menu(int);
inline void print(int n, char c) {
for (int i = 0; i < n; ++i) cout << c;
}
int main()
{
srand((unsigned)time(NULL));
print(10, '\n');
cout << "\t\t\t按下任意键开始游戏\n";
getchar();
char c, c2;
int col, r, max_r = 30;
int score = 0;
int s = 500;
bool b = true;
while (b) {
system("cls");
menu(score);
//随机字母,及其坐标(col,0)
c = (rand() % 26) + 'a';
col= rand() % 50;
for (r = 0; r < max_r; ++r) {
print(col, ' ');
cout << c;
if (_kbhit()) {
c2 = _getch();
if (c2 == '0') b = false;
else if (c2 == c) score += 10 * (score / 100 + 1);
else score -= 20;
break;
}
else {
Sleep((int)(s*(1 - (score / 100)*0.1)));
cout << "\b \n";// \b 回退掉输出的字母c,然后换行,减少cls刷新
continue;
}
}
if (r == max_r) score -= 10;
}
return 0;
}
void menu(int score) {
cout << "Level:" << (score / 100 + 1) << endl;
cout << "Score:" << score << endl;
cout << "Exit:" << 0 << endl;
cout << "______________________________________________\n";
}