0

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 Answers1

0

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
carlitos
  • 76
  • 5
  • I swiched the init to initialize but still got the same error. I got it so that I could install the gem, but it still gave me the same error. This machine doesn't seem to want to run ruby2d. Let me know what else the problem could be. Also it seems my ruby environment is running correctly when I run ruby -e "" in my terminal I can run simple ruby code but not ruby2d... – mattprowlinson Jul 27 '21 at 22:31
  • It runs perfectly https://gist.github.com/carlitos/f9d0e7682068f8ff62ea214112866e8d – carlitos Jul 30 '21 at 05:31