class SnakeGame::Snake

Class representing the whole snake

Attributes

direction_to_go[RW]
gameover[RW]
head[RW]
last_direction[RW]
score[RW]
snake_parts[RW]
speed[RW]

Public Class Methods

new() click to toggle source
# File lib/snake.rb, line 12
def initialize
  @images = ImageDatabase.new
  @snake_parts = []
  initialize_snake 5, WIDTH / 2, HEIGHT / 2
  @score = 0
  @speed = 8.0
  @last_direction = :up
  @direction_to_go = :up
  @gameover = false
end

Public Instance Methods

add_part(position) click to toggle source
# File lib/snake.rb, line 98
def add_part(position)
  @head.image = @images.tail
  @head = SnakePart.new position, @images.head
  @snake_parts << @head
end
cut_tail() click to toggle source
# File lib/snake.rb, line 104
def cut_tail
  @snake_parts.shift
end
draw() click to toggle source
# File lib/snake.rb, line 112
def draw
  @snake_parts.each(&:draw)
  return unless @gameover
  Gosu::Font.new(60).draw_rel(
    'GAME OVER', WIDTH / 2, HEIGHT / 2,
    0, 0.5, 0.5, 1.0, 1.0, Gosu::Color::RED
  )
end
eating?(bait_manager) click to toggle source
# File lib/snake.rb, line 62
def eating?(bait_manager)
  eating = false
  bait_manager.baits.each do |bait|
    next unless bait.position == @head.position
    # puts 'I AM EATING'
    @score += bait.size
    eating = true
    bait_manager.baits.delete bait
    @speed *= 1.1 if (@score % NEXTLEVEL).zero?
  end
  eating
end
gameover!() click to toggle source
# File lib/snake.rb, line 108
def gameover!
  @gameover = true
end
go(direction) click to toggle source
# File lib/snake.rb, line 75
def go(direction)
  position = nil
  case direction
  when :left
    @head.position.x += WIDTH if @head.position.x <= 0
    position = Position.new @head.position.x - STEP,
                            @head.position.y
  when :right
    position = Position.new (@head.position.x + STEP) % WIDTH,
                            @head.position.y
  when :up
    @head.position.y += HEIGHT if @head.position.y <= 0
    position = Position.new @head.position.x,
                            @head.position.y - STEP
  when :down
    position = Position.new @head.position.x,
                            (@head.position.y + STEP) % HEIGHT
  else
    position = go @last_direction
  end
  position
end
initialize_snake(count, center_x, center_y) click to toggle source
# File lib/snake.rb, line 23
def initialize_snake(count, center_x, center_y)
  count.times do |i|
    part = SnakePart.new (Position.new center_x,
                                       center_y + (count - i) * STEP),
                         @images.tail
    @snake_parts << part
  end
  @head = SnakePart.new (Position.new center_x,
                                      center_y),
                        @images.head
  @snake_parts << @head
end
process_input() click to toggle source
# File lib/snake.rb, line 36
def process_input
  if (Gosu.button_down? Gosu::KB_LEFT) && (@last_direction != :right)
    @direction_to_go = :left
  elsif (Gosu.button_down? Gosu::KB_RIGHT) && (@last_direction != :left)
    @direction_to_go = :right
  elsif (Gosu.button_down? Gosu::KB_UP) && (@last_direction != :down)
    @direction_to_go = :up
  elsif (Gosu.button_down? Gosu::KB_DOWN) && (@last_direction != :up)
    @direction_to_go = :down
  end
end
update(bait_manager) click to toggle source
# File lib/snake.rb, line 48
def update(bait_manager)
  next_position = go @direction_to_go

  @snake_parts.each do |part|
    if part.position == next_position
      # puts 'GAME OVER!'
      gameover!
    end
  end
  add_part next_position
  cut_tail unless eating? bait_manager
  @last_direction = @direction_to_go
end