Class: LittleBrat

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/littlebrat.rb

Defined Under Namespace

Classes: Instructions, Letter

Constant Summary collapse

LETTER_LIMIT =
60

Instance Method Summary collapse

Constructor Details

#initialize(width = Gosu.screen_width, height = Gosu.screen_height, fullscreen = true) ⇒ LittleBrat

Returns a new instance of LittleBrat.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/littlebrat.rb', line 6

def initialize width=Gosu.screen_width, height=Gosu.screen_height, fullscreen=true
  # Full resolution, fullscreen
  super width, height, !!fullscreen #force to boolean for Gosu
  self.caption = "Leave me alone you little brat!"
  # The letters the little brat hits on the keyboard
  @letters, @new_letters = [], []
  # Load up the instructions to show at first
  @letters.push Instructions.new(self)
  @beeps = load_beeps
  @redraw = true # init this because it needs to be true or false
end

Instance Method Details

#beepObject



71
72
73
# File 'lib/littlebrat.rb', line 71

def beep
  @beeps[rand @beeps.size].play
end

#button_down(id) ⇒ Object

Callback, not on main thread



47
48
49
50
# File 'lib/littlebrat.rb', line 47

def button_down id # Callback, not on main thread
  close if close? # Close if we press the right keys
  @new_letters.push str_from_button_id(id)
end

#close?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
# File 'lib/littlebrat.rb', line 62

def close?
  (button_down? Gosu::KbEscape        or
   button_down? Gosu::KbLeftControl   or
   button_down? Gosu::KbRightControl) &&
  (button_down? Gosu::KbQ         or button_down? Gosu::KbW or
   button_down? Gosu::KbZ         or button_down? Gosu::KbC or
   button_down? Gosu::KbBackspace or button_down? Gosu::KbDelete)
end

#drawObject



38
39
40
41
# File 'lib/littlebrat.rb', line 38

def draw
  @letters.each { |letter| letter.draw }
  @redraw = false # Done drawing, don't need to draw no more
end

#load_beepsObject



18
19
20
21
22
# File 'lib/littlebrat.rb', line 18

def load_beeps
  Dir.glob(File.dirname(__FILE__) + '/../sounds/**/*.WAV').map do |beep|
    Gosu::Sample.new(self, beep)
  end
end

#needs_cursor?Boolean

Don’t show the cursor, even on Ubuntu

Returns:

  • (Boolean)


45
# File 'lib/littlebrat.rb', line 45

def needs_cursor?; false;   end

#needs_redraw?Boolean

Don’t paint/draw unless we need to

Returns:

  • (Boolean)


43
# File 'lib/littlebrat.rb', line 43

def needs_redraw?; @redraw; end

#random_letterObject



57
58
59
60
# File 'lib/littlebrat.rb', line 57

def random_letter
  @random_letters ||= %w{                }
  @random_letters[rand @random_letters.size]
end

#str_from_button_id(id) ⇒ Object



52
53
54
55
# File 'lib/littlebrat.rb', line 52

def str_from_button_id id
  c = self.button_id_to_char(id).to_s.strip
  c.empty? ? random_letter : c
end

#updateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/littlebrat.rb', line 24

def update
  if !@new_letters.empty?
    # Add new letter to the queue
    while str = @new_letters.shift
      @letters.push(Letter.new self, str)
    end
    beep # Only beep once per game loop
    # Keep the total number of letter sprites at a managable number
    @letters = @letters[-LETTER_LIMIT..-1] if @letters.size > LETTER_LIMIT
    # We updated the letters, so we need to redraw
    @redraw = true
  end
end