c++打字游戏
2016-03-18 11:21
316 查看
#include <iostream> #include <string> #include <ctime> #include <conio.h> #include <Windows.h> using namespace std; // 随机生成数字、大写字母、小写字母 char RandomChar() { int type = rand() % 3; if (type == 0) { return rand() % 10 + '0'; } if (type == 1) { return rand() % 26 + 'A'; } return rand() % 26 + 'a'; } // 将光标移到控制台(x,y)处。 void gotoxy(int x, int y) { CONSOLE_SCREEN_BUFFER_INFO cs; HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsoleOut, &cs); cs.dwCursorPosition.X = y; cs.dwCursorPosition.Y = x; SetConsoleCursorPosition(hConsoleOut, cs.dwCursorPosition); } int main() { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取控制台的句柄,用于随意改变字体颜色 cout << "游戏说明:\n"; cout << "屏幕会掉落若干字符,正确敲击越多,游戏得分也就越高,字符落到终点线为游戏结束标志\n\n"; cout << "请按任意键继续\n\n"; getch(); cout << "请按任意键开始游戏\n"; getch(); system("cls");//清除屏幕当前所有信息 bool again = 1;//判断是否重来游戏 while (again) { srand((unsigned int)time(NULL));//随机产生数字 int x[100];//记录x轴信息 int y[100];//记录y轴信息 char target[100];//记录字符信息 int score = 0;//游戏得分 cout << "请按数字键选择游戏难度:\n"; cout << "1:" << "简单" << endl; cout << "2:" << "入门" << endl; cout << "3:" << "普通" << endl; cout << "4:" << "困难" << endl; cout << "5:" << "噩梦" << endl; cout << endl; int n; cin >> n; system("cls");//清除屏幕当前所有信息 gotoxy(0, 55);//游戏得分位置 cout << "当前得分:" << score; SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY); //红色 gotoxy(20 - 2 * n, 0);//按难度定义游戏终止位置 cout << "-------终---------------点------------------线-------" << endl; SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY); //绿色 for (int i = 1; i <= 2 * n; i++) { x[i] = 0; y[i] = rand() % 50; } for (int i = 1; i <= 2 * n; i++) { target[i] = RandomChar(); } bool gameOver = 0;//判断是否游戏结束 while (gameOver == 0) { while (!kbhit()) // 当用户没按键的时候 { for (int i = 1; i <= 2 * n; i++) { gotoxy(x[i], y[i]); cout << target[i]; x[i]++; if (x[i] == 20 - 2 * n) //字符到达底部 { gameOver = 1; } } Sleep(1000 - n * 100);//按难度定义字符掉落速度 for (int i = 1; i <= 2 * n; i++) { gotoxy(x[i] - 1, y[i]); cout << ' ';//擦掉上一行的字符 } if (gameOver) { break; } } if (gameOver)//如果没有这个判断,那么当字符掉落到终点线时,如果用户没有按键,则游戏不会结束 { break; } char userHit = getch(); for (int i = 1; i <= 2 * n; i++) { if (target[i] == userHit)// 用户击中屏幕字符 { score++; gotoxy(0, 65); cout << score; gotoxy(x[i], y[i]); cout << ' ';//消除字符 // 重新生成字符 x[i] = 0; y[i] = rand() % 50; target[i] = RandomChar(); } } } SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); //白色 gotoxy(20 - 2 * n + 2, 0); cout << "很遗憾,游戏结束!\n您当前得分为:"; SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY); //绿色 cout << score; SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); //白色 cout << "分\n\n是否再挑战一次? Y/N\n"; string str; cin >> str; if (str == "N" || str == "n") { cout << "感谢使用,欢迎下次再来\n"; break; } else { system("cls");//清除屏幕当前所有信息 } } return 0; }
相关文章推荐
- c++打字游戏
- c++打字游戏
- C++学习之深入理解虚函数--虚函数表解析
- 建造者模式加模板模式实现建造小人 C++
- 浅谈C++ 虚函数列表
- Standard for Programming Language C++
- C++多线程编程
- C++/CX(Visual C++ component extensions)
- C++指针和引用
- C语言指针导学(4)——分清函数指针和指针函数
- C++用new来创建对象和非new来创建对象的区别
- 构造、解析函数
- C/C++—— 写一个函数,它的参数为指向函数的指针,返回类型也为指向函数的指针
- c++代码实现图的DFS遍历
- 关于C面向对象编程(一) 与C++内在联系
- VC自动化向导的bug2(vs2005使用Labview控件类时的错误)
- [c++][语言语法]函数模板和模板函数 及参数类型的运行时判断
- C++构造函数和拷贝构造函数等的使用
- C#调用C++写的Dll时的运行时错误解决
- C与C++程序的启动代码(startup)