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.
void main()
                while(1)                                                               //infinite
Esc to Exit…..”);
                                if(kbhit())                                            //check
if a key is pressed
                                                if(ch==57)                           //move upward
                                                if(ch==61)                           //move left
                                                if(ch==63)                           //move right
                                                if(ch==62)                           //move downward
                                                if(ch==27)                           //exit when esc
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.

22 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)

  2. Tanuj Ahuja

    void main()
    int i=250,j=250,x=0,y=-1,ch,gd=DETECT,gm;
    while(1) //infinite loop
    outtextxy(400,400,"Press Esc to Exit…..");


    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.

  3. Sagar Ladla

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

  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

  5. gsk989898

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

  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!!!!!

  7. Talha

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

  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?

  9. Mohit Yadav

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

  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

  11. mohd nadeem

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

  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

  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; 😀

  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.

  15. Hamza Naseer

    Sorry Admin! But I would like to point out that there are several mistakes in your Program..
    1). in initgraph() , write the 3rd parameter as “C:\\Turboc3\\BGI” instead of “C:\Turboc3\BGI” .. Because in strings ( most of the time) you have to use \\ where you want to use ‘\’ ( Remember Escape Sequences?)
    2). Nice Try ! When you attempt to take instant input from user by putting getch() under if(kbhit()) but Sir ! this is not the right way to do it … You need to use GetAsyncKeyState() . 🙂


Leave a Reply

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