Class: Bettys
- Inherits:
-
Object
- Object
- Bettys
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#paused ⇒ Object
Returns the value of attribute paused.
-
#piece ⇒ Object
readonly
Returns the value of attribute piece.
-
#score ⇒ Object
Returns the value of attribute score.
Instance Method Summary collapse
- #dash(direction) ⇒ Object
- #draw ⇒ Object
- #drop ⇒ Object
- #hold ⇒ Object
- #imprint ⇒ Object
-
#initialize(mode, level = 1) ⇒ Bettys
constructor
A new instance of Bettys.
- #notify_lock_reset ⇒ Object
- #over? ⇒ Boolean
-
#show_game_over ⇒ Object
Called AFTER Curses.close_screen — prints to normal stdout.
- #soft_drop ⇒ Object
- #speed ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #sync ⇒ Object
- #toggle ⇒ Object
Constructor Details
#initialize(mode, level = 1) ⇒ Bettys
Returns a new instance of Bettys.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/game.rb', line 45 def initialize(mode, level = 1) @mode = mode @board = Board.new @purse = Purse.new(@board) @piece = @purse.pick @hold = nil @can_hold = true @score = 0 @time = mode == :ultra ? 120 : 0 @line = 0 @start_level = level @level = level @mutex = Mutex.new @paused = false @running = false @lock_timer = nil @lock_resets = 0 @grounded = false end |
Instance Attribute Details
#paused ⇒ Object
Returns the value of attribute paused.
43 44 45 |
# File 'lib/game.rb', line 43 def paused @paused end |
#piece ⇒ Object (readonly)
Returns the value of attribute piece.
42 43 44 |
# File 'lib/game.rb', line 42 def piece @piece end |
#score ⇒ Object
Returns the value of attribute score.
43 44 45 |
# File 'lib/game.rb', line 43 def score @score end |
Instance Method Details
#dash(direction) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/game.rb', line 153 def dash(direction) sync do nil while @piece.move!(direction) notify_lock_reset end end |
#draw ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/game.rb', line 201 def draw oy, ox = origin win = Curses.stdscr draw_hold(oy, ox, win) draw_stats(oy, ox, win) draw_matrix(oy, ox, win) draw_active_piece(oy, ox, win) draw_next(oy, ox, win) (oy, ox, win) if @paused end |
#drop ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/game.rb', line 138 def drop count = 0 sync do count += 1 while @piece. lock_piece end @score += 2 * count end |
#hold ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/game.rb', line 160 def hold sync do return unless @can_hold @hold, @piece = @piece, @hold || @purse.pick @hold.orientation = Face.↑ @hold.center = Point[20, 4] @can_hold = false reset_lock_state end end |
#imprint ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/game.rb', line 183 def imprint oy, ox = origin win = Curses.stdscr win.print("HOLD", Point[oy, ox + 6]) win.draw_box(oy + 1, ox + 2, 14, 6, Box::DOTTED) win.print("SCORE", Point[oy + 8, ox + 3]) win.print("TIME", Point[oy + 11, ox + 3]) win.print("LINE", Point[oy + 14, ox + 3]) win.print("LEVEL", Point[oy + 17, ox + 3]) win.draw_box(oy + 1, ox + 18, 22, 22, Box::DOUBLE) win.print("NEXT", Point[oy, ox + 46]) win.draw_box(oy + 1, ox + 42, 14, 18, Box::NORMAL) end |
#notify_lock_reset ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/game.rb', line 174 def notify_lock_reset return unless @grounded if @lock_resets < MAX_LOCK_RESETS @lock_timer = Time.now + LOCK_DELAY @lock_resets += 1 end end |
#over? ⇒ Boolean
128 129 130 131 132 133 134 135 136 |
# File 'lib/game.rb', line 128 def over? @piece.nil? || !@piece.valid? || case @mode when :marathon then @line >= 150 when :sprint then @line >= 40 when :ultra then @time <= 0 when :infinite then false end end |
#show_game_over ⇒ Object
Called AFTER Curses.close_screen — prints to normal stdout
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/game.rb', line 214 def show_game_over puts puts GAME_OVER_ART puts sleep 0.3 typewriter " Score: #{@score}" puts typewriter " Lines: #{@line}" puts typewriter " Level: #{@level}" puts typewriter " Time: #{format_time(@time)}" puts puts sleep 0.5 = GAME_OVER_MESSAGES.sample typewriter " #{} " sleep 0.4 print "💋" $stdout.flush puts puts end |
#soft_drop ⇒ Object
147 148 149 150 151 |
# File 'lib/game.rb', line 147 def soft_drop sync do @piece..tif { @score += 1 } end end |
#speed ⇒ Object
244 245 246 247 |
# File 'lib/game.rb', line 244 def speed base = 0.8 - ((@level - 1) * 0.007) base ** (@level - 1) end |
#start ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/game.rb', line 69 def start return if @running @running = true @gravity_thread = Thread.new do while @running sleep speed next if @paused || over? sync do if @piece. @grounded = false @score += 1 else start_lock_timer end end end end @lock_thread = Thread.new do while @running sleep 0.05 next if @paused || over? sync do if @lock_timer && Time.now >= @lock_timer lock_piece end end end end @timer_thread = Thread.new do while @running sleep 1 next if @paused || over? @time += @mode == :ultra ? -1 : 1 end end end |
#stop ⇒ Object
113 114 115 116 117 118 |
# File 'lib/game.rb', line 113 def stop @running = false [@gravity_thread, @lock_thread, @timer_thread].each do |t| t&.join(1) end end |
#sync ⇒ Object
124 125 126 |
# File 'lib/game.rb', line 124 def sync @mutex.synchronize { yield if block_given? } end |
#toggle ⇒ Object
120 121 122 |
# File 'lib/game.rb', line 120 def toggle @paused = !@paused end |