|
@@ -58,10 +58,11 @@ const snakeGame = () => {
|
|
|
|
|
|
reset();
|
|
|
|
|
|
+ let currentKey;
|
|
|
let lastKey;
|
|
|
|
|
|
const onKey = ({ code }) => {
|
|
|
- if (snakeTail.length <= 1 || OPPOSITE_KEY[lastKey] !== code) {
|
|
|
+ if (snakeTail.length <= 1 || OPPOSITE_KEY[currentKey] !== code) {
|
|
|
lastKey = code;
|
|
|
}
|
|
|
};
|
|
@@ -75,7 +76,8 @@ const snakeGame = () => {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- switch (lastKey) {
|
|
|
+ currentKey = lastKey;
|
|
|
+ switch (currentKey) {
|
|
|
case "ArrowUp":
|
|
|
snakeHead.vx = 0;
|
|
|
snakeHead.vy = -1;
|
|
@@ -111,7 +113,7 @@ const snakeGame = () => {
|
|
|
ctx.textAlign = "center";
|
|
|
ctx.textBaseline = "middle";
|
|
|
ctx.fillText("Crashed!", rawSize / 2, rawSize / 2);
|
|
|
- setTimeout(() => reset(), 250);
|
|
|
+ setTimeout(() => reset(), 125);
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -152,7 +154,7 @@ const snakeGame = () => {
|
|
|
|
|
|
setTimeout(onFinish, 500);
|
|
|
}
|
|
|
- }, 60);
|
|
|
+ }, 65);
|
|
|
};
|
|
|
|
|
|
const stop = () => {
|