Moving Characters with keyboard arrows

How do I move characters with keyboard arrows? I don't know how to use kbhit() if it's needed.

I'm just wanting to create a game by using arrows.

Thanks in advance!
If you're using the openGL aux library i believe there may be a key press loop that you could use.
It works similar to WinMain in the way that it loops while a certain key is pressed (I believe WinMain should loop while a window is open) and there should be a loop for each key for ease.

Failing that you could use getAsyncKeyState(int keyID)
This function returns true if the argument key ID passed to it is pressed when the function is called, returns false if not.

A few problems with this are that from computer to computer the ID's of certain keys change such as Shift, Arrows, Enter, Caps, etc... Letters on the other hand nearly always follow the ASCII code so 'W, A, S, D' should be fine.

The only other problem is that this works even when your app is out of focus, so lets say you view a different window such as internet explorer, as you're typing or navigating the webpage your character will be running around the place
I tried using getAsyncKeyState. So, I'm trying to move characters up only, but I get linker errors. It has something to do with declaring functions. Compiler used is DEV-C++.

Here's the syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <dos.h>
#include <iostream.h>
#include <conio.h>
#include <windows.h>

#pragma comment(lib,"user32.lib")
int x=1,y=1;
void clrscr();
void gotoxy(int x=1,int y=1);
int main()
{
    clrscr();
    int x=5;
    int y=4;
      if (GetAsyncKeyState(VK_UP))
      {
      clrscr();
      gotoxy(x,y);
      cout<<"*";
      y+=-1;
      }
    return(0);
   
}

Errors:

32:2 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

[Linker error] undefined reference to `clrscr()'

[Linker error] undefined reference to `clrscr()'

[Linker error] undefined reference to `gotoxy(int,int)'

32:2 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h ld returned 1 exit status

Questions:
1. What is #pragma for?
2. Why must I declare very many functions in DEV-CPP? I don't even have to declare in Borland C++

Thanks in advance, and sorry for the long reply. I believe this is a simple problem which I can't solve as a beginner as a programmer.
Could someone help me? I'm still stuck on this problem.
Well i did just type out a load of useful info and accidently hit cancel, i havent got much time now so i'll just redo the working example and hopefully you may be able to figure out the rest (sorry)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <windows.h>
using namespace std;

int x=1, y=1;
void gotoxy(int x, int y);
int main(){
    while(true){ //keeps program open until key pressed
        if(GetAsyncKeyState(VK_UP)){
            gotoxy(x, y+1);
            cout << "*"; //Dont know why you've decided to put this but whatever :)
            return 0; //closes program when key detect successful
        }
    }
}

void gotoxy(int x, int y){
    // You've declared and called this function, it must do something
    return;// If it's just to test then simply return;
}


Will post a way to display current positions and use all arrow keys later today (hopefully i can remember to do so :D )
Last edited on
Obviously I forgot to post later on thursday but here's a primitive way of using the getAsyncKeyState() function to move an object via a function to set x and y positions;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <windows.h>
using namespace std;

int x=0, y=0;
void gotoXY(int argX, int argY);
int main(){
    while(true){
        if(getAsyncKeyState(VK_UP))
            gotoXY(x, y+1);
        if(getAsyncKeyState(VK_DOWN))
            gotoXY(x, y-1);
        if(getAsyncKeyState(VK_LEFT))
            gotoXY(x-1, y);
        if(getAsyncKetState(VK_RIGHT))
            gotoXY(x+1, y);
    }
}

void gotoXY(int argX, int argY){
    x=argX;
    y=argY;
}
Last edited on
Topic archived. No new replies allowed.