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

私は何か間違ったことをしていますか?

4

1 に答える 1

5

それを理解しましょう。

示されているコードは @x と @y を nil に設定して draw_rect を呼び出し、「nil:nilClass に対して未定義のメソッド '-'」例外をスローするため、これらは実際のコードの不完全なスニペットであると想定しています。なし)

書かれているように、 Player.draw が呼び出されることはないため、何も描画されていない空白のウィンドウが表示されていると思います。

なんで?Chingu は、GameObject.new の代わりに GameObject.create を使用する場合に限り、すべてのゲームオブジェクトに対して自動描画と更新を提供するためです。

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

チング::ゲームオブジェクト

ゲーム内のすべてのオブジェクトにこれを使用します。プレイヤー、敵、弾丸、パワーアップ、戦利品。これは非常に再利用可能で、ゲーム ロジックは含まれていません (それはあなた次第です!)。特定の方法で画面に表示するためのもののみ。new() の代わりに GameObject.create() を実行すると、Chingu は自動更新/描画のためにオブジェクトを「game_object」リストに保存し続けます。

ちんぐ::B​​asicGameObject

GameObject の new() と create() の動作は、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~

于 2010-07-29T06:14:10.667 に答える