keyboard interface

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uppili
    New Member
    • Feb 2007
    • 26

    keyboard interface

    hi,

    i am writing a code in c. for that i prepare a tank object . i used the key bord interface to move. i don it very well, but when ever i am pressing the butten its moving but after moving one step it become invisible. here i am giveing cod.


    kindly help me pls
    Code:
    #include <graphics.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include<dos.h>
    int main(void)
    {
       /* request auto detection */
       int gdriver = DETECT, gmode, errorcode;
       int midx, midy;
       int  radius =30;
       int i=1,j=0,k=0;
       char ch;
        /* initialize graphics and local variables */
       initgraph(&gdriver, &gmode, "");
    
         while(1)
         {
    	  if((ch=getch())==75)
    		{
    	      j=j+10;
    	      if(j<550){  setcolor(EGA_BLACK);
    				  circle(j+100,90,radius);
    		setcolor(EGA_BLACK);
    				   rectangle(j+131,88,j+161,95);
    		 setcolor(EGA_BLACK);
    				   rectangle(j+50,40,j+130,140);
    		 setcolor(EGA_BLACK);
    				   arc(j+60,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(j+60,151,181,360,10);
    		 setcolor(EGA_BLACK);
    			   arc(j+80,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(j+80,151,181,360,10);
    		 setcolor(EGA_BLACK);
    			   arc(j+100,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(j+100,151,181,360,10);
    		 setcolor(EGA_BLACK);
    			   arc(j+120,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(j+120,151,181,360,10);
    
    	      }
    		i=i+10;
    	 if(i<550){
    		setcolor(EGA_WHITE);
    				   circle(i+100,90,radius);
    		setcolor(EGA_BLUE);
    				   rectangle(i+131,88,i+161,95);
    		setcolor(EGA_YELLOW);
    				   rectangle(i+50,40,i+130,140);
    		setcolor(EGA_BLUE);
    				   arc(i+60,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				arc(i+60,151,181,360,10);
    		setcolor(EGA_BLUE);
    			   arc(i+80,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				   arc(i+80,151,181,360,10);
    		setcolor(EGA_BLUE);
    			   arc(i+100,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				   arc(i+100,151,181,360,10);
    		setcolor(EGA_BLUE);
    			   arc(i+120,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				   arc(i+120,151,181,360,10);
    delay(15);
    	       setcolor(EGA_BLACK);
    				  circle(i+100,90,radius);
    		setcolor(EGA_BLACK);
    				   rectangle(i+131,88,i+161,95);
    		 setcolor(EGA_BLACK);
    				   rectangle(i+50,40,i+130,140);
    		 setcolor(EGA_BLACK);
    				   arc(i+60,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(i+60,151,181,360,10);
    		 setcolor(EGA_BLACK);
    			   arc(i+80,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(i+80,151,181,360,10);
    		 setcolor(EGA_BLACK);
    			   arc(i+100,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(i+100,151,181,360,10);
    		 setcolor(EGA_BLACK);
    			   arc(i+120,151,0,180,10);
    		 setcolor(EGA_BLACK);
    				   arc(i+120,151,181,360,10);
    
    		      }
    		      k=k+9;
    		      if(k<550)
    		      {
    		setcolor(EGA_WHITE);
    				   circle(k+100,90,radius);
    		setcolor(EGA_BLUE);
    				   rectangle(k+131,88,k+161,95);
    		setcolor(EGA_YELLOW);
    				   rectangle(k+50,40,k+130,140);
    		setcolor(EGA_BLUE);
    				   arc(k+60,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				arc(k+60,151,181,360,10);
    		setcolor(EGA_BLUE);
    			   arc(k+80,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				   arc(k+80,151,181,360,10);
    		setcolor(EGA_BLUE);
    			   arc(k+100,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				   arc(k+100,151,181,360,10);
    		setcolor(EGA_BLUE);
    			   arc(k+120,151,0,180,10);
    		setcolor(EGA_MAGENTA);
    				   arc(k+120,151,181,360,10);    }
    
    		      else
    		      {break;}
    
          /* clean up */
    }
    }   getch();
       closegraph();
       return ch;
    
      }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    what should the program do and what keys do you use to control it?

    Comment

    • uppili
      New Member
      • Feb 2007
      • 26

      #3
      i created a tank which contain rect and a circle in side and 4 weels one bomb out let.
      and i want to move that object by using key.

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by uppili
        hi,

        i am writing a code in c. for that i prepare a tank object . i used the key bord interface to move. i don it very well, but when ever i am pressing the butten its moving but after moving one step it become invisible. here i am giveing cod.
        I'm sorry to say, but your coding style is unreadable. The braces don't line up (big in this case because of the length of it), and what exactly are these if statements doing? I've bitched at programmers for far less problems coding style problems (like huge switch statements for one). And I don't do it just for kicks either. When the programmers fix the code only to make it more readable, they actually find mistakes that they have done because it becomes easier to understand as to what they are doing.

        In industry, writing a programme is only 20% of the problem, the rest is design and maintenance. With a poor design, you get much higher maintenance costs in both time and money.

        I'm sorry, but unless you clean up your code by breaking down the solution into smaller parts (functions are good for that) and comment things that are not absolutely blatant as to what they are for (Variables i, k, and j? Either use variable names that have meaning or comment them as to what they are for), I'm not going to bother with this question.


        Adrian

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Adrian is right, this code is very poorly written. I see what you are doing with i,j, and k. Is this OpenGL? I don't see where you are taking input from your user to to incorporate into the various shape methods. What you should be doing is something like this:

          Assuming that sphere(x, y, z, size) creates a sphere at those xyz coords of size size.

          Code:
          //psudocode
          while(true)
          {
              //initialize xValue, yValue, zValue to something here
              Initialize();
              ResetCurrentDrawingToBlank();
          
              switch(getInputFromUser())//returns a value representing a step in x y or z direction
          
              case x:
                  xValue ++;
               break;
              case y:
                  yValue ++;
               break;
              case z:
                  zVaue ++;
              break;
              
              setColor(COLOR_BLACK);
              sphere (xValue, yValue, zValue, 10);
          }
          Now everytime it loops it gets a step input from you user, this updates the drawing of where the sphere is.

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            That code is much better RedSon (though I'm not sure what the setColor() call was for).

            Anyway, modularising is defiantly the way to go. Oh and instead of using 75, Uppili, you should either use the character constant 'K' (why did you want a capital k? or are you using a non-English keyboard?) or use a variable or constant name. The variable or constant name is a better choice because it allows you to change it later, even allow the user to remap the keyboard (in the case of a variable).

            Now I would like to apologise if I was a bit harsh in my last posting in this thread. I feel frustrated when I see programmers start programming without thinking, because I know that they can do better. However, I do not want to discourage people posting here when they have a question. Just try and be cleaner in your code by thinking about the design before hand.

            The Wild West is on the way out for programming. Shoot from the hip programmes are usually only good for one shot deals, and by that I mean code and throw away, i.e. no maintenance code.

            I cannot emphasise the following enough: think about what you want to accomplish, get yourself a Design Patterns book as they will show you the most used patterns in coding. It will help you in your programming carrier (or even hobby) as how to make a piece of code into a masterpiece, one that can be used and maintained easily.


            Adrian

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Originally posted by AdrianH
              That code is much better RedSon (though I'm not sure what the setColor() call was for).

              Anyway, modularising is defiantly the way to go. Oh and instead of using 75, Uppili, you should either use the character constant 'K' (why did you want a capital k? or are you using a non-English keyboard?) or use a variable or constant name. The variable or constant name is a better choice because it allows you to change it later, even allow the user to remap the keyboard (in the case of a variable).

              Now I would like to apologise if I was a bit harsh in my last posting in this thread. I feel frustrated when I see programmers start programming without thinking, because I know that they can do better. However, I do not want to discourage people posting here when they have a question. Just try and be cleaner in your code by thinking about the design before hand.

              The Wild West is on the way out for programming. Shoot from the hip programmes are usually only good for one shot deals, and by that I mean code and throw away, i.e. no maintenance code.

              I cannot emphasise the following enough: think about what you want to accomplish, get yourself a Design Patterns book as they will show you the most used patterns in coding. It will help you in your programming carrier (or even hobby) as how to make a piece of code into a masterpiece, one that can be used and maintained easily.


              Adrian

              I threw setColor in there because when I did some OpenGL programming I always threw in the color of what I want right before I drew it. There's no telling what color you are going to get when its time to draw unless you specify right before drawing

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                Originally posted by RedSon
                I threw setColor in there because when I did some OpenGL programming I always threw in the color of what I want right before I drew it. There's no telling what color you are going to get when its time to draw unless you specify right before drawing
                Oh, I see. For a simple sphere, it makes sense, however if this were a tank object (which can be arbitrarily complex with multiple colours and such) I would probably tell the tank what colour it is, put the colour setting into the object's drawing function and have it deal with it.

                Adrian

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #9
                  Yes, I would consider that a very good design. Lets hope others on this thread would take note of your insight.

                  Comment

                  • uppili
                    New Member
                    • Feb 2007
                    • 26

                    #10
                    Code:
                    >#include <graphics.h>
                    #include <stdlib.h>
                    #include <stdio.h>
                    #include <conio.h>
                    #include<dos.h>
                    int main(void)
                    {
                       /* request auto detection */
                       int gdriver = DETECT, gmode;
                       
                       int  radius =30;
                       int i=4,j=2,k=4;
                       char ch;
                        /* initialize graphics and local variables */
                       initgraph(&gdriver, &gmode, "");
                    
                         while(1)
                         {
                    	  if((ch=getch())==75) /* L arrow   75 is the value of key*/
                    		{
                    		i=i+1;
                    
                    	   j++;
                    	      if(j<500){  setcolor(EGA_BLACK);
                    				  circle(j+100,90,radius);
                    		setcolor(EGA_BLACK);
                    				   rectangle(j+131,88,j+161,95);
                    		 setcolor(EGA_BLACK);
                    				   rectangle(j+50,40,j+130,140);
                    		 setcolor(EGA_BLACK);
                    				   arc(j+60,151,0,180,10);
                    		 setcolor(EGA_BLACK);
                    				   arc(j+60,151,181,360,10);
                    		 setcolor(EGA_BLACK);
                    			   arc(j+80,151,0,180,10);
                    		 setcolor(EGA_BLACK);
                    				   arc(j+80,151,181,360,10);
                    		 setcolor(EGA_BLACK);
                    			   arc(j+100,151,0,180,10);
                    		 setcolor(EGA_BLACK);
                    				   arc(j+100,151,181,360,10);
                    		 setcolor(EGA_BLACK);
                    			   arc(j+120,151,0,180,10);
                    		 setcolor(EGA_BLACK);
                    				   arc(j+120,151,181,360,10);
                    
                    	      }
                    		      k=k+1;
                    		      if(k<500)
                    		      {
                    		setcolor(EGA_WHITE);
                    				   circle(k+100,90,radius);
                    		setcolor(EGA_BLUE);
                    				   rectangle(k+131,88,k+161,95);
                    		setcolor(EGA_YELLOW);
                    				   rectangle(k+50,40,k+130,140);
                    		setcolor(EGA_BLUE);
                    				   arc(k+60,151,0,180,10);
                    		setcolor(EGA_MAGENTA);
                    				arc(k+60,151,181,360,10);
                    		setcolor(EGA_BLUE);
                    			   arc(k+80,151,0,180,10);
                    		setcolor(EGA_MAGENTA);
                    				   arc(k+80,151,181,360,10);
                    		setcolor(EGA_BLUE);
                    			   arc(k+100,151,0,180,10);
                    		setcolor(EGA_MAGENTA);
                    				   arc(k+100,151,181,360,10);
                    		setcolor(EGA_BLUE);
                    			   arc(k+120,151,0,180,10);
                    		setcolor(EGA_MAGENTA);
                    				   arc(k+120,151,181,360,10);    }
                    
                    		      else
                    		      {break;}
                    
                          /* clean up */
                    }
                    }   getch();
                       closegraph();
                       return ch;
                    
                      }
                    this the way we have to create the inter face.

                    Comment

                    • uppili
                      New Member
                      • Feb 2007
                      • 26

                      #11
                      this code is depend on the previous code and a small mistake ot the i is not passing any thing in that program we can remove it

                      Comment

                      • uppili
                        New Member
                        • Feb 2007
                        • 26

                        #12
                        Mr. AdrianH


                        I created the tank by using different color because its a game and i just want to show the different between the user and computer object. it is not possible to write the code shorter than this because " it's depend on the object "


                        i got a problem at the first code now in second code it was solved.

                        sorry for poor English. English is not my mother ton.

                        Comment

                        • RedSon
                          Recognized Expert Expert
                          • Jan 2007
                          • 4980

                          #13
                          Why dont you try creating something called DrawTank() and as a parameter to DrawTank() you can pass the origin of the tank. So DrawTank(float xLocation, float yLocation)? Then you can concentrate on draw tank that is separate from the logic in your main method. Also you can build your tank independently of the main.

                          Comment

                          • AdrianH
                            Recognized Expert Top Contributor
                            • Feb 2007
                            • 1251

                            #14
                            Originally posted by RedSon
                            Why dont you try creating something called DrawTank() and as a parameter to DrawTank() you can pass the origin of the tank. So DrawTank(float xLocation, float yLocation)? Then you can concentrate on draw tank that is separate from the logic in your main method. Also you can build your tank independently of the main.
                            I'm not sure, but I think that uppili is saying that it is required that he does it this way as per the assignment's requirements. Though this is bad enough when you do text stuff, when you go in to graphics, it will become really messy.

                            Am I understanding you right uppili? Also, don't worry about your English, it may take a little longer, but we can still help you.


                            Adrian

                            Comment

                            • uppili
                              New Member
                              • Feb 2007
                              • 26

                              #15
                              thank you for your kind Mr.AdrianH.

                              by friend suggest me to use RTO.


                              PLS give your advice

                              Comment

                              Working...