CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 23

Threaded View

  1. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Snake

    Quote Originally Posted by vinayak4gargya View Post
    I DID try debugging only to get harrowing results!
    Do more debugging. I don't see what's difficult in seeeing why values in an array are not correct.
    After the MoveSnake function call the topmost elements of the array remain unchanged!
    We don't know what the original values are before the function call, or the values you are sending to the MoveSnake function, so there is no way for us to tell you what you're doing wrong. If you took that MoveSnake function by itself, called the function with known arguments, what do you get?

    Also, I see no relation between the WM_PAINT code and the MoveSnake code.
    if i add another static variable to the code)
    You are adding static variables in the hope that it fixes the problem?

    First, make a simple program -- it doesn't have to be a full blown Windows program. Then take that MoveSnake() function and call it with known parameters. Start off with a smaller two dimensional array:
    Code:
    #include <windows.h>
    #define BLOCK  1  // or whatever it is in your code
    
    void MoveSnake(POINT pt[10][2],int iDirect,int iLength)
    {
        int i;
    	
        for(i = iLength-1 ; i > 0 ; i--)
        {
             pt[i][0] = pt[i-1][0];
             pt[i][1] = pt[i-1][1];
        }
        //Move the first block (i.e. head)	
        switch(iDirect)
        {
           case 0:
    	pt[0][0].y = pt[1][0].y + BLOCK;
    	pt[0][1].y = pt[1][1].y + BLOCK;
    	break;
          case 1:
    	pt[0][0].x = pt[1][0].x - BLOCK;
    	pt[0][1].x = pt[1][1].x - BLOCK;
    	break;
    
          case 2:
    	pt[0][0].x = pt[1][0].x + BLOCK;
    	pt[0][1].x = pt[1][1].x + BLOCK;
    	break;
    
          case 3:
    	pt[0][0].y = pt[1][0].y - BLOCK;
    	pt[0][1].y = pt[1][1].y - BLOCK;
    	break;
        }
    }
    
    int main()
    {
        POINT p[10][2] = { fill in with values };
        MoveSnake(p, /* supply the rest of the parameters*/ );
    }
    So, where does this go wrong? What values are sent to MoveSnake in this simple piece of code that causes the problem? If you don't see the problem here, then the issue is not MoveSnake() -- it is something else going on in your program that is overwriting the array with bogus values.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 27th, 2010 at 07:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured