0

So I am attempting to replicate the game stacker in ruby2d. I have draw my three starting blocks to the screen, but I am having trouble making them move to the right when the game starts.

here is the beginning of the class(mainly put here to show the starting position of the blocks). I omitted the part of the class where I draw the squares but know it is included in the code.

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

Simply i want my three blocks to move to the right until they hit the side of the window. right now they just move into the upper left-hand corner of the window (0,0). Any help is greatly appreciated

maxwe11
  • 309
  • 3
  • 8

0 Answers0