矢量“玩家”而不是使用 Chingu 和 Gosu 的图像

发布于 2024-09-08 16:38:53 字数 703 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2024-09-15 16:38:53

让我们弄清楚一下。

我假设这些是您实际代码的不完整片段,
由于所示代码调用draw_rect并将@x和@y设置为nil,
抛出“未定义方法”-“for nil:nilClass”异常,因为
你不能从 nil 中减去任何东西。)

我怀疑你看到的是一个空白窗口,没有绘制任何东西,
因为正如所写,你的 Player.draw 永远不会被调用。

为什么?因为Chingu 为所有人提供自动绘图和更新
它的 GameObjects,但前提是您使用 GameObject.create 而不是
游戏对象.new。

(http://rdoc.info/projects/ippa/chingu)

Chingu::GameObject

在游戏中使用它
对象。玩家、敌人、
子弹、能量提升、战利品
躺在周围。它非常可重复使用并且
不包含任何游戏逻辑
(这取决于你!)。只能放东西
它以某种方式出现在屏幕上。如果你这样做
GameObject.create() 而不是 new()
Chingu 会将对象保存在
自动的“game_object”列表
更新/抽奖。

Chingu::BasicGameObject

new() 与 create() 的行为
GameObject 来自 BasicGameObject。

所以我们需要解决这个问题。然而......

现在 Player.draw 每一帧都被正确调用
通过Chingu,我们发现了一个新问题:
调用draw_rect不起作用!这就是 Ruby 告诉我的:

in draw_rect': undefined methodx' 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

  # Draws an unfilled rect in given color
  #
  def draw_rect(rect, color, zorder)
    $window.draw_line(rect.x, rect.y, color, rect.right, rect.y, color, zorder)
    $window.draw_line(rect.right, rect.y, color, rect.right, rect.bottom, color, zorder)
    $window.draw_line(rect.right, rect.bottom, color, rect.x, rect.bottom, color, zorder)
    $window.draw_line(rect.x, rect.bottom, color, rect.x, rect.y, color, zorder)
  end

啊,现在说得通了。 draw_rect 期望传递一个 Rectangle 对象,而不是
一堆坐标。这是:(

http://rdoc.info/projects/ippa/chingu)

     Chingu::Rect

     Constructor Details

- (Rect) initialize(*argv)

Create a new Rect, attempting to extract its own information from the 
given arguments.

The arguments must fall into one of these cases:

- 4 integers +(x, y, w, h)+.
- 1 Rect or Array containing 4 integers +([x, y, w, h])+.
- 2 Arrays containing 2 integers each +([x,y], [w,h])+.
- 1 object with a +rect+ attribute which is a valid Rect object.

All rect core attributes (x,y,w,h) must be integers.

所以我们只需要先创建一个 Rect 对象,然后调用
以该 Rect 作为第一个参数的draw_rect。

好吧,我们就这么做吧。这是工作代码——

require 'rubygems'
require 'chingu'

class Game < Chingu::Window
  def initialize
    super
    puts "initializing player..."
    @player = Player.create
  end

end

class Player < Chingu::BasicGameObject
  def initialize(options = {})
    super
    @x = 100
    @y = 100
    @rect = Chingu::Rect.new(@x, @y, 10, 10)
    @c = Gosu::Color.new(0xffff0000)
  end

  def draw
    puts "inside draw"
    puts @x, @y
    $window.draw_rect(@rect, @c, 1)
  end
end

Game.new.show

现在运行它会在 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)

Chingu::GameObject

Use this for all your in game
objects. The player, the enemies, the
bullets, the powerups, the loot
laying around. It’s very reusable and
doesn’t contain any game-logic
(that’s up to you!). Only stuff to put
it on screen a certain way. If you do
GameObject.create() instead of new()
Chingu will keep save the object in
the “game_object”-list for automatic
updates/draws.

Chingu::BasicGameObject

The new() vs create() behavior of
GameObject comes from BasicGameObject.

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 methodx' 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)

  # Draws an unfilled rect in given color
  #
  def draw_rect(rect, color, zorder)
    $window.draw_line(rect.x, rect.y, color, rect.right, rect.y, color, zorder)
    $window.draw_line(rect.right, rect.y, color, rect.right, rect.bottom, color, zorder)
    $window.draw_line(rect.right, rect.bottom, color, rect.x, rect.bottom, color, zorder)
    $window.draw_line(rect.x, rect.bottom, color, rect.x, rect.y, color, zorder)
  end

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)

     Chingu::Rect

     Constructor Details

- (Rect) initialize(*argv)

Create a new Rect, attempting to extract its own information from the 
given arguments.

The arguments must fall into one of these cases:

- 4 integers +(x, y, w, h)+.
- 1 Rect or Array containing 4 integers +([x, y, w, h])+.
- 2 Arrays containing 2 integers each +([x,y], [w,h])+.
- 1 object with a +rect+ attribute which is a valid Rect object.

All rect core attributes (x,y,w,h) must be integers.

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

require 'rubygems'
require 'chingu'

class Game < Chingu::Window
  def initialize
    super
    puts "initializing player..."
    @player = Player.create
  end

end

class Player < Chingu::BasicGameObject
  def initialize(options = {})
    super
    @x = 100
    @y = 100
    @rect = Chingu::Rect.new(@x, @y, 10, 10)
    @c = Gosu::Color.new(0xffff0000)
  end

  def draw
    puts "inside draw"
    puts @x, @y
    $window.draw_rect(@rect, @c, 1)
  end
end

Game.new.show

Running it now shows a small red rectangle at 100,100.

Hope that helps.

c~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文