Problem detecting intersection.

I'm having a problem in a basic snake game like program when it comes to detecting if the character piece hits a wall.

I have the following code to check for intersection:
1
2
3
intercept = getchxy(x,y+1); 
      if (intercept != 'X'){y++; break;}
      else {lives--; ch = '5';}


ch = '5' stops the piece from moving.
The getchxy function consist of this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char getchxy(int x, int y)
    {
    	CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
    	HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE);
    	if (!GetConsoleScreenBufferInfo(hStd, &screenBufferInfo))
    	    printf("GetConsoleScreenBufferInfo (%d)\n", GetLastError());
    	x = screenBufferInfo.dwCursorPosition.X;
    	y = screenBufferInfo.dwCursorPosition.Y;
    	char buf[2]; COORD c = {x,y}; DWORD num_read;
    	if(!ReadConsoleOutputCharacter(hStd,(LPTSTR)buf,1,c,(LPDWORD)&num_read)
)
    	cout << "\7Error reading console.\n";
    	char ch=buf[0];
    	return ch;
    }


Yet when I move the piece in the direction of a wall. Instead of stopping, the piece continues to move in the direction and doesn't stop as if it never even detected the wall.

Full code: http://shrib.com/imf0Jj0P
(If there's a website I should use other than the one above, please let me know.) The format would not transfer correctly when I pasted it here.
Last edited on
From what I see, your collision detection consists of checking if the character value at the current coordinates is 'X' -- which is how you represent your walls.

It's likely that the character value you attempt to get in getchxy() doesn't successfully make it to the intersection test. Have you tried adding a breakpoint for getchxy() and stepping through the function when you're at a wall's coordinates? It should become clear by looking at the local variables in the debugger whether the 'X' value at those coordinates gets successfully retrieved through GetConsoleScreenBufferInfo() and ReadConsoleOutputCharacter() (and is returned to your main() for the intersection test).
Last edited on
Topic archived. No new replies allowed.