JavaScript-Snake
JavaScript-Snake copied to clipboard
Noticeable input lag.
If you do down + right (or any combination really) multiple times, you can see that snake will still be doing those movements even after you've finished typing. I'm assuming this is because it's gridded rather than just a free amount of pixels but still...
This is because the game queues key presses. If key presses aren't queued, you can miss some (ex: someone can press down + right really quickly and only go down because the game wasn't ready for the next key press. This would lead the snake into not going right and make the player feel like the game didn't register their right key press). However, I can see how it would cause the lag effect if down+right were pressed repeatedly really quickly. I'll have to think about this, not sure what the best solution would be.
In that case I think the real question is, how is it missing/discarding keypresses?
The snakeSpeed determines the rate of how fast the snake moves. At each tick the game checks to see which direction it will move. The direction the snake moves is based on the arrow keys, when an arrow key is typed the direction is changed. If the user presses two keys within the snakeSpeed time period / tick period (ex: right+down), then only the last key will register if queuing isn't done.
I have an idea that may work. How about attaching a time with each move in the queue and then for every tick, checking whether too much (this could be defined) time has passed to actually execute the move. That way if one was to press multiple arrow keys in a small amount of time, there wouldn't be 'residual moves' per se if the move is too old.
That's an interesting idea, thank you Sam. When I get a chance in the next few days I'll try and dive into this deeper.
@dginovker was telling me that #56 fixes this issue.