// main.js var draw = use('draw2d') var render = use('render') var graphics = use('graphics') var input = use('input') var config = use('config') var color = use('color') prosperon.camera.transform.pos = [0,0] var cellSize = 20 var gridW = Math.floor(config.width / cellSize) var gridH = Math.floor(config.height / cellSize) var snake, direction, nextDirection, apple var moveInterval = 0.1 var moveTimer = 0 var gameState = "playing" function resetGame() { var cx = Math.floor(gridW / 2) var cy = Math.floor(gridH / 2) snake = [ {x: cx, y: cy}, {x: cx-1, y: cy}, {x: cx-2, y: cy} ] direction = {x:1, y:0} nextDirection = {x:1, y:0} spawnApple() gameState = "playing" moveTimer = 0 } function spawnApple() { apple = {x:Math.floor(Math.random()*gridW), y:Math.floor(Math.random()*gridH)} // Re-spawn if apple lands on snake for (var i=0; i= gridW) pos.x = 0 if (pos.y < 0) pos.y = gridH - 1 if (pos.y >= gridH) pos.y = 0 } resetGame() this.update = function(dt) { if (gameState !== "playing") return moveTimer += dt if (moveTimer < moveInterval) return moveTimer -= moveInterval // Update direction direction = {x: nextDirection.x, y: nextDirection.y} // New head var head = {x: snake[0].x + direction.x, y: snake[0].y + direction.y} wrap(head) // Check collision with body for (var i=0; i