-1

Im working on a cmd game for fun and I've run into a problem. I've coded in the movement and gravity, but in my last if statement where I attempt to make the character seize his downward movement if above a '219' char or in other wards a block, it doesnt work. I end up still going right through the block and deleting it if I attempt to fall on it. Heres the full source code so you may run it and see what I mean.

using namespace std;

char Map[10][81] =
{

   "                                                                                ",
   "                                                                                ",
   "                                                                                ",
   "                                                                                ",
   "                                                   ###                          ",
   "                                                                                ",
   "                                                                                ",
   "                                                                                ",
   "                                                                                ",
   "################################################################################",
};

int main()
{
bool start = true;
char dood('0');
bool print(false);

int x = 1;
int y = 8;

Map [y][x] = dood;
int cnt = 0;
int Height = 0;
int space_below = Map [y + 1][x];

while(start = true)
{

    Sleep(20);
    system("cls");


    for(auto &c: Map)
    {
        for(auto &d: c)
        {
                if(d == '#')
                {
                    d = 219;
                }
                cout << d;
        }
        cout << endl;

    }
    cout << "Height: " << Height <<" units";



    if(GetAsyncKeyState(VK_RIGHT) != 0)
        {
            if(x != 79)
            {
            Map [y][x] = ' ';
            x++;
            Map [y][x] = dood;
            }
        }
    if(GetAsyncKeyState(VK_LEFT) != 0)
            {
                if(x != 0)
                {
                Map [y][x] = ' ';
                x--;
                Map [y][x] = dood;
                }
            }


    if (GetAsyncKeyState(VK_UP) != 0)
    {
        if(y < 9 && y > 0)
        {
            Map[y][x] = ' ';
            y--;
            Height++;
            Map[y][x] = dood;
        }
    }

    if(y < 8 && !GetAsyncKeyState(VK_UP))
        {
            if(space_below  != '219')
            {
                Map[y][x] = ' ';
                y++;
                Height--;
                Map[y][x] = dood;
            }
        }
}
return 0;
}

*Note: Inlude statements would work when pasting them in here, but I included iostream, windows.h, and time.h.

The last if statement is my gravity which procs if the player is not holding up, and when space_below, or the block below him is not a '219' ascii character, but but for some reason when I try to jump on that platform I made this doesn't work and that character just falls through and turns the block into a space(deletes it)!

I'm kinda new to c++, so I understand my mistake could be stupidly simple, but I haven't been able to find a fix on the internet. Any help is much appreciated :)

23scurtu
  • 138
  • 10
  • 1
    You don't re-calculate space_below. Just put "space_below = Map [y + 1][x];" right before your check "if(space_below != 219)" – Michael Jul 20 '14 at 19:32
  • I changed that as well and it yielded the same result, character still falls through the platform – 23scurtu Jul 20 '14 at 19:51
  • beside what @choeger and Michael said, did you forget your Map does not contains any 219, but sharp (#) characters ? The numeric code (ansi) of # is 35, not 219. Just print what you get in the loop at Map[y+1][x] and it should be obvious. – kriss Jul 21 '14 at 07:05
  • That changed that as well but that doesn't work either, the character is still falling through the platform. Is there an alternate way to make this block collision? – 23scurtu Jul 21 '14 at 20:58

1 Answers1

2

The answer is simple : You are comparing the literal '219' to the number 219, which are not the same thing. The tokens are actually completely different values. It is a coincidence that the compiler did not report a type error.

See this stackoverflow question for more details on what a single quote string actually is (I didn't know that myself).

Community
  • 1
  • 1
choeger
  • 3,562
  • 20
  • 33
  • I see what you mean, and I changed the comparison in my last if statement to space_below != 219. I also changed space below to a char after I saw that didnt work and I still have the same issue with the character falling through the platform. – 23scurtu Jul 20 '14 at 19:01