SHARE
TWEET

Untitled

a guest Jan 5th, 2019 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <windows.h> //look about alternative for this library
  5. int headpos_x=0, headpos_y=0;
  6. int lastChar = 119;
  7. int direction = 1;
  8. int gamespeed = 500;
  9.  
  10. void checkDir()
  11. {
  12.     int key;
  13.     key = _getch();
  14.     if ((key == 72) || (key == 75) || (key == 77) || (key == 80) || (key == 119) || (key == 97) || (key == 115) || (key == 100))
  15.     {
  16.         lastChar = key;
  17.     }
  18. }
  19.        
  20. void changeDir()
  21. {
  22.     switch (lastChar)
  23.     {
  24.         case 119: if (direction != 2) direction = 1; break; //w
  25.         case 72:  if (direction != 2) direction = 1; break; //upar
  26.         case 115: if (direction != 1) direction = 2; break; //s
  27.         case 80:  if (direction != 1) direction = 2; break; //dnar
  28.         case 100: if (direction != 4) direction = 3; break; //d
  29.         case 77:  if (direction != 4) direction = 3; break; //rar
  30.         case 97:  if (direction != 3) direction = 4; break; //a
  31.         case 75:  if (direction != 3) direction = 4; break; //lftar
  32.     }
  33. }
  34.  
  35. void movePos()
  36. {
  37.     switch (direction)
  38.     {
  39.         case 1: ++headpos_y; break;
  40.         case 2: --headpos_y; break;
  41.         case 3: ++headpos_x; break;
  42.         case 4: --headpos_x; break;
  43.     }
  44. }
  45. int main()
  46. {
  47.     while (true)
  48.     {
  49.         checkDir();
  50.         changeDir();
  51.         while(true)
  52.         {
  53.             if (_kbhit()==true) break;
  54.             movePos();
  55.             Sleep(gamespeed);
  56.             std::cout << "x " << headpos_x << " y " << headpos_y << "\n";
  57.         }
  58.     }
  59.     return 0;
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top