Class: GamesParadise::GUI::GameWindow1010
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- GamesParadise::GUI::GameWindow1010
- Defined in:
- lib/games_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
-
#clicked_x ⇒ Object
readonly
Returns the value of attribute clicked_x.
Instance Method Summary collapse
-
#button_down(id) ⇒ Object
# === button_down.
-
#button_up(id) ⇒ Object
# === button_up.
-
#cell_color(value = 0) ⇒ Object
# === cell_color ========================================================================= #.
-
#clicked_xy ⇒ Object
# === clicked_xy ========================================================================= #.
-
#clicked_y? ⇒ Boolean
(also: #clicked_y)
# === clicked_y? ========================================================================= #.
-
#consider_drawing_the_help_screen(x = 340, y = 340) ⇒ Object
# === consider_drawing_the_help_screen (help tag).
- #corrected_x(tile, x) ⇒ Object
-
#dragged_xy ⇒ Object
# === dragged_xy ========================================================================= #.
-
#draw ⇒ Object
# === draw (draw tag).
-
#draw_background ⇒ Object
# === draw_background ========================================================================= #.
- #draw_board ⇒ Object
-
#draw_cell(i, j, value) ⇒ Object
# === draw_cell ========================================================================= #.
-
#draw_corners(x, y, width, height, color = nil, size = 0) ⇒ Object
# === draw_corners ========================================================================= #.
-
#draw_dragged_tile ⇒ Object
# === draw_dragged_tile ========================================================================= #.
- #draw_open_pos ⇒ Object
-
#draw_option_tile(tile, pos, selected = false) ⇒ Object
# === draw_option_tile ========================================================================= #.
- #draw_options ⇒ Object
- #draw_rect(x, y, z, width, height, color = Gosu::Color::WHITE) ⇒ Object
-
#draw_score ⇒ Object
# === draw_score.
-
#draw_sqr(x, y, z, length, color = Gosu::Color::WHITE, corner = 0) ⇒ Object
# === draw_sqr ========================================================================= #.
- #draw_status ⇒ Object
-
#get_screen_position ⇒ Object
# === get_screen_position ========================================================================= #.
- #get_selected_option ⇒ Object
-
#initialize(auto = false) ⇒ GameWindow1010
constructor
#.
-
#needs_cursor? ⇒ Boolean
# === needs_cursor? ========================================================================= #.
- #place_selected_tile ⇒ Object
-
#reset ⇒ Object
# === reset ========================================================================= #.
-
#restart ⇒ Object
# === restart ========================================================================= #.
-
#start ⇒ Object
# === start ========================================================================= #.
-
#stop ⇒ Object
# === stop ========================================================================= #.
-
#update ⇒ Object
update.
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
#
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 52 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_x ⇒ Object (readonly)
Returns the value of attribute clicked_x.
19 20 21 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 19 def clicked_x @clicked_x end |
Instance Method Details
#button_down(id) ⇒ Object
#
button_down
Handle all button-down events next.
#
526 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 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 526 def (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.
#
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 591 def (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
#
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 134 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_xy ⇒ Object
#
clicked_xy
#
178 179 180 181 182 183 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 178 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?
#
616 617 618 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 616 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.
#
674 675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 674 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
306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 306 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_xy ⇒ Object
#
dragged_xy
#
171 172 173 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 171 def dragged_xy [@drag_x, @drag_y] end |
#draw ⇒ Object
#
draw (draw tag)
All draw-related actions are gathered here.
#
436 437 438 439 440 441 442 443 444 445 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 436 def draw draw_background draw_board draw_score consider_drawing_the_help_screen draw_dragged_tile draw_open_pos draw_status end |
#draw_background ⇒ Object
#
draw_background
#
124 125 126 127 128 129 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 124 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_board ⇒ Object
342 343 344 345 346 347 348 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 342 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
#
161 162 163 164 165 166 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 161 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
#
238 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 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 238 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_tile ⇒ Object
#
draw_dragged_tile
#
659 660 661 662 663 664 665 666 667 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 659 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_pos ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 319 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
#
641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 641 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_options ⇒ Object
331 332 333 334 335 336 337 338 339 340 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 331 def 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
293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 293 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_score ⇒ Object
#
draw_score
This method will draw the score-value onto the game map.
#
115 116 117 118 119 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 115 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
#
230 231 232 233 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 230 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_status ⇒ Object
392 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 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 392 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_position ⇒ Object
#
get_screen_position
#
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 188 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_option ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 210 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?
#
106 107 108 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 106 def needs_cursor? true end |
#place_selected_tile ⇒ Object
350 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 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 350 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 |
#reset ⇒ Object
#
reset
#
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 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 69 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 |
#restart ⇒ Object
#
restart
#
513 514 515 516 517 518 519 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 513 def restart @board.init(0) if @auto self.caption = "1010! Runs: #{@auto_count} Max Score: #{@max_score}" end @ended = false end |
#start ⇒ Object
#
start
#
493 494 495 496 497 498 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 493 def start @start_time = Time.now @auto ? @board.init(0) : @board.init @ended = false show end |
#stop ⇒ Object
#
stop
#
503 504 505 506 507 508 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 503 def stop stop_time = Time.now puts "Total time played = #{(stop_time - @start_time).to_i} seconds" puts "Score = #{@board.score}" close end |
#update ⇒ Object
update
448 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 |
# File 'lib/games_paradise/gui/gosu/1010/game_1010.rb', line 448 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. @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.(@option_tiles) end |