[SOLVED] Movement on a map in a console program

Dec 10, 2008 at 11:54am
Hey guys,

I'm wondering what header file I would need to use for in a console if:

I had a map of characters in a 2d array etc. That displayed char map[][]; etc. Say i wanted to move an object on that map how would I go about it.
1
2
3
4
5
6
******************
*                *
*                *
*        &       *
*                *
******************


Now if that was the map and i wanted to move the & up how could I make a function that does this for example:

1
2
3
4
5
6
7
8
9
void MovePlayerUp()
{
    player.y--;
}

if(UPARROWisPressed)
{
    MovePlayerUp();
}


Just wondering what I need to include for console key presses?

Cheers
Last edited on Dec 10, 2008 at 2:33pm
Dec 10, 2008 at 12:15pm
Google ncurses or PDcurses.
Dec 10, 2008 at 12:31pm
I remember an old program I did a long time ago - I did this but was using something like "using namespace system and using namespace systemIO" Can't remember exactly though.
Dec 10, 2008 at 12:45pm
I'm about to give this a shot:

http://msdn.microsoft.com/en-us/library/ms685035(VS.85).aspx

1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>
#include <stdio.h>

VOID KeyEventProc(KEY_EVENT_RECORD ker)
{
    printf("Key event: ");

    if(ker.bKeyDown)
        printf("key pressed\n");
    else printf("key released\n");
}


If you know if this will work or not let me know though :)
Dec 10, 2008 at 12:54pm
So far it looks like fail lol
Dec 10, 2008 at 1:12pm
Ok i Just found this:

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
30
31
using namespace System;
using namespace System::Text;
using namespace System::IO;

do
      {
         cki = Console::ReadKey( true );
         switch ( cki.Key )
         {
            case ConsoleKey::LeftArrow:
               if ( Console::WindowLeft > 0 )
                              Console::SetWindowPosition( Console::WindowLeft - 1, Console::WindowTop );
               break;

			case ConsoleKey::UpArrow:
               if ( Console::WindowTop > 0 )
                              Console::SetWindowPosition( Console::WindowLeft, Console::WindowTop - 1 );
               break;

            case ConsoleKey::RightArrow:
               if ( Console::WindowLeft < (Console::BufferWidth - Console::WindowWidth) )
                              Console::SetWindowPosition( Console::WindowLeft + 1, Console::WindowTop );
               break;

            case ConsoleKey::DownArrow:
               if ( Console::WindowTop < (Console::BufferHeight - Console::WindowHeight) )
                              Console::SetWindowPosition( Console::WindowLeft, Console::WindowTop + 1 );
               break;
         }
      }
      while ( cki.Key != ConsoleKey::Escape );


Something like this i gather i could get to work but - not sure if it's a good idea to use this?
Dec 10, 2008 at 1:38pm
That looks like .NET to me.
If it is, then no. Don't use that.
Dec 10, 2008 at 1:52pm
See i just quickly whooped this up and got what I wanted to work. All it does is make a small map with an outline and u can move the letter P around the console screen. But from what you said Don't use that - may I ask why not?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <windows.h>

using namespace System;

struct Player
{
	int x;
	int y;
};

bool gameAlive = true;
ConsoleKeyInfo cki;

void DisplayMap(Player p);
void ResetCursorPosition(int x, int y);

int main()
{
	Player player;
	player.x = 10;
	player.y = 10;

	do
      {
         cki = Console::ReadKey( true );
         switch ( cki.Key )
         {
			case ConsoleKey::W: player.y--; DisplayMap(player); break;

			case ConsoleKey::S: player.y++; DisplayMap(player); break;

			case ConsoleKey::A: player.x--; DisplayMap(player); break;

			case ConsoleKey::D: player.x++; DisplayMap(player); break;
		 }
      }
      while ( cki.Key != ConsoleKey::Escape );

	return 0;
}

void DisplayMap(Player p)
{
	//Create The Map
	char* map[20][78];

	//Load The Map and Boarder
	for(int y = 0; y < 20; ++y)
	{
		for(int x = 0; x < 78; ++x)
		{
			map[y][x] = " ";

			map[0][x] = "#";
			map[19][x] = "#";
		}
		map[y][0] = "#";
		map[y][77] = "#";
	}

	//Player Position
	map[p.y][p.x] = "P";

	//Display The Map
	for(int y = 0; y < 20; ++y)
	{
		for(int x = 0; x < 78; ++x)
		{
			std::cout << map[y][x];
		}
		std::cout << "\n";
	}

	ResetCursorPosition(0,0);
}

void ResetCursorPosition(int x, int y)
{
	COORD CursorPosition = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
}
Dec 10, 2008 at 2:09pm
I don't think it's such a good idea to mix the two.
Also, if I told you "compile it for FreeBSD", do you could be able to repeat the process?

On a different subject, did you notice that you're using double quotes instead of single quotes in lines 53, 55, 56, 58, and 59?
Dec 10, 2008 at 2:14pm
Thanks for pointing out the single character quotes.

Is there any reason you don't think it's a good reason not to mix the two what?
As in .net with ? - Sorry helios just a little confused.

Also whats FreeBSD?
Dec 10, 2008 at 2:17pm
Just changed them to single quotes and now I'm getting errors telling me that I cannot convert char to char*
Dec 10, 2008 at 2:26pm
Change the type of map to char.

C++ mixes well with C and assembler. Anything else is preferable to call through object code or through an interpreter.
.NET has all the disadvantages of being proprietary, plus all the disadvantages of being multiplatform (as contradictory as it sounds).
If you use a portable library that's already been used in anything imaginable, you can be sure your code will compile anywhere. If you use something that depends on reverse engineering to be ported, then you're just making your life a bit harder.

FreeBSD is one of the many UNIX-likes.
Dec 10, 2008 at 2:29pm
Alright I see what your saying now and I agree with you - I'm just going to use one of my game libraries and use one of there key event handlers. As for changing the type of map to char - I had already tried that and was still getting errors. Ah well doesn't matter anyway as I'm scrapping this code.

Cheers for your help helios :)
Topic archived. No new replies allowed.