MinecraftU / 2021-computer-adventures

Repository for our 2021 Computer Adventures Ruby project!
0 stars 0 forks source link

Move tetrominos instead of redrawing every tick #19

Closed dealingwith closed 3 years ago

dealingwith commented 3 years ago

http://www.ruby2d.com/learn/2d-basics/#example-of-moving-a-square-shape-with-keys

jamespeilunli commented 3 years ago

I think literally moving the squares would be more complicated; I think we should just still draw from the matrix, but have an array with the squares and calling .remove on each square instance in the array. source. But it appears that you can also call .add to add the square back to the screen, so this might not work (but at least the things won't be drawn, which should be the major source of the lag).

jamespeilunli commented 3 years ago

Using my idea, this:

  def draw(start_pos, size) # size is the side length of a square
    (0...gameboard.width).each do |i|
      (0...gameboard.height).each do |j|
        if squares[j, i] != 0
          squares[j, i].remove # Ruby2D::Square#remove method https://www.ruby2d.com/learn/2d-basics/#adding-and-removing-objects
        end
        if gameboard[j, i] != 0
          color = color_map[gameboard[j, i]] 
          # draws a square starting at point (x, y) with side length size and color color. z is the layer (the higher z is, the higher on the layers the shape is)
          squares[j, i] = Square.new(
            x: start_pos[0] + size*i, y: start_pos[1] + size*j,
            size: size,
            color: color,
            z: 10
          )
        end
      end
    end
  end

draw method and adding set background: "white" to tetris.rb, there was no more lag.

dealingwith commented 3 years ago

make it so