-
December 26th, 2010, 01:26 AM
#1
[RESOLVED] Snake
Hi
I am trying to build a simple snake game.
I have pretty much done it. It works well but theres a little problem.
if i try to add a static variable over the code that i have written, i get a weird error... (the rectangles of my snake behave very odd, crashing into walls at the press of a button)..
Why is this happening? Any ideas ? Should I upload parts of my code?
-
December 26th, 2010, 05:29 AM
#2
-
December 26th, 2010, 07:55 AM
#3
Re: Snake
Originally Posted by GCDEF
don't be so harsh. get your crystal ball out damnit!
-
December 26th, 2010, 08:22 AM
#4
Re: Snake
My car keeps refusing to start on Wednesdays. What could be wrong???
Yes of course you'll need to upload some code - but not the full program. Upload the smallest sample that reproduces the problem.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 26th, 2010, 08:45 AM
#5
Re: Snake
Originally Posted by Amleto
don't be so harsh. get your crystal ball out damnit!
What was that??? Do you believe your yelling helps somehow to solve the original issue?
Originally Posted by vinayak4gargya
Why is this happening? Any ideas ?
The general idea is: you do that wrong way, or in wrong place, or for wrong reason.
Should I upload parts of my code?
If I were you, I would debug my app step by step and see what happens to my program variables. Sounds kinda trivial, but it really works most of the times.
Best regards,
Igor
-
December 26th, 2010, 09:13 AM
#6
Re: Snake
Originally Posted by Igor Vartanov
What was that??? Do you believe your yelling helps somehow to solve the original issue?
not sure if serious.
http://media.photobucket.com/image/n...if-serious.jpg
I think you should look up, cos I think something just went over your head.
-
December 26th, 2010, 09:18 AM
#7
Re: Snake
Originally Posted by Amleto
I think you should look up, cos I think something just went over your head.
No, I think Igor just got mixed up, maybe thinking you were the original poster! It's an easy mistake to make when you're busy. He's probably still opening his Christmas presents!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 26th, 2010, 10:03 AM
#8
Re: Snake
Okay, in case that was some sort of humor, I didn't get that. Sorry, it happens time to time.
Best regards,
Igor
-
December 26th, 2010, 04:19 PM
#9
Re: Snake
Vinayak4gargya, hi...
Don't know what it could be. Mine is just fine...
Albeit a bit ugly...
Code:
+----------------------------+
|Lives: 1 Score: 410|
+----------------------------+
| |
| |
| |
| o >-------+ |
| | |
|--+ | +------|
| | | | |
| | +----+ | |
| | | | |
| | | |
| +-----------+ |
| |
| |
| |
+----------------------------+
LOL
P.S. Use the [code][/code] tags when you post your code.
-
December 27th, 2010, 06:33 AM
#10
Re: Snake
Heres WM_PAINT
Code:
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
SetIso(hdc,cxClient,cyClient); //Mapping mode
hPen = CreatePen(PS_SOLID,5,RGB(255,255,255));
hBrush = CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255));
//Draw Boundaries
SelectObject(hdc,hPen);
MoveToEx(hdc, -100, 100 , NULL);
LineTo(hdc,-100,-100);
MoveToEx(hdc, 100, 100, NULL);
LineTo(hdc, 100, -100);
SelectObject(hdc,hBrush);
TextOut(hdc,-180,0,szBuffer,wsprintf(szBuffer,"Your Score %i",(int)iScore));
SelectObject(hdc,(HPEN)GetStockObject(WHITE_PEN));
if(iDirect == 4)
TextOut(hdc,0,0,TEXT("Press Enter to start a New Game...."),36);
//Draw Snake's body rectangles
for(i = 0 ; i < iSnakeSize; i++)
Rectangle(hdc,pt[i][0].x,pt[i][0].y,pt[i][1].x,pt[i][1].y);
DeleteObject(hBrush);
hBrush = CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255));
//Draw Food
if(!bFoodOnScreen)
{
rectFood = CreateFood(pt,iSnakeSize);
bFoodOnScreen = TRUE;
}
SelectObject(hdc,hBrush);
Ellipse(hdc,rectFood.left,rectFood.top,rectFood.right,rectFood.bottom);
//Check if food has been eaten
if(EatFood(pt[0],rectFood))
{
iSnakeSize++;
iScore += 10;
bFoodOnScreen = FALSE;
}
SelectObject(hdc,(HPEN)GetStockObject(WHITE_PEN));
EndPaint(hwnd,&ps);
DeleteObject(hBrush);
DeleteObject(hPen);
return 0;
And here is MoveSnake
Code:
void MoveSnake(POINT pt[500][2],int iDirect,int iLength)
{
int i;
//Move all rectangular blocks one position up in the array
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;
}
}
I DID try debugging only to get harrowing results!
After the MoveSnake function call the topmost elements of the array remain unchanged!(if i add another static variable to the code)
Otherwise it works normally.
-
December 27th, 2010, 07:29 AM
#11
Re: Snake
Originally Posted by vinayak4gargya
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.
-
December 27th, 2010, 08:13 AM
#12
Re: Snake
Could this function be a problem?
Code:
void NewGame(BOOL *bFoodOnScreen,int *iDirect, int *iSnakeSize,POINT pt[500][2],int *iScore)
{
int i;
*bFoodOnScreen = FALSE;
*iDirect = 4;
*iScore = 0;
*iSnakeSize = 4;
//Initialize the first four rectangles
for(i = 0; i< *iSnakeSize ; i++)
{
pt[i][0].x = 0;
pt[i][0].y = (3-i)*BLOCK;
pt[i][1].x = BLOCK;
pt[i][1].y = (3-i+1)*BLOCK;
}
//Fill the rest of the array with 0
for(i = *iSnakeSize; i < 500; i++)
{
pt[i][0].x = 0;
pt[i][0].y = 0;
pt[i][1].x = 0;
pt[i][1].y = 0;
}
}
OK here's the deal...
As soon as I press Enter to start the Game this is what happens
http://www.freeimagehosting.net/imag...19a0213629.png
the movesnake function is called after every iteration , thats all..
And i am not fixing this problem with static variables, they are CAUSING this problem.
I need the static variable to implement a functionality in the game.
The direction changes on keystrokes.
These are the values of the Snakes body after the first few iterations:
>1st iteration
- pt 0x013481a0 pt tagPOINT [500][2]
- [0] 0x013481a0 pt {x=-10 y=30} tagPOINT [2]
+ [0] {x=-10 y=30} tagPOINT
+ [1] {x=0 y=40} tagPOINT
- [1] 0x013481b0 {x=0 y=30} tagPOINT [2]
+ [0] {x=0 y=30} tagPOINT
+ [1] {x=10 y=40} tagPOINT
- [2] 0x013481c0 {x=0 y=20} tagPOINT [2]
+ [0] {x=0 y=20} tagPOINT
+ [1] {x=10 y=30} tagPOINT
- [3] 0x013481d0 {x=0 y=10} tagPOINT [2]
+ [0] { x=0 y=10} tagPOINT
+ [1] {x=10 y=20} tagPOINT
>2nd
- pt 0x013481a0 pt tagPOINT [500][2]
- [0] 0x013481a0 pt {x=-266 y=30} tagPOINT [2]
+ [0] {x=-266 y=30} tagPOINT
+ [1] {x=-10 y=40} tagPOINT
- [1] 0x013481b0 {x=-256 y=30} tagPOINT [2]
+ [0] {x=-256 y=30} tagPOINT
+ [1] {x=0 y=40} tagPOINT
- [2] 0x013481c0 {x=0 y=30} tagPOINT [2]
+ [0] {x=0 y=30} tagPOINT
+ [1] {x=10 y=40} tagPOINT
- [3] 0x013481d0 {x=0 y=20} tagPOINT [2]
+ [0] {x=0 y=20} tagPOINT
+ [1] {x=10 y=30} tagPOINT
As you can see, the value of x is -266 ?????
I say it again .. it works fine unless i introduce one more static variable to the code after that all i get is a -256 on the x and a weird scoreline...
HELPP!!!
Last edited by vinayak4gargya; December 27th, 2010 at 09:14 AM.
Reason: Image Link
-
December 27th, 2010, 08:41 AM
#13
Re: Snake
These are the values passed to MoveSnake
- pt 0x00bd81a0 pt tagPOINT [2]*
+ [0] {x=0 y=30} tagPOINT
+ [1] {x=10 y=40} tagPOINT
iDirect 1 int
iLength 4 int
after the function finishes , every value is incremented (normal)
I just tried out a data breakpoint(its a new thing for me) and i set it to view any change in the value of the array after this function..
It does report a breakpoint but then shows a "theres no source code available" thing.
Something is changing the value of the snakes head but i cannot figure out where!!
-
December 27th, 2010, 09:04 AM
#14
Re: Snake
I say it again .. it works fine unless i introduce one more static variable to the code after that all i get is a -256 on the x and a weird scoreline...
Again, you just say it, but don't show how/where you introduce it (at least I cannot see any in your code). As long as this badly affects your program behavior, you do that wrong way, or in wrong place, or for wrong reason. You know, there are lots of ways how you could break your program which currently works fine.
Besides, this prototype looks really dangerous:
Code:
void MoveSnake(POINT pt[500][2],int iDirect,int iLength)
In case you cross the array bounds somehow, you spoil the stack with unpredictable behavior.
Last edited by Igor Vartanov; December 27th, 2010 at 09:08 AM.
Best regards,
Igor
-
December 27th, 2010, 09:06 AM
#15
Re: Snake
Ok heres some more detail...
Code:
case WM_TIMER:
InvalidateRect(hwnd,NULL,TRUE);
SetRect(&rect,pt[0][0].x,pt[0][0].y,pt[0][1].x,pt[0][1].y);
int check;
check = CheckHit(pt,iSnakeSize,rect);
MoveSnake(pt,iDirect,iSnakeSize);
if((check==0) | WallHit(pt[0] ))
{
KillTimer(hwnd,1);
MessageBox(hwnd,szBuffer,szAppName,MB_OK|MB_ICONINFORMATION);
iDirect = 4;
NewGame(&bFoodOnScreen,&iDirect,&iSnakeSize,pt,&iScore);
}
return 0;
I break at MoveSnake;
Everythings normal;
Everything normal upto return 0;
"No source code available"
Breakpoint at switch (msg) of WndProc
Step Into;
Takes me to WM_PAINT:
Step Into : hdc = BeginPaint(hwnd,&ps);
Step Into again : I am returned back to switch(msg) (??) (The rest of the code of WM_PAINT is not executed)(??)
Step Into again : msg = 20;
Step Into : DefWindowProc();
Step Into again : At this time pt[0][0].x is -256
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|