-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.rb
46 lines (38 loc) · 1.06 KB
/
engine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
load "input.rb"
load "screen.rb"
load "snake.rb"
load "apple.rb"
load "event.rb"
class Engine
def run
screen = Screen.new
snake = Snake.new
apple = Apple.new
event = Event.new
score = 0
while true
# Le a entrada do usuario
movement = read_movement
# para de executar o programa caso algo alem das setinhas seja recebido
break if movement == "quit"
snake.move movement
if event.snakeAteApple? snake.position, apple.position
score += 1
snake.grow
apple.generate_apple
end
if (event.snakeHitWall? snake.position) || (event.snakeAteItsefl? snake.position)
puts "GAME OVER"
puts "Final Score: #{score}"
break
end
puts "current score: #{score}"
puts "Snake position: x:#{snake.position[0][:x]} y:#{snake.position[0][:y]}"
puts "Apple position: x:#{apple.position[:x]} y:#{apple.position[:y]}"
# desenha um novo frame
screen.draw snake.position, apple.position
end
end
end
engine = Engine.new
engine.run