Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- // define keyboard events
- #define KB_LEFT 75
- #define KB_RIGHT 77
- #define KB_ESCAPE 27
- #define KB_SPACE 32
- #define KB_UP 72
- #define KB_DOWN 80
- using namespace std; // saves me a lot of time
- void WriteTo(int x, int y) { // this function sets the console cursor position
- static HANDLE hStdout = NULL;
- COORD coord;
- coord.X = x;
- coord.Y = y;
- if(!hStdout)
- {
- hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- }
- SetConsoleCursorPosition(hStdout,coord); // sets console cursor position for writing
- }
- int main() // main function, most code goes here
- {
- //set x and y to 0 (cursor will be sent to 0,0)
- int x = 40;
- int y = 0;
- WriteTo(x, y);
- int KB_code = 0;
- while(KB_code != KB_ESCAPE ) // if escape key pressed, break out and end or else, loop looking for key input
- {
- if (kbhit())
- {
- KB_code = getch();
- //cout << KB_code; used to get new char number
- switch (KB_code)
- {
- case KB_SPACE: // space bar
- WriteTo(x, y);
- cout << "()";
- break;
- case KB_LEFT: // left arrow key
- x -= 1;
- if (x != -1) {
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- } else {
- x += 1;
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- }
- break;
- case KB_RIGHT: // right arrow key
- x += 1;
- if (x != 79) {
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- } else {
- x -= 1;
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- }
- break;
- case KB_UP: // up arrow key
- y -= 1;
- if (y != -1) {
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- } else {
- y += 1;
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- }
- break;
- case KB_DOWN: // down arrow key
- y += 1;
- if (y != 25) {
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- } else {
- y -= 1;
- system("cls");
- cout << "x: " << x << " y: " << y;
- WriteTo(x, y);
- cout << "()";
- }
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement