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
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. The return type of main is int. This is defined by the C standard. In my local copy of the C11 draft it's outlined in § 3.6.1 Main Function: An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int. This is the standard of C language, and Dev C + + strictly comply with this rule. The DOS version of Turbo C 3 does not conform to the language standard, and it does not mean that it is correct. (Note: The main front must be int, since main is preceded by an int so the main function must have a return statement). I'm having a problem on try again program.I checked the syntax and yet is still not working.am trying to make a program that will output the student's grade and when the program is finished it will ask whether to try again or not.If the input is y or Y, the program will repeat all over again.
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>
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;
while(1) //infinite
loop
loop
circle(i,j,30);
if(kbhit()) //check
if a key is pressed
if a key is pressed
Dev C Error Main Must Return Intervals Chart
ch=getch();
{
Dev C Error Main Must Return Intervals Is Termed
y=-1;
{
y=0;
}
if(ch63) //move right
x=1;
}
if(ch62) //move downward
x=0;
}
if(ch27) //exit when esc
pressed
pressed
}
j=j+y;
delay(50);
}
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
Also Read: Simple program to create a circular loading bar using graphics
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.