Advertisement
Felanpro

typeracer game(incomplete v2)

Mar 16th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <thread>
  4. #include <windows.h>
  5. #include <string>
  6. #include <conio.h> //Gain access to getch()
  7.  
  8. using namespace std;
  9.  
  10. //Typeracer game
  11. double timer = 0;
  12. bool end_game = false;
  13. int errors;
  14. int error_words;
  15. int temporary_errors = 0;
  16.  
  17. void hellothere()
  18. {
  19.     while (!end_game)
  20.     {
  21.         if (timer == 30)
  22.         {
  23.             end_game = true;
  24.             system("cls");
  25.         }
  26.         Sleep(1000);
  27.         timer++;
  28.     }
  29. }
  30.  
  31. char word[5];
  32. char input_from_player[5];
  33.  
  34. void intitialize_word()
  35. {
  36.     srand(time(NULL));
  37.     for (int x = 0; x < 5; x++)
  38.     {
  39.         word[x] = (rand() % 25) + 97;
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.     double points = 0;
  46.  
  47.     thread clock(hellothere);
  48.  
  49.     while (!end_game)
  50.     {
  51.         intitialize_word();
  52.         cout << word << endl;
  53.  
  54.         for (int k = 0; k < 5; k++)
  55.         {
  56.             if (k > 0)
  57.                 cout << word[k - 1];
  58.  
  59.             while (input_from_player[k] != word[k])
  60.             {
  61.                 input_from_player[k] = _getch();
  62.                 if (input_from_player[k] != word[k])
  63.                 {
  64.                     errors++;
  65.                     temporary_errors = 1;
  66.                 }
  67.             }
  68.         }
  69.  
  70.         if (temporary_errors == 1)
  71.         {
  72.             error_words++;
  73.             temporary_errors = 0;
  74.         }
  75.  
  76.         points++;
  77.  
  78.         if (points == 10)
  79.         {
  80.             end_game = true;
  81.         }
  82.  
  83.         system("cls");
  84.     }
  85.  
  86.     clock.join(); //End time
  87.  
  88.     double words_per_minute = (points/timer) * 60;
  89.  
  90.     cout << "You typed 10 words over a period of " << timer << " seconds" << endl;
  91.     cout << "This estimates to " << words_per_minute << " words per minute." << endl;
  92.     cout << "Number of erros: " << errors << endl;
  93.     cout << "Number of words messed up on: " << error_words;
  94.  
  95.     int pause; cin >> pause; //Pause the game
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement