I am simply trying to use ruby 2d and it is not showing me any output

318 views Asked by At

I require 'ruby2d' in my code and write a simple program to build onto for my snake game. I am trying to get the program to pop-up a window on the screen. This is my code

require 'ruby2d'

set background: 'navy'

#width = 640 / 20 =32
#height = 480 / 2- = 24
GRID_SIZE = 20

class Snake
    def init
        @positions = [[2,0], [2,1], [2,2], [2,3]]
    end

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

snake = Snake.new
snake.draw
show

I also ran the command gem install ruby2d in the terminal and it gave me this error,

Fetching ruby2d-0.10.0.gem
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
1

There are 1 answers

2
carlitos On

Matt your code works, but you need to check a few things:

  1. Check if your ruby environment it's running correctly, rbenv could be a great solutions for that, I run your code under ruby 2.5.0
  2. The main issue that I saw in your code was the constructor needs to be initialize instead of init