Created
June 21, 2022 02:02
-
-
Save pedrozath/b738bf7493d09d299ace567366ea6825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Square | |
end | |
class EmptySquare < Square | |
def to_s | |
'º' | |
end | |
end | |
class FoodSquare < Square | |
def to_s | |
'O' | |
end | |
end | |
class SnakeSquare < Square | |
attr_accessor :x, :y | |
def initialize(x:, y:) | |
@x = x | |
@y = y | |
@direction = [0, 1] | |
end | |
def to_s | |
'█' | |
end | |
end | |
class Board | |
def initialize(width: 15, height: 15, food_quantity: 5, snake_size: 1) | |
@width = width | |
@height = height | |
@squares = height.times.map do | |
width.times.map do | |
EmptySquare.new | |
end | |
end | |
food_quantity.times.map do | |
@squares[rand(height)][rand(width)] = FoodSquare.new | |
end | |
@snake = SnakeSquare.new(x: height/2, y: width/2) | |
end | |
def clear | |
print("\e[2J\e[f") | |
end | |
def render | |
clear | |
@squares.each.with_index do |line, y| | |
line.each.with_index do |square, x| | |
if @snake.x == x && @snake.y == y | |
print snake | |
else | |
print square | |
end | |
end | |
puts | |
end | |
end | |
end | |
interval = 0.8 | |
board = Board.new | |
loop { | |
board.render | |
sleep(interval) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment