Sooooo i have an error in my code

it dint fit all of it…

I thought you wanted to have the arrow keys for directions? For readability I suggest creating a function

bool IsKeyDown(int vKey)
{
    return (GetAsyncKeyState(vKey) & 0x80'00) != 0;
}

// ...
bKeyRight = IsKeyDown(VK_RIGHT);
1 Like

so when I try to make a bool that you told me to I tried to make it for all the keys but when I do it says " ‘IsKeyDown’: local function definitions are illegal." and also says “identifier “vKey” is undefined”

That would imply you are trying to define a function within a function. I just posted the usage of it in the same block of code the // ... was supposed to indicate “somewhere else”. Sorry if that wasn’t clear.

That is what I named the parameter.

bool IsKeyDown(int vKey)
//                 ^^^^
1 Like

ok bet

so it dint work but i was thinking your probably gonna say no (i hope you say yes) but i was thinking that you copy the code and just change it and make it work then you send it to me and explain what you did and why you did it so i can learn i hope you say yes because i am so confused plssssssssssssssssssssssssssssssssssssssssssssssssssss…

if not it is totally fine but i would appreciate if you kipped helping me till i fix it…

To get arrow key movement, the only piece of code you need to change from OLC is this

while ((chrono::system_clock::now() - t1) < ((nSnakeDirection % 2 == 1) ? 120ms : 200ms))
{
    // Get Input, 
    bKeyRight = (0x8000 & GetAsyncKeyState((unsigned char)('\x27'))) != 0;
    bKeyLeft = (0x8000 & GetAsyncKeyState((unsigned char)('\x25'))) != 0;

    if (bKeyRight && !bKeyRightOld)
    {
        nSnakeDirection++;
        if (nSnakeDirection == 4) nSnakeDirection = 0;
    }

    if (bKeyLeft && !bKeyLeftOld)
    {
        nSnakeDirection--;
        if (nSnakeDirection == -1) nSnakeDirection = 3;
    }

    bKeyRightOld = bKeyRight;
    bKeyLeftOld = bKeyLeft;
}

You have everything you need. As linked above there are macros for the keys. VK_UP would be the value for Up, VK_DOWN for down. Now determine which direction to go in based on what’s being pressed.

1 Like

ok so it kanda worked its an improvement but when i try to do it and run it the game wont load and in the log it gives and error

unresolved external symbol “bool __cdecl IsKeyLeft(int)” (?IsKeyLeft@@YA_NH@Z) referenced in function _main

and also

unresolved external symbol “bool __cdecl IsKeyRight(int)” (?IsKeyRight@@YA_NH@Z) referenced in function _main

That means you declared those functions but didn’t define them. Do you need separate functions in order to do what you want?

1 Like

i ont think so what im getting from all of this is here

	// Frame Timing, compensate for aspect ratio of command line
			auto t1 = chrono::system_clock::now();
			while ((chrono::system_clock::now() - t1) < ((nSnakeDirection % 2 == 1) ? 120ms : 200ms))
			{

				bool IsKeyLeft(int vKey);
				{
					return (GetAsyncKeyState(VK_LEFT) & 0x80'00) != 0;
				}
				
				bool IsKeyRight(int vKey);
				{
					return (GetAsyncKeyState(VK_RIGHT) & 0x80'00) != 0;
				}

				bKeyRight = IsKeyRight(VK_RIGHT);
				bKeyRight = IsKeyLeft(VK_LEFT);

is this what i suppose to do

No.

while ((chrono::system_clock::now() - t1) < ((nSnakeDirection % 2 == 1) ? 120ms : 200ms))
{
    if (IsKeyDown(VK_UP))
    {
        nSnakeDirection = 0;
    }
    if (IsKeyDown(VK_RIGHT))
    {
        nSnakeDirection = 1;
    }
    if (IsKeyDown(VK_DOWN))
    {
        nSnakeDirection = 2;
    }
    if (IsKeyDown(VK_LEFT))
    {
        nSnakeDirection = 3;
    }
}
1 Like

i think im just gonna give up on the project

rip

That + the definition of IsKeyDown provided above is the whole change needed to get arrow keys working.

1 Like

is it kind of lie this??

		while (!bDead)
		{

			bool IsKeyDown(int vKey);
			{
				return (GetAsyncKeyState(bKeyDown) & 0x80'00) != 0;
			}

			bool IsKeyDown(int vKey);
			{
				return (GetAsyncKeyState(bKeyup) & 0x80'00) != 0;
			}
			
			// Frame Timing, compensate for aspect ratio of command line
			auto t1 = chrono::system_clock::now();
			while ((chrono::system_clock::now() - t1) < ((nSnakeDirection % 2 == 1) ? 120ms : 200ms))
			{
				if (bKeyup(VK_UP))
				{
					nSnakeDirection = 0;
				}
				if (IsKeyDown(VK_RIGHT))
				{
					nSnakeDirection = 1;
				}
				if (IsKeyDown(VK_DOWN))
				{
					nSnakeDirection = 2;
				}
				if (IsKeyDown(VK_LEFT))
				{
					nSnakeDirection = 3;
				}
			}

yes it not complete but does it look like this???

No. Do you not know how to define a function?

1 Like

kind of lol

Privacy & Terms