Class: GPLType::ScoreWindow
- Inherits:
-
Object
- Object
- GPLType::ScoreWindow
- Defined in:
- lib/curses_color.rb
Overview
ScoreWindow to show high score, current score and mistyped characters
Instance Method Summary collapse
- #destroy ⇒ Object
- #inc_mistype ⇒ Object
-
#initialize(high_sec, high_chars) ⇒ ScoreWindow
constructor
A new instance of ScoreWindow.
- #set_current_score(sec, chars) ⇒ Object
- #show ⇒ Object
- #update_current_score ⇒ Object
- #update_mistype ⇒ Object
Constructor Details
#initialize(high_sec, high_chars) ⇒ ScoreWindow
Returns a new instance of ScoreWindow.
343 344 345 346 347 348 349 350 |
# File 'lib/curses_color.rb', line 343 def initialize(high_sec, high_chars) @mistype = 0 @high_score = { :sec => high_sec <= 9999 ? high_sec : 9999, :chars => high_chars <= 9999 ? high_chars : 9999, } @current_score = {:sec => 0, :chars => 0} end |
Instance Method Details
#destroy ⇒ Object
404 405 406 407 408 409 |
# File 'lib/curses_color.rb', line 404 def destroy return unless @window @window.clear @window.close @window = nil end |
#inc_mistype ⇒ Object
399 400 401 402 |
# File 'lib/curses_color.rb', line 399 def inc_mistype @mistype += 1 update_mistype end |
#set_current_score(sec, chars) ⇒ Object
384 385 386 387 388 |
# File 'lib/curses_color.rb', line 384 def set_current_score(sec, chars) @current_score[:sec] = sec <= 9999 ? sec : 9999 @current_score[:chars] = chars <= 9999 ? chars : 9999 update_current_score end |
#show ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/curses_color.rb', line 352 def show return if @window @window = Curses::stdscr.subwin(3, Curses::stdscr.width - 2, 2, 1) # high score @window.setpos(0, 0) str = sprintf("High score: %4d sec (%4d characters/min)", @high_score[:sec], @high_score[:chars]) @window.addstr(str, :default, :right) update_current_score update_mistype @window.refresh end |
#update_current_score ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/curses_color.rb', line 369 def update_current_score return unless @window prefix = sprintf("Current score: %4d sec (", @current_score[:sec]) chars = sprintf("%4d", @current_score[:chars]) suffix = " characters/min)" high = @current_score[:chars] > @high_score[:chars] x = @window.width - prefix.size - chars.size - suffix.size @window.setpos(1, x) @window.addstr(prefix) @window.addstr(chars, high ? :highscore : :default) @window.addstr(suffix) @window.refresh end |
#update_mistype ⇒ Object
390 391 392 393 394 395 396 397 |
# File 'lib/curses_color.rb', line 390 def update_mistype return unless @window @window.setpos(2, 0) str = sprintf("Mistype: %4d characters", @mistype) color = @mistype == 0 ? :default : :mistype @window.addstr(str, color, :right) @window.refresh end |