Solved

keyboard help

Posted on 1997-04-05
6
302 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 168
c++ syntax question 9 48
Least Squares Curve Fitting 4 77
c++ reading data from file into two dimensional array 3 109
  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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

773 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