Hi everyone. My name is Zvjezdan Veselinovic and I am a student at Rasmussen College over in New Port Richey, Florida studying Game and Simulation Programming. For my student portfolio, I want to create a "tron-like"/ "snake-like" game called Colors and Explosions.
I have so far arrow key movement. That was no biggie. What I do not have is constant movement while my key is not pressed. Can anyone help me out please? Here is my code. I just want to bounce ideas; not have the project done for me. I can't learn that way.
.
.
.
.
// 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; }
// my main_program.cpp 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())
{
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;
}
} // end of _kbhit()
return 0;
}