I am starting to use the basic graphics in C, and I am trying to get keyboard input to work right. I want something like getch() to recognize what key I pressed and save it in a variable so I can use that variable to control an object's movement on the screen. Here I have a basic box moving code.
Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
using namespace std;int main(void)
{
int gd=DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
int ch;
ch=getch();/*can i use this to bind getch() to a variable?*/
int x=200, y=300, x2=400, y2=400;
while(1==1){
getch();
cleardevice();
if(ch==/*need help here*/){
x+=5;
y-=10;
x2+=5;
y2-=10;
}
else if(ch==/*need help here*/){
x-=5;
y+=10;
x2-=5;
y2+=10;
}
rectangle(x,y,x2,y2);
}
}
I put comments in the code to show you where I need help. I don
t know if I can even bind a variable to getch(). I have searched the web for this and haven't found a good answer.