Skip to content

Commit

Permalink
Snake is moving!
Browse files Browse the repository at this point in the history
  • Loading branch information
qrush committed Aug 23, 2008
1 parent daeab79 commit 60d2074
Showing 1 changed file with 44 additions and 53 deletions.
97 changes: 44 additions & 53 deletions snake.rb
@@ -1,6 +1,6 @@
class Logger
def self.debug(msg)
#p msg
p msg
end
end

Expand All @@ -10,9 +10,7 @@ class Colors
TEST = "#00f"
end

class Cell
attr_accessor :paint, :color

class Cell
SIZE = 15
START_X = 50
START_Y = 50
Expand All @@ -22,67 +20,66 @@ def initialize(app, row, col)
@row = row
@col = col
@paint = true
@color = Colors::GROUND

spacing = SIZE + 1
@x = START_X + (spacing * row)
@y = START_Y + (spacing * col)
end

def paint
if @paint
#if snake.segments.include?([@row, @col])
# color = Colors::SNAKE_RED
#else
# color = Colors::GROUND
#end
Logger.debug "Painting cell [#{@row}, #{@col}]"

@app.fill @color
@app.rect @x, @y, SIZE, SIZE
@paint = false
end
def paint(color = Colors::GROUND)
#if snake.segments.include?([@row, @col])
# color = Colors::SNAKE_RED
#else
# color = Colors::GROUND
#end
Logger.debug "Painting cell [#{@row}, #{@col}]"

@app.fill color
@app.rect @x, @y, SIZE, SIZE
end
end

class Snake
attr_reader :segments
attr_reader :segments, :direction

DIRECTIONS = {
:up => [0, -1],
:down => [0, 1],
:left => [-1, 0],
:right => [1, 0]
}

def initialize(field)
Logger.debug "New snake!"
@field = field
@segments = [Field.rand]
@direction = :right
end

def up
@segments[0] = [head[0], head[1] - 1]
end

def down
@segments[0] = [head[0], head[1] + 1]
def head
@segments[0]
end

def left
@segments[0] = [head[0] - 1, head[1]]
end

def right
@segments[0] = [head[0] + 1, head[1]]
end
def turn(key)
case key
when :up, :down, :left, :right
@direction = key
end

def head
@segments[0]
#@direction = key if DIRECTIONS.keys.include?(key)
end

def move
Logger.debug "Painting snake..."
dir = DIRECTIONS[@direction]
# Logger.debug @direction
@segments[0] = [head[0] + dir[0], head[1] + dir[1]]


@field.cells[0].paint = true
@field.cells[0].color = Colors::SNAKE_RED
@field.cells[0].paint()

end
@segments.each do |seg|
@field.cells[Field::SIDE * seg[0] + seg[1]].paint(Colors::SNAKE_RED)
end

end
end

class Field
Expand Down Expand Up @@ -120,24 +117,18 @@ def rand

Logger.debug "Making field..."
@field = Field.new(self)
@field.paint

@snake = Snake.new(@field)
#animate(1) { @field.paint }
animate(10) {
animate(1) {
@status.replace "Time: #{Time.now.strftime('%T')}"
@field.paint
@snake.move
}

animate(20) { @snake.move }

keypress do |k|
case k
when :up
@snake.up
when :down
@snake.down
when :left
@snake.left
when :right
@snake.right
end
@snake.turn(k)
end

=begin
Expand Down

0 comments on commit 60d2074

Please sign in to comment.