Vector "players", not images using Chingu and Gosu

A 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, not images, how would I do it?

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?

+3
source share
1 answer

Explain it.

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 out the undefined '-' method to exclude nil: nilClass, because you cannot subtract anything from scratch.)

, ,   , , Player.draw .

? Chingu   GameObjects, GameObject.create  GameObject.new.

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

Chingu:: GameObject

. , , , , . ( !). , . GameObject.create() new() Chingu "game_object" -list /.

Chingu:: BasicGameObject

() vs create() GameObject BasicGameObject.

. ...

, Player.draw Chingu, : draw_rect ! , Ruby :

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

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.

, .

~

+5

Source: https://habr.com/ru/post/1753913/


All Articles