Skip to content

Commit 2f154f5

Browse files
committed
Added Snake head & tail
1 parent 89feed7 commit 2f154f5

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎snake.html‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,27 @@ <h3 data-i18n="snake.label.top_snakes">Top Snakes</h3>
611611

612612
// Draw Snake
613613
ctx.fillStyle = "black";
614-
snake.forEach(part => {
615-
ctx.fillRect(part.x * GRID_SIZE, part.y * GRID_SIZE, GRID_SIZE - 1, GRID_SIZE - 1);
614+
snake.forEach((part, index) => {
615+
if (index === 0) {
616+
ctx.beginPath();
617+
ctx.arc(part.x * GRID_SIZE + GRID_SIZE / 2, part.y * GRID_SIZE + GRID_SIZE / 2, GRID_SIZE / 2 - 1, 0, 2 * Math.PI);
618+
ctx.fill();
619+
} else if (index === snake.length - 1 && snake.length > 1) {
620+
const prev = snake[index - 1];
621+
const dx = part.x - prev.x;
622+
const dy = part.y - prev.y;
623+
const cx = part.x * GRID_SIZE + GRID_SIZE / 2;
624+
const cy = part.y * GRID_SIZE + GRID_SIZE / 2;
625+
const r = GRID_SIZE / 2 - 1;
626+
ctx.beginPath();
627+
ctx.moveTo(cx + dx * r, cy + dy * r);
628+
ctx.lineTo(cx + (-dy) * r, cy + dx * r);
629+
ctx.lineTo(cx - (-dy) * r, cy - dx * r);
630+
ctx.closePath();
631+
ctx.fill();
632+
} else {
633+
ctx.fillRect(part.x * GRID_SIZE, part.y * GRID_SIZE, GRID_SIZE - 1, GRID_SIZE - 1);
634+
}
616635
});
617636

618637
// Draw Food (Circle)

0 commit comments

Comments
 (0)