Solved

keyboard help

Posted on 1997-04-05
6
301 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.

943 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now