如何在C / C ++中使用箭头键移动和控制对象?

How to Move and Control an Object Using Arrow Keys in C/C++?
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.
难以理解本教程。
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.
在这里,我们将使用ASCII码的概念。 下面我有
使用 turbo c ++编写了一个简单的图形 程序 。 在这个程序中一个圆圈
使用箭头键移动和控制。 因此,只需看一下代码即可。
#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;
                initgraph(&gd,&gm,”c:\turboc3\bgi”); initgraph(&gd,&gm,“ c:\ turboc3 \ bgi”);
                while(1)                                                               //infinite
loop
while(1)//无限 循环
                { {
                                circle(i,j,30); 圆(i,j,30);
                                outtextxy(400,400,”Press
Esc to Exit…..”);
outtextxy(400,400,” Press Esc退出…..”);
                                if(kbhit())                                            //check
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
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 ij are 250, so that the circle will first j的初始值为250,因此圆将首先
printed at coordinate (250,250) and initial values of
在坐标(250,250)处打印,且 x and xy 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.
如果您有任何疑问或无法理解,请放心 通过在下面的评论中提问。

翻译自: https://www.thecrazyprogrammer.com/2013/08/how-to-move-and-control-object-using.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值