How to Move and Control an Object Using Arrow Keys in C/C++?

How to Move and Control an Object Using Arrow Keys in C/C++?
Are you thinking to create a game? If yes, then you may need
to move and control an object using arrow keys. For example, if you are going
to create pacman game. In that case you have to control the pacman with the
help of arrow keys. In this tutorial I will tell you the easiest way to do
this. I have written this tutorial hoping that you already have knowledge of
c/c++ graphics. If you don’t have any idea about graphics then it will be very
difficult for you to understand this tutorial.
Here we will use concept of ASCII codes. Below I have
written a simple program in graphics using turbo c++. In this program a circle
is moved and controlled using arrow keys. So just take a look at the code.
#include<graphics.h>
#include<process.h>
#include<dos.h>
#include<conio.h>
void main()
{
                int
i=250,j=250,x=0,y=-1,ch,gd=DETECT,gm;
                initgraph(&gd,&gm,”c:\turboc3\bgi”);
                while(1)                                                               //infinite
loop
                {
                                circle(i,j,30);
                                outtextxy(400,400,”Press
Esc to Exit…..”);
                                if(kbhit())                                            //check
if a key is pressed
                                {
                                                ch=getch();
                                                if(ch==57)                           //move upward
                                                {
                                                                x=0;
                                                                y=-1;
                                                }
 
                                                if(ch==61)                           //move left
                                                {
                                                                x=-1;
                                                                y=0;
 
                                                }
 
                                                if(ch==63)                           //move right
                                                {
                                                                x=1;
                                                                y=0;
                                                }
 
                                                if(ch==62)                           //move downward
                                                {
                                                                x=0;
                                                                y=1;
                                                }
 
                                                if(ch==27)                           //exit when esc
pressed
                                                                exit(0);
                                }
                                i=i+x;
                                j=j+y;
 
                                delay(50);
                                cleardevice();
                }
}
Ok! Let’s understand what is actually happening here.
Initial values of i and j are 250, so that the circle will first
printed at coordinate (250,250) and initial values of x and y are 0 and -1 to
make the circle move upward. We have used an infinite while loop and in that loop
we used a function called kbhit() to
check if any key is pressed or not.
Initially the circle is moving upward, suppose right arrow
key is pressed then 77 (ASCII value of right arrow key) is stored in ch and values of x and y become 1 and 0
respectively. Now value of i
increased by one and j remains as it
is, this make the circle to move by one coordinate in x direction. This process
is repeated again and again till any other arrow key is pressed.
The program exit if escape (ASCII value 27) key is pressed.
Here we have used cleardevice()
function to clear previous printed data and after that circle is printed at new
coordinate which makes circle to appear as if it is moving.
If you have any doubt or unable to understand then feel free
to ask by commenting below.

20 thoughts on “How to Move and Control an Object Using Arrow Keys in C/C++?

  1. Saguna Saha

    C:UsersHPDesktopa.cpp:1:21: graphics.h: No such file or directory
    C:UsersHPDesktopa.cpp:7: error: main' must return int'

    C:UsersHPDesktopa.cpp: In function int main(...)':
    C:UsersHPDesktopa.cpp:8: error:
    DETECT' undeclared (first use this function)
    C:UsersHPDesktopa.cpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)
    C:UsersHPDesktopa.cpp:9: error: initgraph' undeclared (first use this function)
    C:UsersHPDesktopa.cpp:12: error:
    circle' undeclared (first use this function)
    C:UsersHPDesktopa.cpp:13: error: outtextxy' undeclared (first use this function)
    C:UsersHPDesktopa.cpp:43: error:
    exit' undeclared (first use this function)
    C:UsersHPDesktopa.cpp:48: error: delay' undeclared (first use this function)
    C:UsersHPDesktopa.cpp:49: error:
    cleardevice' undeclared (first use this function)

    Reply
  2. Tanuj Ahuja

    void main()
    {
    int i=250,j=250,x=0,y=-1,ch,gd=DETECT,gm;
    initgraph(&gd,&gm,"c:\turboc3\bgi");
    while(1) //infinite loop
    {
    circle(i,j,30);
    outtextxy(400,400,"Press Esc to Exit…..");

    I CAN NOT UNDERSTAND THESE LINES
    PLEASE HELP
    THANKS

    Reply
    1. Neeraj Mishra

      initgraph() is used to initialise graphics, while(1) is used for infinite loop, circle() function is used to draw a circle and outtextxy() is used to print the text.

      Reply
  3. Sagar Ladla

    I have to make menu driven program and control menu options with arrow keys so while loop will be required??

    Reply
  4. faiz

    if i want to move an object at particulars screen means only one time right and one time left after that no movement any option please help

    Reply
  5. gsk989898

    Program is not being executed….
    closegraph(), getch(), restorecrtmode() not required?

    Reply
  6. kanhaiya agrawal

    I am trying to make game similar to pacman. I tried to move the pacman using your meyhod but when it moves it leaves a black trail behind and the image also becomes black. plz help!!!!!

    Reply
  7. Talha

    Can the circle move continuously and am hit any left or right etc key then circle change their direction

    Reply
  8. Aashi Shukla

    My OS is windows8 and my graphics programs are not working with it. It is showing an error asking to use initgraph though I have used it. Can anyone help?

    Reply
  9. Mohit Yadav

    Thank u very much sir. Because of this help I came one more step close to my dream.
    Thanks a lot.

    Reply
  10. sushil

    thank youso much for this article. It helped me alot. I was having a great problem with navigating a player in my simple game project in c. Now i will start working on it. Thanks

    Reply
  11. mohd nadeem

    circle is not being controlled by left and right arrow key please do check the code….

    Reply
  12. indrajeet singh

    why we are using process.h header file and im confused about the ascii values … in c 77 is the value of “M” and we are using in the program as move right

    Reply
  13. hemnath

    man this ain’t working
    n all the code u have written is correct i think bt its preview is not a correct order ;
    plzzz check it once again; 😀

    Reply
  14. Nishchith Rai

    The output isn’t displaying,I copied down the program properly and the compiler shows no error but just as soon as I run the program it automatically closes.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *