Class: GPLType::ScoreWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/curses_color.rb

Overview

ScoreWindow to show high score, current score and mistyped characters

Instance Method Summary collapse

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

#destroyObject



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_mistypeObject



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

#showObject



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_scoreObject



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_mistypeObject



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