Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 971331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:56:52+00:00 2026-05-16T02:56:52+00:00

The Chingu example looks something like this: require ‘rubygems’ require ‘chingu’ class Game <

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:56:52+00:00Added an answer on May 16, 2026 at 2:56 am

    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)

    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.

    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)

      # 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
    

    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)

         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.
    

    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 —

    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
    

    Running it now shows a small red rectangle at 100,100.

    Hope that helps.

    c~

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is causing a very strange exception in Chingu, a game library for
I have a problem with the validation of this piece of XML: <?xml version=1.0
I have a json field in my database which is like jsonfield = {'username':'chingo','reputation':'5'}
I'm trying to save the current state of a game. I serialize the game_state
I wrote a simple program to understand how objective-c works. This program is the
In my small Ruby project using OpenGL via Gosu and Chingu I'm now seeing
I'm hoping to use Google Maps on my site. My addresses are stored in
I'm trying to build and run ruby code that a team member wrote for
I'm trying to implement the solar terms in my .emacs so my holidays will
My jquery submit handler is not called in Chrome. Here is the code: <input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.