Posts

Showing posts from August, 2023
var Snake = (function () { const INITIAL_TAIL = 4; var fixedTail = true; var intervalID; var tileCount = 10; var gridSize = 400/tileCount; const INITIAL_PLAYER = { x: Math.floor(tileCount / 2), y: Math.floor(tileCount / 2) }; var velocity = { x:0, y:0 }; var player = { x: INITIAL_PLAYER.x, y: INITIAL_PLAYER.y }; var walls = false; var fruit = { x:1, y:1 }; var trail = []; var tail = INITIAL_TAIL; var reward = 0; var points = 0; var pointsMax = 0; var ActionEnum = { 'none':0, 'up':1, 'down':2, 'left':3, 'right':4 }; Object.freeze(ActionEnum); var lastAction = ActionEnum.none; function setup () { canv = document.getElementById('gc'); ctx = canv.getContext('2d'); game.reset(); } var game = { reset: function () { ctx.fillStyle = 'grey'; ctx.fillRect(0, 0, canv.width, canv.height); tail = INITIAL_TAIL; points = 0; velocity.x =...