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.
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.
Also Read:Â Download Turbo C++ for Windows 7 for Free
Also Read:Â C/C++ Program to Create a Digital Stopwatch
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.
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<process.h>
#include<dos.h>
#include<conio.h>
#include<conio.h>
void main()
{
               int
i=250,j=250,x=0,y=-1,ch,gd=DETECT,gm;
i=250,j=250,x=0,y=-1,ch,gd=DETECT,gm;
               initgraph(&gd,&gm,”c:\turboc3\bgi”);
               while(1)                                                              //infinite
loop
loop
               {
                               circle(i,j,30);
                               outtextxy(400,400,”Press
Esc to Exit…..”);
Esc to Exit…..”);
                               if(kbhit())                                           //check
if a key is pressed
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
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.
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.
Also Read:Â C++ Program to create an Analog Clock
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.
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.
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.
to ask by commenting below.
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)
Just copy file EGAVGA located in BIG folder in turbo folder in c to bin folder
Thq Admin
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
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.
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
HOW TO RECTIFY
LINKER ERROR:UNDEFINED SYMBOL _OUTTEXTXY IN MODULE PRO.CPP
HELP
THANKS
I have to make menu driven program and control menu options with arrow keys so while loop will be required??
Yes it will be required.
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
Program is not being executed….
closegraph(), getch(), restorecrtmode() not required?
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!!!!!
Can the circle move continuously and am hit any left or right etc key then circle change their direction
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?
Thank u very much sir. Because of this help I came one more step close to my dream.
Thanks a lot.
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
the circle is vanished when i press arrow keys??
circle is not being controlled by left and right arrow key please do check the code….
Its amazing it can be used to create games thanks.
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
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; 😀
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.
Did you use the getch() function???
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() . 🙂
It was a nice programming tutorial.
I’ve copied the whole program as it is but it’s still giving me error.
When i run this program.the circle start moving upwards and the arrow keys are not working.help me fast as possible
Same problem here…
Why the circle not responding to any key press it’s just moving upwards and getting disappeared ?
Same happening with my program
how do you know the returned values of the arrow keys?
I thought they don’t have an ascii table values?
the ascii values are wrong in this program…use hex ascii values of wasd keys it will work
What is the output of this code
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.
Dear sir i am not do it in c++ dev
Unreachable code
function should return a value
CAn any onle help me how can I resolve this?