Results 1 to 5 of 5

Thread: Need Advice on Multiple Objects of the Same Class in Ruby

  1. #1
    Join Date
    Oct 2008
    Location
    Bayamon, Puerto Rico
    Beans
    61
    Distro
    Ubuntu 14.04 Trusty Tahr

    Need Advice on Multiple Objects of the Same Class in Ruby

    Hello everyone,

    Thanks in advance for any advice. I am currently trying to create a breakout clone with rubygame. One thing has me stumped and I am unsure of how to approach it. I need to create multiple bricks for a player to break. I have already create a class for an individual brick, but I was wondering if there was a way to generate a certain number of brick objects so that I wouldn't have to hardcode each individual brick in, and I could use it to give players a random number of bricks to break per level. Any advice is greatly appriciated

  2. #2
    Join Date
    Sep 2009
    Beans
    217

    Re: Need Advice on Multiple Objects of the Same Class in Ruby

    Depending on how complex your brick class is, couldn't you just make a random length array of bricks? (I assume they contain their coordinates or something, so you can then iterate over the array and make each brick have a random coordinate that isn't already in use.)

  3. #3
    Join Date
    Oct 2008
    Location
    Bayamon, Puerto Rico
    Beans
    61
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Need Advice on Multiple Objects of the Same Class in Ruby

    /facepalm I feel stupid now. Don't know why I didn't think of that, but thank you so much. I am gonna try that out here later.

  4. #4
    Join Date
    Oct 2008
    Location
    Bayamon, Puerto Rico
    Beans
    61
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Need Advice on Multiple Objects of the Same Class in Ruby

    Well, I tried it, but unfortunately for me it is not working. I have an array of unique block class objects. I just have no clue how to call their respective draw methods.

  5. #5
    Join Date
    Sep 2009
    Beans
    217

    Re: Need Advice on Multiple Objects of the Same Class in Ruby

    Quote Originally Posted by tdawgf View Post
    Well, I tried it, but unfortunately for me it is not working. I have an array of unique block class objects. I just have no clue how to call their respective draw methods.
    Assuming your block class has an init method that sets its location to a random coordinate that isn't already taken and a draw method that draws the block:

    Code:
    array.each do |x| 
        x.init(takenCoordinates)
        x.draw
    end

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •