console game help!!!

hello i am trying to create a simple console game in c++
that moves the player up down left right when the keys are pressed
i am using windows 7 64 bit with dev c++ and my includes are iostream and windows.h

but when i run this code which i think should move 0 up and down it will let me move it up but if i go to far it will crash and will sort of let me move it down but it turns into a 1 and you cant see it.

thank you for any help in advance!

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
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <windows.h>

using namespace std;

typedef const char name;

void clear()
{system("cls");}
void pause()
{system("pause");}

void start()
{
     system("MODE CON COLS=25 LINES=22");//sets window size!
     clear();
     bool running = 1;
     const int ROWS = 10;
     const int COLUMNS = 10;
     unsigned int colup = 4;//used in if(GetAsyncKeyState(VK_UP)) statement
     unsigned int rowup = 7;//used in if(GetAsyncKeyState(VK_UP)) statement
     unsigned int coldown = 4;//used in if(GetAsyncKeyState(VK_DOWN)) statement
     unsigned int rowdown = 7;//used in if(GetAsyncKeyState(VK_DOWN)) statement
     double Map[ROWS][COLUMNS] = {
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,0,1,1,1,1,1}
            };
            while (running != 0)
            {
                  clear();
                  for (int i = 0; i < ROWS; ++i)
                  {
                      for (int k = 0; k < COLUMNS; ++k)
                      {
                          cout <<Map[i][k];
                          }
                          cout <<endl;
                      }
                      if (GetAsyncKeyState(VK_UP) != 0)//moves player up not rely working...
                      {
                                                    Map[rowup][colup] = 1;
                                                    rowup = rowup - 1;
                                                    Map[rowup][colup] = 0;
                      }
                      //not working...
                      if (GetAsyncKeyState(VK_DOWN) != 0)//moves player down
                      {
                                                    Map[rowdown][coldown] = 1;
                                                    rowdown = rowdown + 1;
                                                    Map[rowdown][coldown] = 0;
                                                    if (rowdown > 10){rowdown--;} 
                                                  }
                  }
     }
     
void help()
{
     clear();
     cout <<"Help coming soon!\n";
     pause();
     }

int main()
{
    system("title text tactics!");
    system("color 6a");
    clear();
    string input;
    cout <<"\t\t\tText tactics!\a\n";
    cout <<"Type 1 to start and 2 for help!\n: ";
    cin >> input;
    if (input == "1")
    {
              start();
              }
    else if (input == "2")
    {
         help();
         main();
         }
    else 
    {
         main();
         }
    return 0;
}
Last edited on
Interesting. I haven't tried GetaSyncKeyState yet, but there is another way to do this with the use of kbhit().
For instance, within your while loop, commands such as the following could be used.
1
2
3
4
5
6
7
if (kbhit())
        {
            char c=getch();
            //use enter to quit
            if (c==13)
                break;
        }

Instead of using macros such as VK_DOWN, it requires the actual character codes. I believe 72 and 80 are up and down, and 75 and 77 left and right. Using similar commands, I was able to get a moving object that continues moving without input to change color or speed based on various commands.
is this in the windows.h libary? as i get a error (i am using Dev c++)
Last edited on
You should not call main() from main(), or from anywhere else for that matter.
Why?
Firstly, you should not be using DevCpp. It's outdated, old, and ugly. I recommend Cross-platform Code::Blocks.

As for compiler, try MinGW(download latest one), and you will be ok. DevCpp most likely has compiler that does not include any of things from C++11.

Calling main() does not take you back to main(), it calls up another copy main(). It must not be done.

Thanks Disch for teaching me that long ago. ^_^
Last edited on
is this in the windows.h libary? as i get a error (i am using Dev c++)

I believe you need to include the conio library for this one.
#include<conio.h>
i used

if (kbhit())
{
char c=getch();
//use enter to quit
if (c==13)
break;
}

but it dose not work still i think it may be to do with the Map array and not how i get the key input?

this is now my code
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <windows.h>
#include "SF.h"
#include <conio.h>

using namespace std;

typedef const char name;

short int ycord = 7;
short int xcord = 4;


void start()
{
     SF sfobj;
     system("MODE CON COLS=25 LINES=22");//sets window size!
     sfobj.clear();
     bool running = 1;
     const int ROWS = 10;
     const int COLUMNS = 10;
     double Map[ROWS][COLUMNS] = {
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,1,1,1,1,1,1} ,
            {1,1,1,1,0,1,1,1,1,1}
            };
            while (running != 0)
            {
                  sfobj.clear();
                  for (int i = 0; i < ROWS; ++i)
                  {
                      for (int k = 0; k < COLUMNS; ++k)
                      {
                          cout <<Map[i][k];
                          }
                          cout <<endl;
                      }
                      if (kbhit())//moves player up not rely working...
                      {
                                          char keyup = getch();
                                          if (keyup == 72)
                                          {
                                                    if (ycord <= 0)
                                                    {ycord++;}
                                                    Map[ycord][ycord] = 1;
                                                    ycord = ycord - 1;
                                                    Map[xcord][xcord] = 0;
                                          }
                      }
                      //not working...
                     if (kbhit())//moves player down
                      {
                                                    char keydown = getch();
                                                    if (keydown == 80)
                                                    {
                                                          if (ycord >= 10)
                                                          {
                                                                     ycord++;
                                                                     }
                                                          Map[ycord][xcord] = 1;
                                                          ycord = ycord + 1;
                                                          Map[ycord][xcord] = 0;
                                                          }
                                                  }
                  }
     }
     
void help()
{
     SF sfobj;
     sfobj.clear();
     cout <<"Help coming soon!\n";
     sfobj.pause();
     }

int main()
{
    SF sfobj;
    system("title SF");
    sfobj.ver();
    sfobj.pause();
    system("title text tactics! (test version)");
    system("color 6a");
    sfobj.clear();
    string input;
    cout <<"\t\t\tText tactics!\a\n";
    cout <<"Type 1 to start and 2 for help!\n: ";
    cin >> input;
    if (input == "1")
    {
              start();
              }
    else if (input == "2")
    {
         help();
         main();
         }
    else 
    {
         main();
         }
    return 0;
}
It should work, but it could be complicated by the two different if statements. Try putting all your if statements for key input inside of it. Here's an example of what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (kbhit())//moves player up/down not rely working...
{
    char key= getch();
    if (key == 72)
    {
        if (ycord <= 0)
        {
            ycord++;
        }
        Map[ycord][ycord] = 1;
        ycord = ycord - 1;
        Map[xcord][xcord] = 0;
    }
    if (key == 80)
    {
        if (ycord >= 10)
        {
            ycord++;
        }
        Map[ycord][xcord] = 1;
        ycord = ycord + 1;
        Map[ycord][xcord] = 0;
    }
}

It's true you lose your easily identifed characters this way (char keyup=getch()), but it should become increasingly easy to understand as you add more and more controls. When I get home, I am going to take a closer look at my program from earlier, and I will let you know the other libraries I used. If it turns out conio wasn't the one we need, we'll find out which one we do need.

In the meantime, you can take a look at the thread where I posted it.
http://www.cplusplus.com/forum/beginner/120444/
Last edited on
Ok, I didn't get a chance the other night, but the required library is definitely one of the ones listed at the above link.
um Dev-C++ has been getting support for a couple of years now, its certainly not old and ugly.
Any ideas why kbhit works with the code at my above link, but is not working for him in Dev-C++? Personally, I am using Code:Blocks 12.11.
Last edited on
 
if (key == 72)


I strongly recommend creating some constants rather than relying on magic numbers.
If using Dev-C++, make sure it is Orwell Dev-C++
http://orwelldevcpp.blogspot.co.uk/
closed account (Dy7SLyTq)
I strongly recommend creating some constants rather than relying on magic numbers.
not to derail, but didnt you say in the nsf post that you would write your code like
1
2
3
4
//these might not be valid commands. not important though
case 0x0f:
case 0x16:
...

rather than say wrap it up in an enum
For emulation of CPU opcodes, yes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//  This:
switch(opcode)
{
case 0xA9:  LDA( Mode_Immediate() );  break;
case 0xAD:  LDA( Mode_Absolute() );   break;
}

// Is not really any less clear than this:

switch(opcode)
{
case opLdaIm:  LDA( Mode_Immediate() );  break;
case opLdaAb:  LDA( Mode_Absolute() );   break;
}



I wouldn't say the same of the discussion in this thread... where you are just supposed to remember that '72' is the key code for the Up arrow.
Last edited on
I have a couple nitpicks.
How many SF sfobjs does this program need? This seems strange that every function needs to create one. There is a possibility that those could be done as just one global variable.
Hopefully you do not want to add graphics or sound to this game in the future, because the console is a terrible medium for games.
And like mats and Disch have both said you should not be calling main.

Okay now that I have said a couple of my gripes, I shall get back to the original question.
but when i run this code which i think should move '0' up and down it will let me move it up but if i go to far it will crash and will sort of let me move it down but it turns into a 1 and you cant see it.

You could keep up with the location of the zero and the dimensions of the map to solve this problem. (You have both those things already too.) If the location changing would result in an area outside of the grid dimensions, the location should not be allowed to change in that direction.
For example if there was a negative change in x, you would want to disallow the x-coordinate being less than 0;

I have done a few things in the past with arrow keys, and it usually goes something like 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
int xdiff = 0;
int ydiff = 0;
if(/*up key pressed?*/)
    ydiff = -1;
else if(/*down key pressed?*/)
    ydiff = 1;
else if(/*left key pressed?*/)
    xdiff = -1;
else if(/*right key pressed?*/)
    xdiff = 1;

x += xdiff;
y += ydiff;
if(0 <= x && x <= MAX_X /*valid x?*/ &&
   0 <= y && y <= MAX_Y /*valid y?*/)
{
    //good move
}
else
{
    //bad move, undo change
    x -= xdiff;
    y -= ydiff;
}
Last edited on
Topic archived. No new replies allowed.