473,424 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,424 software developers and data experts.

How to control movement on the screen

MrStephens
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.
May 3 '07 #1
10 4309
AdrianH
1,251 Expert 1GB
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
May 3 '07 #2
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.
May 3 '07 #3
AdrianH
1,251 Expert 1GB
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
May 4 '07 #4
Wow! A lot of info. Thanks. We will put this to use and I will let you know how it goes. I am also posting the code that a student came up with on his own. It doesn't turn real well, but it does "eat" dots from above or below (but not from the side).

We do C++ basics. loops, functions, string basics. We go to a contest in April and practice for that most of the second semester. After that, I pick something different to do. I tried OOP last year, and they did not get it. This year, we went to graphics instead. (Thought about JAVA, but another programming teacher I know reccommended against it. He said after C++ it is too wordy. your opinion??)

Expand|Select|Wrap|Line Numbers
  1. //Name:C S
  2. //Date:
  3. //Description: Snake
  4.  
  5. #include <iostream.h>
  6. #include <dos.h>
  7. #include <graphics.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <conio.h>
  11. void ws();
  12. void ad();
  13. void drawerase();
  14. void food();
  15. char x;
  16. int left, right, top, bottom;//rectangle
  17. int num, a, b;
  18. int move=0;//amount of boxes
  19. // food variables
  20. int food1=1;
  21. int j;
  22. int k;
  23. int food2=1;
  24. int test1=0;
  25. int test2=0;
  26. int g;//finds food hit
  27. int h;//finds food hit
  28. int gdriver = DETECT, gmode, errorcode;
  29. int xmax, ymax;
  30. int main(void)
  31.  
  32. {
  33.   /* request auto detection */
  34.  
  35.  
  36.   /* initialize graphics and local variables */
  37.   initgraph(&gdriver, &gmode, "X:/TC/BGI");
  38.  
  39.   /* read result of initialization */
  40.   errorcode = graphresult();
  41.   /* an error occurred */
  42.   if (errorcode != grOk)
  43.     {
  44.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  45.       printf("Press any key to halt:");
  46.       getch();
  47.       exit(1);
  48.     }
  49.   cout<<"W A S D to move\n";
  50.   //left top right bottom
  51.   xmax = getmaxx();
  52.   ymax = getmaxy();
  53.   left=xmax/2-15;
  54.   top=ymax/2-15;
  55.   right=xmax/2+15;
  56.   bottom=ymax/2+15;
  57.  
  58.   do
  59.     {
  60.  
  61.  
  62.       //y++;  //draw new
  63.       if (right<-5)
  64.         {
  65.           left=xmax+5;
  66.           right=xmax+35;
  67.         }
  68.       if (left>xmax+5)
  69.         {
  70.           right=-5;
  71.           left=-35;
  72.         }
  73.       if (bottom<-5)
  74.         {
  75.           top=ymax+35;
  76.           bottom=ymax+5;
  77.         }
  78.       if (bottom>ymax+5)
  79.         {
  80.           bottom=-5;
  81.           top=-35;
  82.         }
  83.       food();
  84.       //setfillstyle(SOLID_FILL, 15);
  85.       setcolor(3);
  86.       //floodfill(right-5,bottom-5, y);  //a and b create new squares
  87.       drawerase();
  88.  
  89.  
  90.  
  91.  
  92.       delay(200);
  93.       //setfillstyle(SOLID_FILL, 0);
  94.       setcolor(0);//erase
  95.       drawerase();
  96.       if(kbhit())
  97.         {
  98.           x=getch();
  99.           //floodfill(right-1,bottom-1,0);
  100.         }
  101.       switch(x)
  102.         {
  103.         case 'a':
  104.           left-=20;
  105.           right-=20;
  106.           break;
  107.  
  108.         case 's':
  109.           top+=20;
  110.           bottom+=20;
  111.           break;
  112.  
  113.         case 'd':
  114.           left+=20;
  115.           right+=20;
  116.           break;
  117.  
  118.         case 'w':
  119.           top-=20;
  120.           bottom-=20;
  121.           break;
  122.         }
  123.     }
  124.   while (x!=1);
  125.  
  126.  
  127.  
  128.  
  129.   return 0;
  130. }
  131. void food()//random food location and adds on snake if it touches
  132. {
  133.  
  134.   if (food1!=test1 || food2!=test2)
  135.     {
  136.       move++;
  137.       setcolor(0);
  138.       circle(test1, test2, 5);
  139.       randomize();
  140.       food1=rand() % xmax;
  141.       test1=food1;
  142.       food2=rand() % ymax;
  143.       test2=food2;
  144.       setcolor(15);
  145.       circle(food1, food2, 5);
  146.     }
  147.   g=0;
  148.   h=0;
  149.   switch(x)
  150.     {
  151.     case 'w':
  152.       ws();
  153.       break;
  154.  
  155.  
  156.     case 's':
  157.       ws();
  158.       break;
  159.  
  160.     case 'a':
  161.       ad();
  162.       break;
  163.  
  164.     case 'b':
  165.       ad();
  166.       break;
  167.     }
  168. }
  169.  
  170. void drawerase()
  171. {
  172.   a=0;
  173.   b=0;
  174.   num=0;
  175.  
  176.   do
  177.     {
  178.  
  179.       rectangle(left+a,top+b ,right+a ,bottom+b);
  180.       switch(x)
  181.         {
  182.         case 'a':
  183.           a+=35;
  184.           break;
  185.  
  186.         case 'w':
  187.           b+=35;
  188.           break;
  189.  
  190.         case 's':
  191.           b-=35;
  192.           break;
  193.  
  194.         case 'd':
  195.           a-=35;
  196.           break;
  197.  
  198.         }
  199.       num++;
  200.     }
  201.   while (num!=move);
  202. }
  203.  
  204. void ad()
  205. {
  206.   g=top;
  207.   h=bottom;
  208.   j=left;
  209.   k=right;
  210.   do
  211.     {
  212.       if (g==food2)
  213.         {
  214.           do
  215.             {
  216.               if (j==food1)
  217.                 {
  218.  
  219.                   sound(450);
  220.                   delay(5);
  221.                   nosound();
  222.                   //move++;
  223.                   food2++;
  224.                   break;
  225.                 }
  226.               else
  227.                 {
  228.                   h++;
  229.  
  230.                 }}
  231.           while(j<=k);
  232.         }
  233.       else
  234.         {
  235.           g++;
  236.         }
  237.     }
  238.   while(g<=h);
  239. }
  240.  
  241.  
  242. void ws()
  243. {
  244.   g=left;
  245.   h=right;
  246.   j=top;
  247.   k=bottom;
  248.   do
  249.     {
  250.       if(g==food1)
  251.         {
  252.           do
  253.             {
  254.               if (j==food2)
  255.                 {
  256.                   sound(450);
  257.                   delay(5);
  258.                   nosound();
  259.                   //move++;
  260.                   food1++;
  261.                   break;
  262.                 }
  263.  
  264.               else
  265.                 {
  266.                   j++;
  267.                 }
  268.  
  269.  
  270.             }while (j<=k);
  271.         }
  272.       else
  273.         {
  274.           g++;
  275.         }}
  276.   while(g<=h);
  277. }
May 7 '07 #5
AdrianH
1,251 Expert 1GB
Wow! A lot of info. Thanks. We will put this to use and I will let you know how it goes. I am also posting the code that a student came up with on his own. It doesn't turn real well, but it does "eat" dots from above or below (but not from the side).

We do C++ basics. loops, functions, string basics. We go to a contest in April and practice for that most of the second semester. After that, I pick something different to do. I tried OOP last year, and they did not get it. This year, we went to graphics instead. (Thought about JAVA, but another programming teacher I know reccommended against it. He said after C++ it is too wordy. your opinion??)
It's late so I will not look at the code you sent just yet.

C++ too wordy? C++ is fairly terse, not as terse as C, but still fairly terse. Maybe I am misunderstanding what he/she is meaning by wordy? Perhaps if you could supply his/her definition it would be clearer and I would be able to rebuttal. How terse does this person want the language? What language is he/she recommending?

I know kids can have a short attention span, so I'm not really that surprised you had difficulty in teaching OOP. It really takes some creative teaching to get teens to see the magic, or even the concept. Using everyday physical objects usually help in this regard.

I.e. I have a car. That car has 4 wheels. One wheel got a flat so I can replace that wheel with another. That shows UML's definition of aggregation (life span of wheel is not tied to car). The car has a roll-cage, this is an example of UML's definition of composition (life span of roll-cage is tied to car). The car is a Honda Ultima (or whatever car is considered cool), this is a specialisation of the car and inherits all of a car's general abilities, that is an example of inheritance…

The list of analogies goes on, hopefully keeping the interest of the students. When they get the basic concepts, they can go on to using that paradigm to make things. Get them to make multiple instances (a pre-existing framework may be needed so that the students can learn the basic concepts without having to programme mondo amounts of code) and have them interact with each other.

Getting the kids to programme is easy; getting them to think ahead is hard (I was one of those kids ;)). However, by getting them to see patters ahead of time, it will make them able to programme faster and cleaner and they should get excited about that.

I can see Java being difficult to teach without teaching OOP. Same with C++ unless you degrade it down to C. This is especially due to all of the libraries that are based on OOP.

If you want to try and get together (online), perhaps we can hammer out some stuff which you think the kids would be interested in learning. PM me if you are interested.

I will see if I can compile the code you posted. It would be interesting to see.

G’nite,


Adrian
May 8 '07 #6
He meant that Java was too wordy, compared to C++. I wasn't clear on that, sorry.
May 10 '07 #7
r035198x
13,262 8TB
We do C++ basics. loops, functions, string basics. We go to a contest in April and practice for that most of the second semester. After that, I pick something different to do. I tried OOP last year, and they did not get it. This year, we went to graphics instead. (Thought about JAVA, but another programming teacher I know reccommended against it. He said after C++ it is too wordy. your opinion??)
You may want to browse through this or this.
May 10 '07 #8
AdrianH
1,251 Expert 1GB
You may want to browse through this or this.
Tisk-tisk r0, you should know better than to quote uncontrollably. ;)


Adrian
May 10 '07 #9
r035198x
13,262 8TB
Tisk-tisk r0, you should know better than to quote uncontrollably. ;)


Adrian
I never read the post after posting. I'll have to be more careful next time.
May 10 '07 #10
AdrianH
1,251 Expert 1GB
He meant that Java was too wordy, compared to C++. I wasn't clear on that, sorry.
Wordy? Argh! That just pisses me off. A language should be based on its abilities, not that it is too 'wordy'. There are definitely languages that I think are just ugly, but I wordy is a valid criteria.

Take Eiffel. I think it is ugly, but it is a powerful language and has built in syntax for pre/postconditions, loop and class invariants and assertions. I still don't like it ascetically (I think the code it produces looks like a donkey's arse ;)), but it does its job well and would probably still recommenced it for projects.

Java and C++ have almost identical syntax, so Java is not wordy. The libraries may be less terse for Java, and IMHO C++ is starting down that path too.

Terseness is a bane to the programming industry. I'm sorry but writing using a bunch of letters as variable names and stupid unstandardised short forms just make for an unmanageable mess of unmaintainable code. I've been on many projects where terseness was paramount. Their poorly constructed arguments of "I'll misspell it if it gets too long", "It takes too long for me to type in 2 more letters" or even "I know what it does, why should I use long winded names?" are all a bunch of crap!

With today's auto-completion IDE's (MS VC++ uses intellisense (tm), Eclipse uses its own auto-completion, emacs (an old, but still very used editor) also has auto-completion too, makes most of these arguments useless.

As for the "I know what it does..." argument. I would fire that person (out of a cannon if possible ;)) if he was under my management. S/he, is not the only one who will have to maintain the code. S/he must make it maintainable by others! Otherwise productivity will diminish when that bus accidentally hits him/her. (Honest, I had nothing to do with it officer. :D).

Just in case you missed my point, wordyness is not a valid criteria. ;)


Adrian

P.S. If there are standardised short-forms and agreed upon uses for letters in a small context, I could live with that. That is in fact how I removed a lot of terseness in my last projects. By getting everyone on board to minimise the scope of its use. But I still use 'index' instead of 'i' for a variable name.
May 10 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Robin Shuff | last post by:
Hi, I'm trying to limit the movement of the mouse cursor in using a VB app. The idea is to stop the cursor straying on to the second monitor of a dual screen set-up (i.e. a projector) while this...
1
by: 2D Rick | last post by:
I have a form that opens maximized and requires scrolling down to see all the textboxes. When you enter a textbox in the lower portion of the form the Enter event opens a large textbox with a large...
1
by: Benny Raymond | last post by:
In my attempt to make a macro recording program where i can then playback mouse movements i'm running into a big problem: I'm trying to simulate the movement of the mouse to a point on the...
0
by: sdbranum | last post by:
I have been using Visual C#.NET to code a large project having many data adapters, data sets, datagrids, multiple forms with tab pages, each containing various controls (mostly label, text boxes,...
0
by: Chubby Arse | last post by:
Hi all, I have a control, that basically is my own version of the datagrid, except that is renderers purely readonly tabular information. When the control is rendered to the designer, I can...
5
by: hurricane_number_one | last post by:
I am creating a simple server application, that will listen for incoming mouse coordinates and then move the mouse accordingly. So basically it's like a very simple VNC server without and screen...
5
by: LurkB | last post by:
Hey , I wonder how and which programming language I can use to capture movement of a mouse on Linux screen (not just on a particular webpage user is on) like how VB, C#,C++, and J# can be used to...
2
by: LurkB | last post by:
Hey , I wonder how I could use C/C++ to capture movement of a mouse on Linux screen (not just on a particular webpage user is on). Thanks in advance L
2
by: LurkB | last post by:
Hello, I wonder how I could monitor a mouse movement on LINUX operating system screen using C# Thanks in Advance L
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.