From 5a0a5be355c7a9bc79f8d77d26174c0be5162710 Mon Sep 17 00:00:00 2001 From: fceccon Date: Thu, 28 Jul 2011 21:52:32 +0200 Subject: [PATCH] Move the snake around --- coffee/Player.coffee | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/coffee/Player.coffee b/coffee/Player.coffee index ccbe562..b00fcd8 100644 --- a/coffee/Player.coffee +++ b/coffee/Player.coffee @@ -1,26 +1,38 @@ +size = 10 # Each piece of the body is 10px X 10px class Player constructor: -> # Initial position - @x = 100 - @y = 100 - @size = 10 + @body = [[10, 10], [11, 10], [12, 10], [13, 10]] @dir = 'left' setDir: (d) -> @dir = d + head: -> + @body[@body.length - 1] + move: -> + currentHead = @head() + x = currentHead[0] + y = currentHead[1] + switch @dir - when 'left' then @x -= @size - when 'right' then @x += @size - when 'up' then @y -= @size - when 'down' then @y += @size + when 'left' then x-- + when 'right' then x++ + when 'up' then y-- + when 'down' then y++ + + @body.push [x, y] + + # Remove the old tail + @body = @body[1...@body.lenght] draw: -> @move() ctx.fillStyle = '#2F2F2F' - ctx.fillRect(@x, @y, @size, @size) + @body.forEach (piece) -> + ctx.fillRect(piece[0] * size, piece[1] * size, size, size) window.player = new Player