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.

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

  1. 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. void main()
    {
    int i=250,j=250,x=0,y=-1,ch,gd=DETECT,gm;
    initgraph(&gd,&gm,"c:turboc3bgi");
    while(1) //infinite loop
    {
    circle(i,j,30);
    outtextxy(400,400,"Press Esc to Exit…..");

    I CAN NOT UNDERSTAND THESE LINES
    PLEASE HELP
    THANKS

    1. 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.

    2. I,j is a position of your circle

      gd : graphic driver
      gm : graphic mode

      Initgraph : pointer location of gd and gm which is situated in BGI folder

      While is infinite loof end less loop

      Outtextxy outside test of the position of pixel x and pixel y

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

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

  6. 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?

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

  8. 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

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

  10. 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.

  11. 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() . 🙂

  12. When i run this program.the circle start moving upwards and the arrow keys are not working.help me fast as possible

  13. Hi there,I check your blogs named “How to Move and Control an Object Using Arrow Keys in C/C ? – The Crazy Programmer” on a regular basis.Your story-telling style is awesome, keep it up! And you can look our website about free proxy.

Leave a Comment

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