Hi everyone. My name is Zvjezdan Veselinovic and I am studying Game and Simulation Programming at Rasmussen College over in New Port Richey, Florida. My question pertains to a "Tron-like" game that I am creating. It is called "Colors and Explosions". For my game, I have arrow key movement and a moving object. My question is: "How do I make it so that when I press an arrow key, my object will constantly move until I press another arrow key?" I need help on this. Any help would be appreciated, really.
Code:// My "Things_in_background.h" header file code #include <iostream> #include <string> #include <windows.h> // resize the screen, gotoxy, delay, and so on. #include <iomanip> // GetAsyncKeyState(), GetKeyState(), and so on. #include <ctime> #include <conio.h> // _getch(), _kbhit() using namespace std; int gotoxy(int x, int y) // used to place things anywhere in the window { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD point; point.X = x-1; point.Y = y-1; SetConsoleCursorPosition(hConsole, point); return SetConsoleCursorPosition(hConsole, point); } void Continue_() { cout << "Press any key to continue... "; _getch(); } void Space_() { cout << "Press SPACE to continue... "; _getch(); } void Clear_Line() { gotoxy(5,35); cout << " " << endl; } void Up_arrow() { printf("%c", 30); } void Down_arrow() { printf("%c", 31); } void Left_arrow() { printf("%c", 17); } void Right_arrow() { printf("%c", 16); } void Object() { cout << "&" << endl; }
Thank You all for taking your time at looking at my code.Code:// my main_program.cpp file code #include "Things_in_background.h" struct Player_Presses { char up_arrow, down_arrow, left_arrow, right_arrow; }; int main() { Player_Presses player1; char movement; int objectx = 2, objecty = 2; cout << "Press the Up arrow: "; player1.up_arrow = _getch(); if(_kbhit()) { player1.up_arrow = _getch(); Up_arrow(); if(player1.up_arrow == GetAsyncKeyState(VK_UP)) { player1.up_arrow = VK_UP; } } cout << endl << endl; cout << "Press the Down arrow: "; player1.down_arrow = _getch(); if(_kbhit()) { player1.down_arrow = _getch(); Down_arrow(); if(player1.down_arrow == GetAsyncKeyState(VK_DOWN)) { player1.down_arrow = VK_DOWN; } } cout << endl << endl; cout << "Press the Left arrow: "; player1.left_arrow = _getch(); if(_kbhit()) { player1.left_arrow = _getch(); Left_arrow(); if(player1.left_arrow == GetAsyncKeyState(VK_LEFT)) { player1.left_arrow = VK_LEFT; } } cout << endl << endl; cout << "Press the Right arrow: "; player1.right_arrow = _getch(); if(_kbhit()) { player1.right_arrow = _getch(); Right_arrow(); if(player1.right_arrow == GetAsyncKeyState(VK_RIGHT)) { player1.right_arrow = VK_RIGHT; } } cout << endl << endl << endl << endl; cout << "Your arrows are..." << endl << endl << endl; cout << "Up: " << player1.up_arrow << endl << endl; cout << "Down: " << player1.down_arrow << endl << endl; cout << "Left: " << player1.left_arrow << endl << endl; cout << "Right: " << player1.right_arrow << endl << endl; Space_(); system("cls"); system("mode 100,40"); gotoxy(5,35); Space_(); Clear_Line(); gotoxy(objectx,objecty); Object(); a: gotoxy(5,35); cout << "Press any key to move piece: "; movement = _getch(); if(_kbhit()) { do { movement = _getch(); if(movement == player1.up_arrow) { objecty--; gotoxy(objectx, objecty); Object(); Clear_Line(); goto a; } if(movement == player1.down_arrow) { objecty++; gotoxy(objectx, objecty); Object(); Clear_Line(); goto a; } if(movement == player1.left_arrow) { objectx--; gotoxy(objectx, objecty); Object(); Clear_Line(); goto a; } if(movement == player1.right_arrow) { objectx++; gotoxy(objectx, objecty); Object(); Clear_Line(); goto a; } } while(objectx != 20 || objectx != 1 || objecty != 1 || objecty != 25); } // end of _kbhit() gotoxy(5,35); Continue_(); return 0; }
- Zvjezdan Veselinovic



3Likes
LinkBack URL
About LinkBacks


