2010-07-09 38 views
3

的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 

我做错了什么?

回答

5

让我们弄清楚。

我认为这些都是你的实际代码, 不完整的片段,因为代码所示调用与@x draw_rect和@y设为零, 抛出一个“未定义的方法以‘ - ’零:nilClass”例外,因为 你不能从零中减去任何东西。)

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

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

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

Chingu ::游戏对象

使用此为您的所有游戏 对象。玩家,敌人,子弹,通电,战利品 铺设。这是非常可重用的,并且 不包含任何游戏逻辑 (这取决于你!)。只有在屏幕上以某种方式放置 的东西。如果你做 GameObject.create()而不是new() Chingu会保存对象在 的“game_object” - 自动清单 更新/绘制。

Chingu :: BasicGameObject

新的()与创建() 游戏对象的行为来自BasicGameObject。

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

现在Player.draw是越来越正确调用每帧由Chingu ,我们已经找到了新的问题: 调用draw_rect不起作用!这是红宝石告诉我:draw_rect': undefined method X”

为[99,101,101,101,101,99,101,101]:阵列(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. 

所以我们只需要先创建一个矩形对象,然后调用 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〜