Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <conio.h>
- #include <windows.h> //look about alternative for this library
- int headpos_x=0, headpos_y=0;
- int lastChar = 119;
- int direction = 1;
- int gamespeed = 500;
- void checkDir()
- {
- int key;
- key = _getch();
- if ((key == 72) || (key == 75) || (key == 77) || (key == 80) || (key == 119) || (key == 97) || (key == 115) || (key == 100))
- {
- lastChar = key;
- }
- }
- void changeDir()
- {
- switch (lastChar)
- {
- case 119: if (direction != 2) direction = 1; break; //w
- case 72: if (direction != 2) direction = 1; break; //upar
- case 115: if (direction != 1) direction = 2; break; //s
- case 80: if (direction != 1) direction = 2; break; //dnar
- case 100: if (direction != 4) direction = 3; break; //d
- case 77: if (direction != 4) direction = 3; break; //rar
- case 97: if (direction != 3) direction = 4; break; //a
- case 75: if (direction != 3) direction = 4; break; //lftar
- }
- }
- void movePos()
- {
- switch (direction)
- {
- case 1: ++headpos_y; break;
- case 2: --headpos_y; break;
- case 3: ++headpos_x; break;
- case 4: --headpos_x; break;
- }
- }
- int main()
- {
- while (true)
- {
- checkDir();
- changeDir();
- while(true)
- {
- if (_kbhit()==true) break;
- movePos();
- Sleep(gamespeed);
- std::cout << "x " << headpos_x << " y " << headpos_y << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement