?
Solved

keyboard help

Posted on 1997-04-05
6
Medium Priority
?
307 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 100 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

718 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