0

所以我试图在 ruby​​2d 中复制游戏堆栈器。我已经将我的三个起始块绘制到屏幕上,但是当游戏开始时我无法让它们向右移动。

这里是类的开始(主要是放在这里显示积木的开始位置)。我省略了类中绘制正方形的部分,但知道它包含在代码中。

require 'ruby2d'
set fps_cap: 1
set title: 'Stacker'

set background: 'blue'
GRID_SIZE = 40

#width = 640/ 40 = 16
#height = 480/ 40 = 12


# Create a class that is the starting position of three centered blocks along the bottom of the screen
class Blocks
  def initialize
    @positions = [[6,11], [7,11], [8,11]]
    @direction = 'right'
  end

#draw the actual blocks on the screen
  def draw
    @positions.each do |position|
      Square.new(x: position[0] * GRID_SIZE, y: position[1] * GRID_SIZE, size: GRID_SIZE - 1, color: 'white')
    end
  end

#move the blocks right first and then have them bounce off the wall and move the opposite direction
#Have the blocks stop moving when I hit the spacebar
#Have the blocks fall if they are not places above a previouslt place block or the bottom floor
  def move
    @positions.shift

    case @direction
    when 'right'
      @positions.push(head[0] + 1, head[1])
    when 'left'
      @positions.push(first[0] - 1, first[1])
    end
  end

  private

#define the right side of the blocks
  def head
    @positions.last
  end
#define the left side of the blocks
  def first
    @positions.first
  end
end
#create an instance variable of the blocks to start the game
blocks = Blocks.new

#during each frame of the game clear the previously drawn blocks and then re-draw them into their
#moved position
update do
  clear

  blocks.draw
  blocks.move
end
#When the spacebar is pressed, stop the blocks from moving
on :key_down do |event|
end


show

只是我希望我的三个街区向右移动,直到它们撞到窗户的一侧。现在他们只是移动到窗口的左上角(0,0)。任何帮助是极大的赞赏

4

0 回答 0