Advertisement

打字小游戏c++

阅读量:

代码:

复制代码
 #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;
    
 }

全部评论 (0)

还没有任何评论哟~