Class: Game
Constant Summary collapse
- TOP =
0- BOTTOM =
22- LEFT =
0- RIGHT =
70
Instance Method Summary collapse
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #start ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby-snake/game.rb', line 12 def initialize Curses.init_screen Curses.noecho # Don't show typed key. Curses.stdscr.keypad(true) # colors Curses.start_color Curses.init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLUE) # Color of boders Curses.init_pair(COLOR_RED, COLOR_RED, COLOR_RED) Curses.init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK) Curses.init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_YELLOW) setup end |
Instance Method Details
#start ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ruby-snake/game.rb', line 28 def start # Create a thread to montior key events key_listener = Thread.new do loop {Thread.current[:key] = Curses.getch} end while true handle_key key_listener[:key] if !@pause if @dead_snake # erase dead snake @dead_snake.body.each {|pos| erase pos} erase @dead_snake.food @dead_snake = false end tail = @snake.goto @direction if @snake.alive? draw_snake draw_food erase tail @speed += 1 if @snake.score / 100 > @speed - 1 else @dead_snake = @snake.dup setup = "You die! Press 'S' to restart the game." @pause = true end draw_ui Curses.refresh sleep(0.2 * (0.8 ** (@speed - 1))) end end end |