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?
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 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)
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~