hi,

i discovered another problem with my snake game. sorry for that, but i'm a beginner on directx, and i need every help i can get. the problem is the following:

when the snake goes down for example, it is not possible to press the up key to move the snake up, the only possible directions are down (although it doesn't make sense), left and right. usually, when i press "right"then "up" during the time the snake goes down, everything goes well, the snake moves one rectangle to the right, and then directly goes up. but if i do that move very fast, i mean very, very fast, the snake behaves like if i pressed up only, and my program allowed that keypress. but then you lost the game, cause the head of the snake touched its body.

i'm wondering how this can be. i attached the program (for 1 player) again, so that you can test that. to start the game, press "S", to quit press escape. here is my main game loop again:



snakemainproc proc uses ebx edi esi

invoke GetTickCount
mov ebx,eax
mov ecx,lasttick
sub eax,ecx
.if eax>=snakespeed
mov lasttick,ebx
invoke ClearScreen
invoke movesnake

invoke changedirection
.if foodflag==1
invoke placefood
.endif
invoke foodcrash
invoke snakecrash
.if eax==1
jmp @F
.endif
invoke wallcrash
.if eax==1
jmp @F
.endif
invoke DrawBitmap,foodx,foody,offset surface_food,offset bmp_food_info
mcall [dds_back],IDirectDrawSurface7_GetDC,addr backdc
invoke SelectObject, backdc, fonthandle
mov oldfont,eax
invoke SetTextColor,backdc,0FFFFFFh
invoke SetBkMode,backdc,TRANSPARENT
.if point1flag==1
invoke wsprintf,addr strbuffer,addr szformat,p1pointsint
invoke wsprintf,addr p1points,addr p1pointsformat,addr strbuffer
mov point1flag,0
.endif
invoke DrawMyText,addr p1points,addr pointsrectstruct
invoke SelectObject,backdc,oldfont

mcall [dds_back],IDirectDrawSurface7_ReleaseDC,backdc

invoke DrawWindowFrame
invoke UpdateScreen
.endif

invoke ReadKeyboard

.if keyboard_state[DIK_ESCAPE]
invoke DestroyWindow,hWnd
.endif

.if keyboard_state[DIK_UP]
.if snakeone[0].direction!=3
mov snakeone[0].direction,1
.endif

.elseif keyboard_state[DIK_DOWN]
.if snakeone[0].direction!=1
mov snakeone[0].direction,3
.endif

.elseif keyboard_state[DIK_LEFT]
.if snakeone[0].direction!=2
mov snakeone[0].direction,4
.endif

.elseif keyboard_state[DIK_RIGHT]
.if snakeone[0].direction!=4
mov snakeone[0].direction,2
.endif

.endif
.if keyboard_state[DIK_W]
.if snaketwo[0].direction!=3
mov snaketwo[0].direction,1
.endif

.elseif keyboard_state[DIK_S]
.if snaketwo[0].direction!=1
mov snaketwo[0].direction,3
.endif

.elseif keyboard_state[DIK_A]
.if snaketwo[0].direction!=2
mov snaketwo[0].direction,4
.endif

.elseif keyboard_state[DIK_D]
.if snaketwo[0].direction!=4
mov snaketwo[0].direction,2
.endif

.elseif keyboard_state[DIK_S]

.endif

@@:

ret
snakemainproc endp


looking forward to get the same good answers as before! thanks

nop
Posted on 2001-12-19 14:38:56 by NOP-erator
Easiest way is to only allow movement perpendicular to path (ie If moving up or down, only allow movement left or right.)
Posted on 2001-12-19 15:17:17 by bitRAKE
hey bitRAKE,

please have a closer look at the code i pasted, there is exactly what you said.........nevertheless doesn't work....

nop
Posted on 2001-12-19 15:29:30 by NOP-erator
Oh, so your saying if you press two keys to do a U turn then the program acts like the snake backs up on itself? If that is the case, I'd move the direction update to the screen update routine. That way you could change directions real fast, but at the screen update routine they would get cancelled out if they conflict with current snake direction. The keys would just update a pseudo-direction -- the real direction would be updated from the screen update routine based on the current direction and the pseudo-direction.
Posted on 2001-12-19 16:41:19 by bitRAKE
NOP-erator,

If the snake is moving down, and you quickly press
right then up, queue the up command and any other commands--don't process them--until the snake has moved at least one snake unit to the right.

farrier
Posted on 2001-12-19 23:51:38 by farrier
farrier, that will make the game too easy, IMHO. :)
But is the better choice, than ignore.
Posted on 2001-12-20 00:04:17 by bitRAKE
Bitrake, easier how? Easier as in not being able to move left when
you're moving right (and thus dying)?
Posted on 2001-12-20 00:42:12 by f0dder
No, I mean easier in buffering key presses instead of needing to press direction changes at intervals based on the speed of the snake. Well, it's highly subjective I suppose? :rolleyes:
Posted on 2001-12-20 00:48:18 by bitRAKE
Ah ;). Yes, that sort of buffering might make it a bit too easy.
A solution might be to buffer keys, but to flush the buffer at each
"time to move snake" interval, and only use the last valid direction
change...
Posted on 2001-12-20 10:23:51 by f0dder
The method I stated above sort of does that, but the buffer being only a single char deep. ;)
Posted on 2001-12-20 14:59:33 by bitRAKE
NOP-erator: When I try to run your game in win2k, I just get a blank screen but no game. It doesn't crash but just doesn't display something.
Am I the only one with problems in win2k?

Thomas
Posted on 2001-12-23 05:30:07 by Thomas
Hmm...

I had your same problem, Thomas, but I only forgot to press 'S' to start... Aren't you missing it too? Or does not really Snake work under Win2000?

Sincerely,
Daniel
Posted on 2001-12-23 09:06:24 by dguzz
Hehe that did the trick :grin: Thanks

Thomas
:stupid:
Posted on 2001-12-23 10:01:28 by Thomas
cool that it runs on Win9x/2000/XP. it would be interesting if it works on NT, too.

nop
Posted on 2001-12-23 12:14:45 by NOP-erator