Game using X-Y Quadrants

closed account (iG3b7k9E)
I know how to sort of use X-Y Quadrants and with what I know I want to make a simple game but I can't figure out how to make someone be able to move by pressing keys.

My Question:
How do you make someone be able move something across the screen using the keyboard? (Like in games.) I prefer an answer telling me how to do it without having to constantly press a key and just hold it down.
Last edited on
I am assuming that you are talking about console. The console isnt really built for that kind of applications. I would recommend you to read this article.

http://cplusplus.com/articles/G13hAqkS/

Personally i loved SDL. I started with it early, i think it is very easy to understand.

Wish you best luck!
closed account (iG3b7k9E)
I know that it is not made for games but it can be done. (I've seen consul games on YouTube.) I am going to try and make a simple game anyways but if you know of a better way to make a game please tell me.
Well in turbo c++ I have used capital 'H' and 'P' for comparison with up and down key.

A little example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void menumove(int opt)
{

if ( ch == 'H')
{

	if (move == 1)
		move = opt;
	else if (move > 1)
			move--;
}
else if (ch == 'P')
	{

	if (move == opt)
			move = 1;
		else if (move < opt)
				move++;
	}
c1 = c2 = c3 = c4 = c5 = c6 =c7= 15;
c1b = c2b = c3b = c4b = c5b = c6b =c7b= 0;
if      (move == 1) {c1 = 0; c1b = 4;}
else if (move == 2) {c2 = 0; c2b = 4;}
else if (move == 3) {c3 = 0; c3b = 4;}
else if (move == 4) {c4 = 0; c4b = 4;}
else if (move == 5) {c5 = 0; c5b = 4;}
else if (move == 6) {c6 = 0; c6b = 4;}
else if (move == 7) {c7 = 0; c7b = 4;}
}


All variables are integers.
Play with this little bit of code. If you add it as a function then loop your movement with a timer until another key pressed it will continue to move in that direction or you can just put into a recursive function so it will keep firing as long as you hold down the key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "windows.h"
#include <conio.h>

while(1)
    {
        if(kbhit())
        {
          if (GetKeyState(0x25) < 0)
		  {

				cout << "Left Pressed" ; movmentX--;
				
		  }
		  break;
        }
    }
Last edited on
As i told i woul recommend you to start with SDL. It's a very simple way to make good games in my opinion.

http://www.lazyfoo.net/SDL_tutorials/ Great SDL text tutorials. (I mostly use it for reference tho. Cause i like to learn from videos)

http://www.youtube.com/watch?v=Lb_Jy5HGMsk&feature=plcp This guy has terrible english (even worse than me) but he's the most educational youtuber ive seen. Atleast i learn alot for hes tutorials. He also makes easy projects for you to follow along with when you have come to a "Milestone".

Also allegro could be worth checking out. Tho i have no experience with it at all.

Wish you best luck.
closed account (iG3b7k9E)
Thank you everyone I'll try your ideas.
Topic archived. No new replies allowed.