Class: GamesAndRpgParadise::GUI::GameWindow1010

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb

Constant Summary collapse

TITLE_TO_USE =
#

TITLE_TO_USE

#
'1010!'
COLORS =
#

COLORS

#
{
  maroon: ::Gosu::Color.new(*[222, 217,  17, 152]),
  purple: ::Gosu::Color.new(*[245, 119,  47, 192]),
  brown:  ::Gosu::Color.new(*[226, 236,  85,  37]),
  grey:   ::Gosu::Color.new(*[62,  121, 218, 109]),
  pink:   ::Gosu::Color.new(*[193, 254,   9, 124]),
  bleau:  ::Gosu::Color.new(*[250,   5, 145, 245]),
  green:  ::Gosu::Color.new(*[248,  27, 184, 139])
}.freeze
FONT_COLOR_RED =
#

FONT_COLOR_RED

#
Gosu::Color::RED
LINE_COLOR =
#

LINE_COLOR

#
Gosu::Color::BLUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Gosu::Window

#gosu_button_down?, #image, #image10?, #image1?, #image2?, #image3?, #image4?, #image5?, #image6?, #image7?, #image8?, #image9?, #on_left_arrow_pressed?, #on_right_arrow_pressed?, #q_means_quit, #set_font, #set_title, #sqrt, #tab_key?, #write_this_text

Constructor Details

#initialize(auto = false) ⇒ GameWindow1010

#


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 53

def initialize(auto = false)
  @width  = 480
  @height = 480
  super(@width, @height, {fullscreen: false})
  self.caption = TITLE_TO_USE
  @board           = Board1010.new # Dependency injection?
  
  @selected_row    = @selected_col = -1
  @clicked_x       = @clicked_y    = -1
  reset
  @ended = @undo = @show_pos = @record = @playback = @drag = @step = false
  @auto  = auto
end

Instance Attribute Details

#clicked_xObject (readonly)

Returns the value of attribute clicked_x.



20
21
22
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 20

def clicked_x
  @clicked_x
end

Instance Method Details

#button_down(id) ⇒ Object

#

button_down

Handle all button-down events next.

#


527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 527

def button_down(id)
  case id
  # ======================================================================= #
  # === Button 'H' pressed, which means that the help-menu will be shown
  # ======================================================================= #
  when Gosu::KbH
    @show_the_help_menu = true
  # ======================================================================= #
  # === Escape button will stop the game
  # ======================================================================= #
  when Gosu::KbEscape
    stop
  # ======================================================================= #
  # === The 'A' button was pressed
  # ======================================================================= #
  when Gosu::KbA
    @auto = !@auto
  when Gosu::KbB
    # about
  when Gosu::KbC
    # cheat
    @board.pos_exists?(@option_tiles, true)
  when Gosu::KbE
    @step = true
  when Gosu::KbN
    # new
    restart
  when Gosu::KbO
    @show_pos = true
  when Gosu::KbP
    self.caption = "1010! (playback)"
    @playback = true
    restart
  when Gosu::KbQ
    @board.stop
  when Gosu::KbR
    @record = true
    self.caption = "1010! (recording)"
  when Gosu::KbS
    @board.save
  when Gosu::KbU
    @undo = true
  when Gosu::Kb1
    @selected_option = 1
  when Gosu::Kb2
    @selected_option = 2
  when Gosu::Kb3
    @selected_option = 3
  when Gosu::MsRight
    # do nothing
  when Gosu::MsLeft
    @drag      = true
    @clicked_x = mouse_x
    @clicked_y = mouse_y
    @selected_option = get_selected_option if @selected_option.zero?
  else
    puts "Button down pressed with id = #{id}"
  end
end

#button_up(id) ⇒ Object

#

button_up

This is the button-release action.

#


592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 592

def button_up(id)
  case id
  # ======================================================================= #
  # The 'H' key is used for the help-screen display.
  # ======================================================================= #
  when Gosu::KbH
    @show_the_help_menu = false
  # ======================================================================= #
  # === Mouse left event
  # ======================================================================= #
  when Gosu::MsLeft
    @drag = false
    @clicked_x = mouse_x
    @clicked_y = mouse_y
    @selected_row, @selected_col = get_screen_position if @selected_row < 0 || @selected_col < 0
  when Gosu::MsRight
    # do nothing
  when Gosu::KbO # on key '0' pressed.
    @show_pos = false
  end
end

#cell_color(value = 0) ⇒ Object

#

cell_color

#


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 135

def cell_color(value=0)
  color = case value
          when 0
            COLORS[:grey]
          when -1
            COLORS[:bleau]
          when -2
            COLORS[:maroon]
          when 1
            COLORS[:green]
          when 2
            COLORS[:brown]
          when 3
            Gosu::Color::FUCHSIA
          when 4
            COLORS[:purple]
          else
            Gosu::Color::BLUE
  end

  # puts "Cell color of #{value} = #{color}"
  color
end

#clicked_xyObject

#

clicked_xy

#


179
180
181
182
183
184
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 179

def clicked_xy
  x = @clicked_x
  y = @clicked_y # these are from the latest click
  @clicked_x = @clicked_y = -1 # so immediately reset the co-ordinates
  [x, y]
end

#clicked_y?Boolean Also known as: clicked_y

#

clicked_y?

#

Returns:

  • (Boolean)


617
618
619
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 617

def clicked_y?
  @clicked_y
end

#consider_drawing_the_help_screen(x = 340, y = 340) ⇒ Object

#

consider_drawing_the_help_screen (help tag)

This method will draw the on-game-map help screen.

#


675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 675

def consider_drawing_the_help_screen(
    x = 340,
    y = 340
  )
  return unless @show_the_help_menu
  @help_font.red_text('ESC: abort',       x, y,     )
  @help_font.red_text('A: auto',          x, y +  20)
  @help_font.red_text('N: new game',      x, y +  40)
  @help_font.red_text('O: show position', x, y +  60)
  @help_font.red_text('Q: save and quit', x, y +  80)
  @help_font.red_text('S: save',          x, y + 100)
  @help_font.red_text('U: undo',          x, y + 120)
end

#corrected_x(tile, x) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 307

def corrected_x(tile, x)
  new_x = x
  cell  = tile.first
  cell.each do |val|
    if val.zero?
      new_x -= (@size + @gap)
    else
      break
    end
  end
  new_x
end

#dragged_xyObject

#

dragged_xy

#


172
173
174
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 172

def dragged_xy
  [@drag_x, @drag_y]
end

#drawObject

#

draw (draw tag)

All draw-related actions are gathered here.

#


437
438
439
440
441
442
443
444
445
446
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 437

def draw
  draw_background
  draw_board
  draw_score
  consider_drawing_the_help_screen
  draw_options
  draw_dragged_tile
  draw_open_pos
  draw_status
end

#draw_backgroundObject

#

draw_background

#


125
126
127
128
129
130
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 125

def draw_background
  draw_rect(0, 0, 0, @width + 5, @height + 5, Gosu::Color::WHITE)
  boarder = 10 * (@size + @gap) + 1
  draw_rect(boarder, 0, 0, 5, boarder + 5, Gosu::Color::GRAY)
  draw_rect(0, boarder, 0, boarder,   5, Gosu::Color::GRAY)
end

#draw_boardObject



343
344
345
346
347
348
349
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 343

def draw_board
  @board.arr.each_with_index do |row, i|
    row.each_with_index do |val, j|
      draw_cell(i, j, val)
    end
  end
end

#draw_cell(i, j, value) ⇒ Object

#

draw_cell

#


162
163
164
165
166
167
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 162

def draw_cell(i, j, value)
  x = @gap + (j * (@size + @gap))
  y = @gap + (i * (@size + @gap))
  z = 1
  draw_sqr(x, y, z, @size, cell_color(value), 4)
end

#draw_corners(x, y, width, height, color = nil, size = 0) ⇒ Object

#

draw_corners

#


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 239

def draw_corners(x, y, width, height, color=nil, size=0)
  return if size.zero?
  color ||= Gosu::Color::WHITE

  z = 1

  # Top Left
  x1 = x
  y1 = y
  c1 = color
  x2 = x1 + size
  y2 = y1
  c2 = color
  x3 = x1
  y3 = y1 + size
  c3 = color
  draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, :default)

  # Top Right
  x1 = x + width
  y1 = y
  c1 = color
  x2 = x1 - size
  y2 = y1
  c2 = color
  x3 = x1
  y3 = y1 + size
  c3 = color
  draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, :default)

  # Bot Left
  x1 = x
  y1 = y + height
  c1 = color
  x2 = x1
  y2 = y1 - size
  c2 = color
  x3 = x1 + size
  y3 = y1
  c3 = color
  draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, :default)

  # Bot Right
  x1 = x + width
  y1 = y + height
  c1 = color
  x2 = x1 - size
  y2 = y1
  c2 = color
  x3 = x1
  y3 = y1 - size
  c3 = color
  draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, :default)
end

#draw_dragged_tileObject

#

draw_dragged_tile

#


660
661
662
663
664
665
666
667
668
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 660

def draw_dragged_tile
  return unless @drag
  return unless [1,2,3].include?(@selected_option)

  x, y = dragged_xy
  tile = @option_tiles[@selected_option - 1]
     x = corrected_x(tile, x)
  _draw_option_tile(tile, x, y)
end

#draw_open_posObject



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 320

def draw_open_pos
  return unless @show_pos

  tile      = @option_tiles[@selected_option - 1]
  bpos      = @board.best_starting_position(tile)
  positions = (bpos ? [bpos] : [])
  positions.each do |pos|
    i, j = pos
    draw_cell(i, j, -2)
  end
end

#draw_option_tile(tile, pos, selected = false) ⇒ Object

#

draw_option_tile

#


642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 642

def draw_option_tile(tile, pos, selected = false)
  return unless (1 .. Board1010::MAX_OPTIONS).cover?(pos)

  gap  = @gap  / 2
  size = @size / 2

  x = 360
  y = (@option_cell_size * pos) + ((pos - 1) * (size + gap) * 5)

  gap, size = _draw_option_tile(tile, x, y, selected && Gosu::Color::YELLOW, gap, size)

  draw_line(360, ((@option_cell_size / 2) * pos) + ((pos - 1) * (size + gap) * 5), LINE_COLOR,
            460, ((@option_cell_size / 2) * pos) + ((pos - 1) * (size + gap) * 5), LINE_COLOR, 1)
end

#draw_optionsObject



332
333
334
335
336
337
338
339
340
341
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 332

def draw_options
  n = 1
  @option_tiles.each_with_index { |tile, i|
    if tile
      draw_option_tile(tile, (i + 1), @selected_option == (i + 1))
      n += 1
    end
  }
  n
end

#draw_rect(x, y, z, width, height, color = Gosu::Color::WHITE) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 294

def draw_rect(x, y, z, width, height, color=Gosu::Color::WHITE)
  c1 = c2 = c3 = c4 = color
  x1 = x
  y1 = y
  x2 = x1 + width
  y2 = y1
  x3 = x1
  y3 = y1 + height
  x4 = x2
  y4 = y3
  Gosu.draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, :default)
end

#draw_scoreObject

#

draw_score

This method will draw the score-value onto the game map.

#


116
117
118
119
120
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 116

def draw_score
  @score_font.draw_text(
    "Score: #{@board.score}", 20, 360, 1, 1.2, 1.2, FONT_COLOR_RED
  )
end

#draw_sqr(x, y, z, length, color = Gosu::Color::WHITE, corner = 0) ⇒ Object

#

draw_sqr

#


231
232
233
234
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 231

def draw_sqr(x, y, z, length, color=Gosu::Color::WHITE, corner=0)
  draw_rect(x, y, z, length, length, color)
  draw_corners(x, y, length, length, Gosu::Color::WHITE, corner)
end

#draw_statusObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 393

def draw_status
  @count += 1
  if @ended
    @score_font.draw("Score: #{@board.score}     Game Over!", 20, 360, 1, 1.0, 1.0, FONT_COLOR_RED)
    if @auto
      @auto_count += 1
      if @board.score > @max_score
        puts "Score = #{@board.score}"
        @max_score  = @board.score
      end
      restart
    else
      @score_font.draw("N to start new game, ESC to Quit", 20, 390, 1, 1.0, 1.0, FONT_COLOR_RED)
    end
  end

  return unless @record

  if @count.even?
    x1 = 20
    y1 = 400
    x2 = 40
    y2 = 400
    x3 = 30
    y3 = 410
  else
    x1 = 20
    y1 = 400
    x2 = 40
    y2 = 400
    x3 = 30
    y3 = 390
  end

  z = 1
  c1 = c2 = c3 = Gosu::Color::RED
  draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, :default)
end

#get_screen_positionObject

#

get_screen_position

#


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 189

def get_screen_position
  x, y = clicked_xy
  i = -1
  j = -1

  Board1010::MAX_COLS.times do |jj|
    if (x >= @gap + (@size + @gap) * jj) && (x < @gap + (@size + @gap) * (jj + 1))
      j = jj
      break
    end
  end

  Board1010::MAX_ROWS.times { |ii|
    if (y >= @gap + (@size + @gap) * ii) && (y < @gap + (@size + @gap) * (ii + 1))
      i = ii
      break
    end
  }

  [i, j]
end

#get_selected_optionObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 211

def get_selected_option
  x, y = clicked_xy
  opt  = 0
  gap  = @gap  / 2
  size = @size / 2
  (1..Board1010::MAX_OPTIONS).each do |i|
    y1 = (@option_cell_size * i) + ((i - 1) * (size + gap) * 5)
    y2 = (@option_cell_size * (i + 1)) + (i * (size + gap) * 5)
    if (x > 360) && (y1 < y) && (y < y2)
      opt = i
      break
    end
  end

  opt
end

#needs_cursor?Boolean

#

needs_cursor?

#

Returns:

  • (Boolean)


107
108
109
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 107

def needs_cursor?
  true
end

#place_selected_tileObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 351

def place_selected_tile
  if @placed_positions.empty?
    if @rows_to_clean.empty? && @cols_to_clean.empty?
      @rows_to_clean, @cols_to_clean = @board.cleanup(true)
    else
      unless @rows_to_clean.empty?
        i = @rows_to_clean.shift
        Board1010::MAX_ROWS.times {|j| @board.cell = [i, j, 0] }
      end

      unless @cols_to_clean.empty?
        j = @cols_to_clean.shift
        Board1010::MAX_COLS.times {|i| @board.cell = [i, j, 0] }
      end

      sleep(0.25) unless @auto
    end
  else
    i, j, val = @placed_positions.shift
    @board.cell = [i, j, val]
    return
  end

  return unless (@selected_option > 0) && (@selected_row >= 0) && (@selected_col >= 0)

  unless @undo
    @prev_score   = @board.score
    @prev_arr     = Marshal.load(Marshal.dump(@board.arr))
    @prev_option_tiles = Marshal.load(Marshal.dump(@option_tiles))
  end
  tile = @option_tiles[@selected_option - 1]
  new_score = @board.place(tile, @selected_row, @selected_col, true)
  if new_score > 0
    @board.score += new_score
    @placed_positions = @board.placed_pos
    @option_tiles.delete_at(@selected_option - 1)
  end

  @selected_option = 0
  @selected_row = @selected_col = -1
end

#resetObject

#

reset

#


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
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 70

def reset
  ## These should be even so that div by 2 is proper int
  @gap  = 2
  @size = 32
  @option_cell_size = 20

  @recording = []
  # === @placed_positions
  @placed_positions = []
  # === @rows_to_clean
  @rows_to_clean = []
  # === @cols_to_clean
  @cols_to_clean = []
  @auto_count = @max_score = @count = 0
  # ======================================================================= #
  # === @score_font
  # ======================================================================= #
  @score_font = Gosu::Font.new(30)
  # ======================================================================= #
  # === @show_the_help_menu
  # ======================================================================= #
  @show_the_help_menu = false
  # ======================================================================= #
  # === @selected_option
  # ======================================================================= #
  @selected_option = 0
  # ======================================================================= #
  # === @help_font
  #
  # The help-font is the one that is used for the on-game help screen.
  # ======================================================================= #
  @help_font = Gosu::Font.new(22)
end

#restartObject

#

restart

#


514
515
516
517
518
519
520
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 514

def restart
  @board.init(0)
  if @auto
    self.caption = "1010!  Runs: #{@auto_count}  Max Score: #{@max_score}"
  end
  @ended = false
end

#startObject

#

start

#


494
495
496
497
498
499
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 494

def start
  @start_time = Time.now
  @auto ? @board.init(0) : @board.init
  @ended = false
  show
end

#stopObject

#

stop

#


504
505
506
507
508
509
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 504

def stop
  stop_time = Time.now
  puts "Total time played = #{(stop_time - @start_time).to_i} seconds"
  puts "Score = #{@board.score}"
  close
end

#updateObject

update



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/games_and_rpg_paradise/gui/gosu/1010/game_1010.rb', line 449

def update
  if @drag
    @drag_x = mouse_x
    @drag_y = mouse_y
    return
  end

  if @playback && !@recording.empty?
    puts "playing back"
    @selected_row, @selected_col, @selected_option, @option_tiles = @recording.shift
    return
  end

  if @undo
    @board.score = @prev_score
    @board.restore_arr(@prev_arr)
    @option_tiles = @prev_option_tiles

    @undo = false
  else
    @option_tiles = @board.options
    @option_tiles = @board.generate_tiles if @option_tiles.empty?
    @ended = !@board.pos_exists?(@option_tiles)
  end

  if @auto || @step
    @selected_option = rand(@option_tiles.size) + 1
    tile = @option_tiles[@selected_option - 1]
    position = @board.best_starting_position(tile)
    @selected_row, @selected_col = position if position
    @step = false
  else
    if @record
      @recording << [@selected_row, @selected_col, @selected_option, @option_tiles]
    end
  end

  place_selected_tile

  @board.restore_options(@option_tiles)
end