Advertisement
BeamNG_IRC

Console game tests

Jun 9th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. // define keyboard events
  6. #define KB_LEFT 75
  7. #define KB_RIGHT 77
  8. #define KB_ESCAPE 27
  9. #define KB_SPACE 32
  10. #define KB_UP 72
  11. #define KB_DOWN 80
  12. using namespace std; // saves me a lot of time
  13.  
  14. void WriteTo(int x, int y) { // this function sets the console cursor position
  15.     static HANDLE hStdout = NULL;
  16.      COORD coord;
  17.      coord.X = x;
  18.      coord.Y = y;
  19.      if(!hStdout)
  20.      {
  21.        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  22.      }
  23.      SetConsoleCursorPosition(hStdout,coord); // sets console cursor position for writing
  24. }
  25.  
  26. int main() // main function, most code goes here
  27. {
  28.     //set x and y to 0 (cursor will be sent to 0,0)
  29.     int x = 40;
  30.     int y = 0;
  31.     WriteTo(x, y);
  32.    int KB_code = 0;
  33.    while(KB_code != KB_ESCAPE ) // if escape key pressed, break out and end or else, loop looking for key input
  34.    {
  35.      if (kbhit())
  36.       {
  37.             KB_code = getch();
  38.             //cout << KB_code; used to get new char number
  39.             switch (KB_code)
  40.             {
  41.                 case KB_SPACE: // space bar
  42.                        WriteTo(x, y);
  43.                        cout << "()";
  44.                        break;
  45.  
  46.                 case KB_LEFT: // left arrow key
  47.                     x -= 1;
  48.                     if (x != -1) {
  49.                         system("cls");
  50.                         cout << "x: " << x << " y: " << y;
  51.                         WriteTo(x, y);
  52.                         cout << "()";
  53.                     } else {
  54.                         x += 1;
  55.                         system("cls");
  56.                         cout << "x: " << x << " y: " << y;
  57.                         WriteTo(x, y);
  58.                         cout << "()";
  59.                     }
  60.                     break;
  61.  
  62.                 case KB_RIGHT: // right arrow key
  63.                     x += 1;
  64.                     if (x != 79) {
  65.                         system("cls");
  66.                         cout << "x: " << x << " y: " << y;
  67.                         WriteTo(x, y);
  68.                         cout << "()";
  69.                     } else {
  70.                         x -= 1;
  71.                         system("cls");
  72.                         cout << "x: " << x << " y: " << y;
  73.                         WriteTo(x, y);
  74.                         cout << "()";
  75.                     }
  76.                     break;
  77.  
  78.                 case KB_UP: // up arrow key
  79.                     y -= 1;
  80.                     if (y != -1) {
  81.                         system("cls");
  82.                         cout << "x: " << x << " y: " << y;
  83.                         WriteTo(x, y);
  84.                         cout << "()";
  85.                     } else {
  86.                         y += 1;
  87.                         system("cls");
  88.                         cout << "x: " << x << " y: " << y;
  89.                         WriteTo(x, y);
  90.                         cout << "()";
  91.                     }
  92.                     break;
  93.  
  94.                 case KB_DOWN: // down arrow key
  95.                     y += 1;
  96.                     if (y != 25) {
  97.                         system("cls");
  98.                         cout << "x: " << x << " y: " << y;
  99.                         WriteTo(x, y);
  100.                         cout << "()";
  101.                     } else {
  102.                         y -= 1;
  103.                         system("cls");
  104.                         cout << "x: " << x << " y: " << y;
  105.                         WriteTo(x, y);
  106.                         cout << "()";
  107.                     }
  108.                     break;
  109.             }
  110.       }
  111.   }
  112.   return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement