矢量“玩家”而不是使用 Chingu 和 Gosu 的图像
Chingu 示例看起来像这样:
require 'rubygems'
require 'chingu'
class Game < Chingu::Window
def initialize
super
@player = Player.new
end
end
class Player < Chingu::GameObject
def initialize(options = {})
super(options.merge(:image => Gosu::Image["player.png"])
end
end
Game.new.show
如果我希望用线条而不是图像来绘制 Player 对象,我将如何去做呢?
下面的代码看起来很直观,但我无法让它工作!
class Player < Chingu::BasicGameObject
def initialize(options = {})
super
@radius = options[:radius]
@c = Gosu::Color.new(0xffff0000)
end
def draw
$window.draw_rect([@x-1,@y+1,@x+1,@y+1,@x+1,@y-1,@x+1,@y+1],@c,1)
end
end
我做错了什么吗?
The Chingu example looks something like this:
require 'rubygems'
require 'chingu'
class Game < Chingu::Window
def initialize
super
@player = Player.new
end
end
class Player < Chingu::GameObject
def initialize(options = {})
super(options.merge(:image => Gosu::Image["player.png"])
end
end
Game.new.show
If I want the Player object to be drawn with lines rather than images, how would I go about doing this?
The following code seems intuitive, but I can't get it to work!
class Player < Chingu::BasicGameObject
def initialize(options = {})
super
@radius = options[:radius]
@c = Gosu::Color.new(0xffff0000)
end
def draw
$window.draw_rect([@x-1,@y+1,@x+1,@y+1,@x+1,@y-1,@x+1,@y+1],@c,1)
end
end
Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们弄清楚一下。
我假设这些是您实际代码的不完整片段,
由于所示代码调用draw_rect并将@x和@y设置为nil,
抛出“未定义方法”-“for nil:nilClass”异常,因为
你不能从 nil 中减去任何东西。)
我怀疑你看到的是一个空白窗口,没有绘制任何东西,
因为正如所写,你的 Player.draw 永远不会被调用。
为什么?因为Chingu 为所有人提供自动绘图和更新
它的 GameObjects,但前提是您使用 GameObject.create 而不是
游戏对象.new。
(http://rdoc.info/projects/ippa/chingu)
所以我们需要解决这个问题。然而......
现在 Player.draw 每一帧都被正确调用
通过Chingu,我们发现了一个新问题:
调用draw_rect不起作用!这就是 Ruby 告诉我的:
in
draw_rect': undefined method
x' for [99, 101, 101, 101, 101, 99, 101, 101]:Array (NoMethodError)嗯...我可以看到传递给draw_rect方法的内容,
我想知道它期望收到什么?让我们看一下代码。
(http://github.com/ippa/chingu /blob/master/lib/chingu/helpers/gfx.rb)
啊,现在说得通了。 draw_rect 期望传递一个 Rectangle 对象,而不是
一堆坐标。这是:(
http://rdoc.info/projects/ippa/chingu)
所以我们只需要先创建一个 Rect 对象,然后调用
以该 Rect 作为第一个参数的draw_rect。
好吧,我们就这么做吧。这是工作代码——
现在运行它会在 100,100 处显示一个红色小矩形。
希望有帮助。
c~
Let's figure it out.
I assume these are incomplete snippets of your actual code,
since the code as shown calls draw_rect with @x and @y set to nil,
throwing an 'undefined method '-' for nil:nilClass' exception because
you can't subtract anything from nil.)
I suspect you are seeing a blank window with nothing drawn,
because as written, your Player.draw will never get called.
Why? Because Chingu provides automated drawing and updating for all
its GameObjects, but only if you use GameObject.create instead of
GameObject.new.
(http://rdoc.info/projects/ippa/chingu)
So we need to fix that. However...
Now that Player.draw is getting properly called every frame
by Chingu, we have find a new problem: the
call to draw_rect doesn't work! This is what Ruby tells me:
in
draw_rect': undefined method
x' for [99, 101, 101, 101, 101, 99, 101, 101]:Array (NoMethodError)Hmmm... I can see what is getting passed into the draw_rect method,
I wonder what it expects to receive? Let's look at the code.
(http://github.com/ippa/chingu/blob/master/lib/chingu/helpers/gfx.rb)
Ah, now it makes sense. draw_rect expects to be passed a Rectangle object, not
a bunch of coordinates. Here it is:
(http://rdoc.info/projects/ippa/chingu)
So we just need to create a Rect object first, and then call
draw_rect with that Rect as the first parameter.
Okay, let's do that. Here's the working code --
Running it now shows a small red rectangle at 100,100.
Hope that helps.
c~