0

我需要在我的代码中使用“ruby2d”并编写一个简单的程序来构建我的蛇游戏。我试图让程序在屏幕上弹出一个窗口。这是我的代码

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

我还在终端中运行了命令 gem install ruby​​2d 它给了我这个错误,

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.
4

1 回答 1

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
于 2021-07-27T05:28:42.777 回答