Module: Snake2d

Defined in:
lib/snake2d.rb,
lib/snake2d/version.rb

Defined Under Namespace

Classes: Game, Snake

Constant Summary collapse

SQUARE_SIZE =
20
GRID_WIDTH =
Window.width / SQUARE_SIZE
GRID_HEIGHT =
Window.height / SQUARE_SIZE
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.runObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/snake2d.rb', line 126

def self.run
  Window.set background: 'navy'
  Window.set fps_cap: 20
  
  snake = Snake.new
  game = Game.new
  
  Window.update do
    Window.clear
  
    unless game.finished?
      snake.move
    end
  
    snake.draw
    game.draw
  
    if game.snake_hit_ball?(snake.x, snake.y)
      game.record_hit
      snake.grow
    end
  
    if snake.hit_itself?
      game.finish
    end
  end
  
  Window.on :key_down do |event|
    if ['up', 'down', 'left', 'right'].include?(event.key)
      if snake.can_change_direction_to?(event.key)
        snake.direction = event.key
      end
    end
  
    if game.finished? && event.key == 'r'
      snake = Snake.new
      game = Game.new
    end
  end
  
  Window.show
end