Skip to content

Commit

Permalink
Move the snake around
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Jul 28, 2011
1 parent e54ee00 commit 5a0a5be
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions 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

0 comments on commit 5a0a5be

Please sign in to comment.