Here are fixes for 2 bugs that were introduced by me.
The first bug makes the game end if the snake enters the square in the top-left corner. It is caused by some leftover and unneeded code. The fix is to remove the code.
The second bug makes the snake detect a collision with itself by allowing the snake to collide with where its tail part used to be before it moved. It is caused by a combination in the way the snake was updated and the detection for collision with itself. It is corrected by moving the collision detection after the update of the snakes length.
Previous code:
Code: Select all
AddElement(Snakes()) ;add a new part for the head at the front of snake
If Snakes()\x = SnakePart\x + 16 * vx And Snakes()\y = SnakePart\y + 16 * vy
vx * -1
vy * -1
EndIf
Snakes()\x = SnakePart\x + 16 * vx
Snakes()\y = SnakePart\y + 16 * vy
SnakePart = Snakes() ;save information for new head part
;-Collide(head, target)
If SnakePart\x = tx And SnakePart\y = ty
TargetCreate = #True
UpdateSquares = #True
Score + 1
PlaySound(#Sound_FindTarget, #PB_Sound_MultiChannel, 100)
EndIf
;-Collide(head, Body)
While NextElement(Snakes())
If SnakePart\x = Snakes()\x And SnakePart\y = Snakes()\y
Boom = #True
Break
EndIf
Wend
If UpdateSquares = #False
;Remove tail part to keep snake the same length
LastElement(Snakes())
DeleteElement(Snakes())
Else
;Adds an element to the body of the snake, it does so by NOT removing the tail part
UpdateSquares = #False
EndIf
Replacement code:
Code: Select all
AddElement(Snakes()) ;add a new part for the head at the front of snake
; If Snakes()\x = SnakePart\x + 16 * vx And Snakes()\y = SnakePart\y + 16 * vy
; vx * -1
; vy * -1
; EndIf
Snakes()\x = SnakePart\x + 16 * vx
Snakes()\y = SnakePart\y + 16 * vy
SnakePart = Snakes() ;save information for new head part
;-Collide(head, target)
If SnakePart\x = tx And SnakePart\y = ty
TargetCreate = #True
UpdateSquares = #True
Score + 1
PlaySound(#Sound_FindTarget, #PB_Sound_MultiChannel, 100)
EndIf
If UpdateSquares = #False
;Remove tail part to keep snake the same length
LastElement(Snakes())
DeleteElement(Snakes())
Else
;Adds an element to the body of the snake, it does so by NOT removing the tail part
UpdateSquares = #False
EndIf
;-Collide(head, Body)
FirstElement(Snakes())
While NextElement(Snakes())
If SnakePart\x = Snakes()\x And SnakePart\y = Snakes()\y
Boom = #True
Break
EndIf
Wend
Another effect may or may not be a bug. It seems like it is to me though. The effect is based on the window coordinates. The window's inner coordinates is used instead of the frame coordinates. When the effect is in play is using these coordinates it nudges the window off the bottom of the screen if 'up' or 'down' is pushed.
Previous code:
Code: Select all
WX = WindowX(#MainForm, #PB_Window_InnerCoordinate)
WY = WindowY(#MainForm, #PB_Window_InnerCoordinate)
Replacement code:
Code: Select all
WX = WindowX(#MainForm, #PB_Window_FrameCoordinate)
WY= WindowY(#MainForm, #PB_Window_FrameCoordinate)