![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++?](https://2.bp.blogspot.com/-Hooyx4Hb-gs/Ugk7lwuEuUI/AAAAAAAABaI/31XVpYxi9MI/s1600/How+to+Move+and+Control+an+Object+Using+Arrow+Keys+in+C-C++.jpg)
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++
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++
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.
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 <graphics.h>
#include<process.h>
#include <process.h>
#include<dos.h>
#include <dos.h>
#include<conio.h>
#include <conio.h>
void main()
无效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;
整型
i = 250,j = 250,x = 0,y = -1,ch,gd = DETECT,gm;
initgraph(&gd,&gm,”c:\turboc3\bgi”);
initgraph(&gd,&gm,“ c:\ turboc3 \ bgi”);
while(1) //infinite
loop
loop
while(1)//无限
循环
{
{
circle(i,j,30);
圆(i,j,30);
outtextxy(400,400,”Press
Esc to Exit…..”);
Esc to Exit…..”);
outtextxy(400,400,” Press
Esc退出…..”);
if(kbhit()) //check
if a key is pressed
if a key is pressed
if(kbhit())//检查
如果按下一个键
{
{
ch=getch();
ch = getch();
if(ch==57) //move upward
if(ch == 57)//向上移动
{
{
x=0;
x = 0;
y=-1;
y = -1;
}
}
if(ch==61) //move left
if(ch == 61)//向左移动
{
{
x=-1;
x = -1;
y=0;
y = 0;
}
}
if(ch==63) //move right
if(ch == 63)//向右移动
{
{
x=1;
x = 1;
y=0;
y = 0;
}
}
if(ch==62) //move downward
if(ch == 62)//向下移动
{
{
x=0;
x = 0;
y=1;
y = 1;
}
}
if(ch==27) //exit when esc
pressed
pressed
if(ch == 27)//退出esc
压
exit(0);
退出(0);
}
}
i=i+x;
i = i + x;
j=j+y;
j = j + y;
delay(50);
延迟(50);
cleardevice();
cleardevice();
}
}
}
}
Initial values of i and i和 j are 250, so that the circle will first j的初始值为250,因此圆将首先
printed at coordinate (250,250) and initial values of
在坐标(250,250)处打印,且 x and x和 y are 0 and -1 to y的初始值为0和-1至
make the circle move upward. We have used an infinite while loop and in that loop
使圆圈向上移动。 我们使用了一个无限的while循环,并且在那个循环中
we used a function called
我们使用了一个名为 kbhit() to kbhit()的函数
check if any key is pressed or not.
检查是否按下任何键。
key is pressed then 77 (
按下键,然后将77(右箭头键的 ASCII value of right arrow key) is stored in ASCII值 )存储在 ch and values of x and y become 1 and 0 ch中 ,x和y的值分别为1和0
respectively. Now value of
分别。 i 我现在的价值
increased by one and
增加一, j remains as it j保持原样
is, this make the circle to move by one coordinate in x direction. This process
就是说,这使圆在x方向上移动了一个坐标。 这个流程
is repeated again and again till any other arrow key is pressed.
一次又一次地重复此操作,直到按下任何其他箭头键。
Here we have used
在这里我们使用了 cleardevice() 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.
如果您有任何疑问或无法理解,请放心
通过在下面的评论中提问。
翻译自: https://www.thecrazyprogrammer.com/2013/08/how-to-move-and-control-object-using.html