Solved

keyboard help

Posted on 1997-04-05
6
304 Views
Last Modified: 2010-04-10
I am trying to make a RPG game and in it I want to have an object move,  and I want the object to move with the keyboard. So if I want it to move up, I hit up on the keyboard and the object will move up. I need some detailed example how to do this.
0
Comment
Question by:lee5i3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 

Author Comment

by:lee5i3
ID: 1162593
Edited text of question
0
 

Expert Comment

by:reyelts
ID: 1162594
This depends upon whether you are writing a dos-based or windows
based game. In dos you can use getchar() to trap the value of the
key. In windows, you can trap WM_CHAR messages which tell you
what key or keys have been pressed. Windows will have #defines
for keys like up-arrow and down-arrow.. i.e. VK_UPARROW, etc...

  For example,

  In Dos...
    int iKeyPressed;  /* The code of the key pressed */
   
    /* Keep checking for a pressed key  
    while ( ( iKeyPressed = getchar() != 'q' ) {
      printf( "Key %c was pressed and it's code is %d\n",
              iKeyPressed,
              iKeyPressed );
    }      

    That will help you figure out the values to chek for.. i.e.
up-arrow = 65 or whatever.
 
   So you let your application do whatever it needs to do.
Then when it catches a keystroke, you pass that off to a handling
function.

0
 

Author Comment

by:lee5i3
ID: 1162595
the code you did, didnt work, when it ran it didnt give correct keyboard code, can you check on it. I wont grade you yet.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lee5i3
ID: 1162596
you see, i already know how to get the code from it, but i dont understand how to make it move.
0
 

Author Comment

by:lee5i3
ID: 1162597

0
 

Accepted Solution

by:
Phoenix020397 earned 50 total points
ID: 1162598
Here is a function that I built once it moves objects :


/* Move a character.
   keypressed - the key the user pressed 0 for function to read the key.
   CX, CY - pointers to x&y of the character.
   x0,y0,Mx,My - Borders
   step - movement step
   IxS, IyS - Image X&Y size

   The function checks to which direction the char should move by checking the
   keypressed, and then checks if it will be within the bordes before moving it.
   In graphics mode, enter a non0 imagesize.
   It checks whether the X,Y are Smaller/Greater than the image X/Y size + the step
   so that part of the image won't be outside the borders.
*/

/*Move*/ void move(char keypressed, int *CX, int *CY , int x0, int y0, int Mx, int My, int step, int IxS, int IyS)
/*Move*/ {
/*Move*/   if (keypressed == 0) keypressed = getch();
/*Move*/   if (keypressed == 0) keypressed = getch();
/*Move*/
/*Move*/     switch(keypressed)
/*Move*/     {
/*Move*/       case 80 : if (*CY <= My-step-IyS) *CY += step; break;
/*Move*/       case 72 : if (*CY >= y0+step) *CY -= step; break;
/*Move*/       case 75 : if (*CX >= x0+step) *CX -= step; break;
/*Move*/       case 77 : if (*CX <= Mx-step-IxS) *CX += step; break;
/*Move*/       case 71 : if (*CX >= x0+step) *CX -= step; if (*CY >= step+y0) *CY -= step; break;
/*Move*/       case 73 : if (*CX <= Mx-step-IxS) *CX += step; if (*CY >= step+y0) *CY -= step; break;
/*Move*/       case 79 : if (*CX >= x0+step) *CX -= step; if (*CY <= My-step-IyS) *CY += step; break;
/*Move*/       case 81 : if (*CX <= Mx-step-IxS) *CX += step; if (*CY <= My-step-IyS) *CY += step; break;
/*Move*/       case 27 : exit(1); break;
/*Move*/     }
/*Move*/
/*Move*/ }

You should give the function the parameters it requires and it automatically changes the X/Y given to it, and checks that they are within the borders that gave it.
Here is a small program part demonstrating its use:

/* program for a face to move in ansi mode. Not letting it out of the screen */

#include ..... /*include everything that is needed */

void main (void)
{
  int x,y; /* Position of the face */
  int ox, oy /* Old position of the face */

  clrscr(); x = ox = 39; y = oy = 13;  
  while(1)
  {
    gotoxy(x,y); putchar(1); /* put the face in its position */
    move(0, &x, &y, 1, 1, 79, 24, 1, 0, 0); /* change the face         X/Y position according to the key the user presses. */
    gotoxy(ox,oy); putchar(' '); /* delete the face from the old         position. */
    ox = x; oy = y;/*The new position is now the old position*/
   
    /* the next time the loop runs it will put the face in the        modified X/Y after the old position was deleted. */  
  }  
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question