samples/05_mouse/01_mouse_click/app/main.rb
# Uses borders to create grid squares for the game's board. Also outputs the game pieces using labels.
Uses borders to create grid squares for the game's board. Also outputs the game pieces using labels.
Uses borders to create grid squares for the game ' s board . Also outputs the game pieces using labels .
def render_board
    square_size = 80
    board_left = grid.w_half - square_size * 1.5
    board_top  = grid.h_half - square_size * 1.5
    outputs.borders << all_spaces do |x, y, space| 
      space.border ||= [
        board_left + x.add(1) * square_size, 
        board_top  + y.add(1) * square_size,
        square_size,
        square_size
      ]
    end
    outputs.labels << filled_spaces do |x, y, space| 
          label board_left + x.add(1) * square_size + square_size.fdiv(2),
          board_top  + y.add(1) * square_size + square_size - 20,
          space.piece 
    end
    outputs.labels << if state.x_won
                        label grid.w_half, grid.top - 80, "x won" 
                      elsif state.o_won
                        label grid.w_half, grid.top - 80, "o won" 
                      elsif state.draw
                        label grid.w_half, grid.top - 80, "a draw"
                      else 
                        label grid.w_half, grid.top - 80, "turn: #{state.current_turn}"
                      end
  end
def render_board square_size = 80 board_left = grid . w_half - square_size * 1.5 board_top = grid . h_half - square_size * 1.5 outputs . borders << all_spaces do | x , y , space | space . border ||= [ board_left + x . add ( 1 ) * square_size , board_top + y . add ( 1 ) * square_size , square_size , square_size ] end outputs . labels << filled_spaces do | x , y , space | label board_left + x . add ( 1 ) * square_size + square_size . fdiv ( 2 ) , board_top + y . add ( 1 ) * square_size + square_size - 20 , space . piece end outputs . labels << if state . x_won label grid . w_half , grid . top - 80 , "x won" elsif state . o_won label grid . w_half , grid . top - 80 , "o won" elsif state . draw label grid . w_half , grid . top - 80 , "a draw" else label grid . w_half , grid . top - 80 , "turn: #{state.current_turn}" end end
Uses borders to create grid squares for the game's board.
Uses borders to create grid squares for the game ' s board .
# Positions the game's board in the center of the screen. # Try removing what follows grid.w_half or grid.h_half and see how the position changes! # At first glance, the add(1) looks pretty trivial. But if you remove it, # you'll see that the positioning of the board would be skewed without it! # Or if you put 2 in the parenthesis, the pieces will be placed in the wrong squares # due to the change in board placement. # outputs borders for all board spaces # space.border is initialized using this definition # Again, the calculations ensure that the piece is placed in the center of the grid square. # Remove the '- 20' and the piece will be placed at the top of the grid square instead of the center. # put label in each filled space of board # text of label, either "x" or "o" # Uses a label to output whether x or o won, or if a draw occurred. # If the game is ongoing, a label shows whose turn it currently is. # the '-80' positions the label 80 pixels lower than top # grid.w_half positions the label in the center horizontally # if no one won and the game is ongoing