gpt4 book ai didi

ruby - 矢量 'Players' 而不是使用 Chingu 和 Gosu 的图像

转载 作者:数据小太阳 更新时间:2023-10-29 07:54:27 30 4
gpt4 key购买 nike

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

我做错了什么吗?

最佳答案

让我们弄清楚。

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

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

为什么?因为Chingu为大家提供了自动绘图和更新 它的游戏对象,但前提是你使用 GameObject.create 而不是 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.

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

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

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对象,然后调用draw_rect 以那个 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~

关于ruby - 矢量 'Players' 而不是使用 Chingu 和 Gosu 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214816/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com