如何控制屏幕上的移动 [英] How to control movement on the screen

查看:61
本文介绍了如何控制屏幕上的移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++老师,我们现在正在学期末玩图形。我从未上过课,我只是从我们的书中自学。我们的教科书(9岁)中没有提到图形。我想知道是否有办法使用键盘来控制某些东西(一个图形,一个点,一个角色)的移动,但是没有使用switch语句(正如我在许多在线可用的蛇游戏中看到的那样) 。任何帮助将不胜感激。谢谢。

解决方案


我是C ++老师,我们现在正在学期末玩图形。我从未上过课,我只是从我们的书中自学。我们的教科书(9岁)中没有提到图形。我想知道是否有办法使用键盘来控制某些东西(一个图形,一个点,一个角色)的移动,但是没有使用switch语句(正如我在许多在线可用的蛇游戏中看到的那样) 。任何帮助将不胜感激。谢谢。



您是老师吗?什么年级?


图形不是标准的一部分,所以在任何纯C ++的C ++书中都不会提到它。


你用的是什么操作系统?图形并不是微不足道的,所以在我可以帮助之前你需要给我一些关于你想要完成的事情。


Adrian


< blockquote>


你是老师?什么年级?


图形不是标准的一部分,所以在任何纯C ++的C ++书中都不会提到它。


你用的是什么操作系统?图形并不是微不足道的,所以在我可以帮助之前你需要给我一些关于你想要完成的事情。


Adrian



高中,三年级和四年级学生。我已经教了8年,但这只是我教这门课的第二年。


我们使用旧的Borland编译器。这是DOS。我正在努力将我的实验室升级到MS Visual Studio。我有10台计算机,其中6台没有磁盘驱动器并且运行Win2000并且没有连接到互联网。这些是由芝加哥熊队(朋友为他们工作)捐赠的,并且是我一年前运行Win3.1的386和486台机器的巨大升级。 4是更新的戴尔并运行XP并连接。


我挑战孩子们让事情在屏幕上移动。我们通过绘制各种图形(使用drawpoly(),circle(),line(),linerel()和rectangle()等)来完成它,然后在黑色上对齐并在其他地方重新绘制它。他们想知道我们是否可以让用户控制数字的位置。我们在网上查看了一些蛇游戏,他们使用了切换语句(案例8:移动了数字,案例2:移动了计算结果等)。它似乎非常麻烦,我想知道是否有更好的方法。



高中,三年级和老年人。我已经教了8年,但这只是我教这门课的第二年。


我们使用旧的Borland编译器。这是DOS。我正在努力将我的实验室升级到MS Visual Studio。我有10台计算机,其中6台没有磁盘驱动器并且运行Win2000并且没有连接到互联网。这些是由芝加哥熊队(朋友为他们工作)捐赠的,并且是我一年前运行Win3.1的386和486台机器的巨大升级。 4是更新的戴尔并运行XP并连接。


我挑战孩子们让事情在屏幕上移动。我们通过绘制各种图形(使用drawpoly(),circle(),line(),linerel()和rectangle()等)来完成它,然后在黑色上对齐并在其他地方重新绘制它。他们想知道我们是否可以让用户控制数字的位置。我们在网上查看了一些蛇游戏,他们使用了切换语句(案例8:移动了数字,案例2:移动了计算结果等)。这看起来非常麻烦,我想知道是否有更好的方法。



你能告诉我你在编程技术方面还涉及到什么吗?


''更好''的设计是有一个对象(蛇或其他)绘制自己,移动自己,基本上做自己需要的一切。这有助于更好的封装,从而更容易修改代码。我不知道你是否教过任何面向对象的技巧。我从来没有尝试过这个年级水平,如果你愿意,他们会对他们的反应感兴趣。


不幸的是,使用案例有时是唯一可行的方法(你可以使用)如果,但这可能会更慢)。你应该尽量减少你在switch语句中放入的代码量,这样你就应该知道如何向上,向下,向左和向右移动对象并调用这些方法,而不是将所有代码放在switch语句中。

至于使用图形:

您描述的图形功能是矢量图形,可能很慢(特别是使用填充的图形功能)。我更喜欢使用精灵,除非调整大小是一个问题,因为它们通常更简单?并且更快地使用,特别是在具有有限图形能力的慢速计算机上。矢量图形的另一个问题是,如果你有两个图像并且你在另一个上面绘制一个图像然后移动一个图像,通过使用着色它黑色技术擦除图像,它将导致aa你移动的图像的黑色轮廓出现在你没有的图像上。纠正这个问题需要当你将图像从另一个图像移开时,你必须让另一个图像重绘,但它仍然是一个明显的擦除和恢复。你可以使用分页技术来解决这个问题(我稍后会对此进行描述)。


另一方面,Sprites非常快,允许使用AND,OR之类的位操作和XOR。在互联网上,我发现了这个 doc 它显示了BGI接口。在其中我找到了这个这是一个使用精灵和XOR绘制精灵同时保持下面的图像不被删除的例子。这有点受限,因为如果在另一张图像上绘制颜色会反转。


当我在高中时,我发现''我自己如何使用按位绘制运算符,并使我后来学到的称为位掩码,让精灵移动到其他位置之上。精灵本质上是矩形的,所以,如果你有一些看起来像''A'的东西,它会在它周围的那个盒子里绘制所有东西。要阻止它显示,你必须生成一个位掩码,这是''白色''上的黑色轮廓。我将颜色放在引号中,因为它们的颜色是当前调色板当前设置的颜色。白色表示的是所有位都打开,黑色表示所有位都关闭,这是默认调色板。

然后可以通过放置在其他任何位置上绘制精灵使用AND_PUT的位掩码图像,然后使用OR_PUT放置实际图像。你可以(可能)看到,使用精灵可以非常快。


Sprites可以在应用程序的初始化阶段使用矢量图形绘制并捕获并写入/读取从磁盘读取方便,因为从磁盘读取可以更快,然后每次应用程序启动时绘制它们,但这不应该是一个大问题。


停止看到正在重绘的页面,您可以使用页面翻转。这涉及在屏幕外绘制下一帧,然后在准备好后,翻转到该页面。就像在翻书中一样,你看不到任何渲染,它似乎是瞬间的。当我这样做时它使用QBasic所以我真的需要速度。我不确定将C ++与BGI一起使用有多么必要,但如果你有兴趣,请使用 setactivepate() setvisualpage()来实现这一目标。


使用精灵不是强制性的,只需使用矢量图形和页面翻转,你可以获得相当好的效果。最小化重绘有点复杂,需要生成一个框架来处理重叠时显示的多个对象。事实上,我称之为?精灵?其实不是。一个真正的精灵实际上是一个框架的一部分,它自动进行碰撞检测,提升和降低它们超过其他精灵,并处理为您绘制图像。我只是称他们为精灵,因为他们以一种基本的方式与他们相似。


如果您有任何其他问题,请随意询问。另外,请告诉我它是怎么回事,我会感兴趣的。


祝你好运,


Adrian


I am a C++ teacher and we are playing with graphics now at the end of the semester. I have never taken a class, I just taught myself from our book. There is no graphics mentioned in our textbook (which is 9 years old). I want to know if there is a way to control the movement of something (a figure, a point, a character) using the keyboard, but not using switch statements (as I have seen in a lot of snake games that are available online). Any help would be greatly appreciated. Thanks.

解决方案

I am a C++ teacher and we are playing with graphics now at the end of the semester. I have never taken a class, I just taught myself from our book. There is no graphics mentioned in our textbook (which is 9 years old). I want to know if there is a way to control the movement of something (a figure, a point, a character) using the keyboard, but not using switch statements (as I have seen in a lot of snake games that are available online). Any help would be greatly appreciated. Thanks.

You are a teacher? What grade level?

Graphics are not part of the standard so it will not be mentioned in any C++ book that is pure C++.

What OS are you using? Graphics are not trivial, so you need to give me something as to what you wish to accomplish before I can help.


Adrian


You are a teacher? What grade level?

Graphics are not part of the standard so it will not be mentioned in any C++ book that is pure C++.

What OS are you using? Graphics are not trivial, so you need to give me something as to what you wish to accomplish before I can help.


Adrian

High school, Juniors and Seniors. I have been teaching 8 years, but this is only my second year teaching this class.

We use an old Borland compiler. It is DOS. I am working on getting my lab upgraded to MS Visual Studio. I have 10 computers, 6 of them have no disk drive and run Win2000 and are not connected to the internet. These were donated by the Chicago Bears (a friend works for them) and were a HUGE upgrade over the 386 and 486 machines running Win3.1 that I had a year ago. 4 are newer Dells and run XP and are connected.

I challenged the kids to make things move across the screen. We acomplished that by drawing various figures (using drawpoly(), circle(), line(), linerel(), and rectangle(), etc) then colring over the figure in black and redrawing it somewhere else. They wanted to know if we could have the user control where the figure went. We looked at a few snake games online and they used switch statements (case 8: moved the figure up, case 2: moved the figured down, etc). It seemed very cumbersome and I was wondering if there was a better way.


High school, Juniors and Seniors. I have been teaching 8 years, but this is only my second year teaching this class.

We use an old Borland compiler. It is DOS. I am working on getting my lab upgraded to MS Visual Studio. I have 10 computers, 6 of them have no disk drive and run Win2000 and are not connected to the internet. These were donated by the Chicago Bears (a friend works for them) and were a HUGE upgrade over the 386 and 486 machines running Win3.1 that I had a year ago. 4 are newer Dells and run XP and are connected.

I challenged the kids to make things move across the screen. We acomplished that by drawing various figures (using drawpoly(), circle(), line(), linerel(), and rectangle(), etc) then colring over the figure in black and redrawing it somewhere else. They wanted to know if we could have the user control where the figure went. We looked at a few snake games online and they used switch statements (case 8: moved the figure up, case 2: moved the figured down, etc). It seemed very cumbersome and I was wondering if there was a better way.

Can you tell me what else you have covered in terms of programming techniques?

A ''better'' design is to have an object (snake or whatever) draw itself, move itself, basically do everything it needs to itself. This lends to better encapsulation which in turn makes for easier modification to the code. I don''t know if you taught any object oriented techniques. I''ve never tried with that grade level, and would be interested in how they are responding if you are.

Unfortunately, using case is sometimes the only viable method (you could use if, but that can be slower). You should minimise the amount of code you put in a switch statement, so you should have your object know how to go up, down, left and right and call those methods instead of putting all of the code in the switch statement.

As for using graphics:
The graphic functions you are describing are vector graphics, and can be slow (esp those that use a fill). I prefer using sprites unless resizing is an issue as they are usually ?simpler? and faster to work with, esp on slow computers with limited graphic abilities. Another problem with vector graphics is that if you have two images and you draw one on top of the other and then move one off, by ''erasing'' the image using the ''colouring it black'' technique, it will cause a a black silhouette of the image you moved to appear on the image you haven''t. Correcting this would require that when you move an image off of another, you would then have to get the other to redraw, but it can be still a noticeable erasure and recovery. You can sort of correct for this using paging techniques (I?ll describe this later).

Sprites on the other hand are very fast and allow for using bit operations such as AND, OR and XOR. Looking on the Internet, I found this doc which shows the BGI interface. In it I found this which is an example using sprites and XOR to draw a sprite while keeping the image underneath from being erased. This is a bit limited though as the colours will become inverted if drawn over another image.

When I was in high-school, I ''discovered'' on my own how to use the bitwise draw operators and made what I later learned is called a bitmask to have sprites move on top of others. Sprites are rectangular in nature so, if you have something that looks like an ''A'', it will draw everything in that box surrounding it. To stop that from showing, you have to generate a bitmask, which is a ''black'' silhouette on ''white''. I put the colours in quotes because their colour is an artefact of what the current palette is currently set to. What white represents is all the bits are on, and black represents all the bits are off which is the default palette.

Drawing the sprite on top of anything else can be then accomplished by putting the bitmask image using AND_PUT, and then putting the actual image using OR_PUT. As you can (may) see, using sprites can be quite fast.

Sprites can be drawn in the initialisation phase of the app using vector graphics and captured and also be written to/read from disk which is handy as reading from disk can be faster then drawing them each time the app starts, but that shouldn?t be a big concern.

To stop seeing the page being redrawn, you can use page flipping. This involves drawing your next frame off-screen and then when you are ready, ''flip'' to that page. Just like in a flip book, you don''t see any rendering occurring, it just appears to be instantaneous. When I did that it was using QBasic so I really needed the speed. I''m not sure how necessary it is using C++ with the BGI, but if you are interested, use setactivepate() and setvisualpage() to accomplish this.

Using sprite are not mandatory, and just by using vector graphics and page flipping, you can get a fairly good effect. Minimising redraw is somewhat complex and requires generating a framework to deal with multiple objects displayed when they can overlap. Infact, what I am calling a ?sprite? is actually not. A true sprite is actually part of a framework that automatically does collision detection, raising and lowering them over other sprites and deals with the drawing of the images for you. I just call them sprites because they resemble them in a rudimentary way.

If you have any other questions, please feel free and ask. Also, please let me know how it goes, as I would be interested.

Good luck,


Adrian


这篇关于如何控制屏幕上的移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆