// 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') var random = use('random') prosperon.camera.transform.pos = [0,0] var cellSize = 20 var gridW = floor(config.width / cellSize) var gridH = floor(config.height / cellSize) var snake = null, direction = null, nextDirection = null, apple = null var moveInterval = 0.1 var moveTimer = 0 var gameState = "playing" function resetGame() { var cx = floor(gridW / 2) var cy = 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:floor(random.random()*gridW), y:floor(random.random()*gridH)} // Re-spawn if apple lands on snake var i = 0; for (i=0; i= gridW) pos.x = 0 if (pos.y < 0) pos.y = gridH - 1 if (pos.y >= gridH) pos.y = 0 } resetGame() var 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 var i = 0; for (i=0; i