Help me with Ruby please!

Discussion in 'Mac Programming' started by Tomple, Feb 28, 2011.

  1. Tomple, Feb 28, 2011
    Last edited: Feb 28, 2011

    Tomple macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #1
    I plan on posting many, MANY questions here so be prepared :)

    My first question is EDIT: more questions, scroll down please!

    Why wont the speed change when the score >= 500

    here is the code, there are a few other classes, tell me if you need them

    main.rb

    Code:
    require 'rubygems'
    require 'gosu'
    require 'player'
    require 'ballbad'
    
    class MyGame < Gosu::Window
      def initialize
        super(2000, 1000, false)
        @player = Player.new(self)
        @ballbad = 10.times.map {Ballbad.new(self)}
        @running = true
        @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
        @score = 0
        @highscore = @score
        @message = ""
        @messageend = ""
        @begin = ""
        stop_game
      end
      
      def update
        if @running
          @messageend = ""
          @message = ""
          @begin = ""
          @score = @score + 1
          @score.to_i
          if @score >= 500
            @message = "Speed Up!"
            @ballbad.speed1 = rand(100)
          end
          if @score >= 600
            @message = ""
          end
          if @player.py < 50
            @message = "Double Points!"
            @score = @score + 3
          end
          if button_down? Gosu::Button::KbLeft
            @player.move_left
          end
          if button_down? Gosu::Button::KbRight
            @player.move_right
          end
          if button_down? Gosu::Button::KbUp
            @player.move_up
          end
          if button_down? Gosu::Button::KbDown
            @player.move_down
          end
          
          @ballbad.each {|ballbad| ballbad.update}
    
          if @player.hit_by? @ballbad
            stop_game
              if @score > @highscore
                @message = "New Record!"
                @begin = ""
                @highscore = @score
              else
                 @messageend = "Press Escape to Restart"
                 @message = ""
                 @begin = ""
              end
          end
        else
          if button_down? Gosu::Button::KbEscape
            restart_game
          end
        end
      end
      
      def draw
        @player.draw
        @ballbad.each {|ballbad| ballbad.draw}
        @font.draw("Score: #{@score}",10,10,5)
        @font.draw("Highscore: #{@highscore}",10,30,5)
        @font.draw(@message,240,175,10)
        @font.draw(@messageend,200,175,10)
        @font.draw(@begin,200,175,10)
      end
      
      def stop_game
        @running = false
        @begin = "Press Escape to Start"
      end
      
      def restart_game
        @running = true
        @ballbad.each {|ballbad| ballbad.reset}
        @score = 0
        @player.rlocation
      end
    end
    
    window = MyGame.new
    window.show

    ballbad.rb

    Code:
    class Ballbad
      
      def initialize(game_window)
        @game_window = game_window
        @icon = Gosu::Image.new(@game_window, "images/ballbad.png", true)
        @speed = rand(15)
        reset
      end
    
      def update
        if @y > @game_window.height
          reset
        else
          @y = @y + @speed
        end
      end
      
      def draw
        @icon.draw(@x,@y,2)
      end
      
      def x
        @x
      end
      
      def y
        @y
      end
      
      def reset
        @y = - 80
        @x = rand(@game_window.width)
      end
      
      def speed1
        @speed = rand(100)
      end
      
    end
    Thank You!


    EDIT: Got It! @ballbad.each {|ballbad| ballbad.speed}

    Can somebody explain what {|texthere|} this does
    would this work @ballbad.each {|test| test.speed}

    EDIT: Q#2

    Can I do this?

    Code:
      def speedupdate
        @speed = rand()
      end
    and then specify the rand value somewhere else like

    Code:
    @ballbad.speedupdate = rand(100)
    EDIT: Q#3 already!

    How can I make the direction of the falling balls change from up and down to left and right

    GAH! EDIT: Q#4!

    Why can't I consolidate this and put all the levels code in a "Levels" class

    levels.rb

    Code:
    require 'main'
    
    class Levels
      
      def levelup
       if @main.score >= 500
          @main.message = "Speed Up!"
          @ballbad.each {|ballbad| ballbad.level1}
        end
        if @main.score >= 600
          @main.message = ""
        end
        if @main.score >= 1000
          @main.message = "Speed Up!"
          @ballbad.each {|ballbad| ballbad.level2}
        end
        if @main.score >= 1100
          @main.message = ""
        end
        if @main.score >= 1500
          @main.message = "Speed Up!"
          @ballbad.each {|ballbad| ballbad.level3}
        end
        if @main.score >= 1600
          @main.message = ""
        end
        if @main.score >= 2000
          @main.message = "Speed Up!"
          @ballbad.each {|ballbad| ballbad.level4}
        end
        if @main.score >= 2100
          @main.message = ""
        end
      end
      
    end  
    
    new main.rb

    Code:
    require 'rubygems'
    require 'gosu'
    require 'player'
    require 'ballbad'
    require 'levels'
    
    class MyGame < Gosu::Window
      def initialize
        super(2000, 1000, false)
        @player = Player.new(self)
        @ballbad = 10.times.map {Ballbad.new(self)}
        @running = true
        @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
        @score = 0
        @highscore = @score
        @message = ""
        @messageend = ""
        @begin = ""
        stop_game
      end
      
      def update
        if @running
          @messageend = ""
          @message = ""
          @begin = ""
          @score = @score + 1
          @score.to_i
          @levels.levelup
          if @player.py < 50
            @message = "Double Points!"
            @score = @score + 3
          end
          if button_down? Gosu::Button::KbLeft
            @player.move_left
          end
          if button_down? Gosu::Button::KbRight
            @player.move_right
          end
          if button_down? Gosu::Button::KbUp
            @player.move_up
          end
          if button_down? Gosu::Button::KbDown
            @player.move_down
          end
          
          @ballbad.each {|ballbad| ballbad.update}
    
          if @player.hit_by? @ballbad
            stop_game
              if @score > @highscore
                @message = "New Record!"
                @begin = ""
                @highscore = @score
              else
                 @messageend = "Press Escape to Restart"
                 @message = ""
                 @begin = ""
              end
          end
        else
          if button_down? Gosu::Button::KbEscape
            restart_game
          end
        end
      end
      
      def draw
        @player.draw
        @ballbad.each {|ballbad| ballbad.draw}
        @font.draw("Score: #{@score}",10,10,5)
        @font.draw("Highscore: #{@highscore}",10,30,5)
        @font.draw(@message,240,175,10)
        @font.draw(@messageend,200,175,10)
        @font.draw(@begin,200,175,10)
      end
      
      def stop_game
        @running = false
        @begin = "Press Escape to Start"
      end
      
      def restart_game
        @running = true
        @ballbad.each {|ballbad| ballbad.reset}
        @score = 0
        @player.rlocation
      end
    end
    
    window = MyGame.new
    window.show
    EDIT!: another quick question

    Can I set a min and a max for the rand() number
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Perhaps you might actually be better off finding a Gosu forum to ask your questions.

    Many of the helpful folks here won't know Ruby. Mac programming is usually done in Objective-C and Cocoa. Almost none would know Gosu.

    I knew enough Ruby and re-applied existing knowledge from another domain, to help you before. But I don't know Gosu, I just looked up the Gosu docs.

    Also may I advise that it's kind of poor form to chain questions together like this. It's better to post different questions in different threads, unless it's a follow-on question directly related to your original question. A follow-on question asking about a post in reply to your original post is a good example. Another question only related by the fact they're both about the same project is a bad example.
     
  3. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #3
    Yeah, I realized that it would not go well just stringing on these questions after like me 4th edit :p

    I think what I really need is just a tutor, someone who can answer all of my many questions

    Thanks jiminaus
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    As jiminaus said, you'll find few people steeped deep enough in gosu here. Try at gosu forums. http://www.libgosu.org/cgi-bin/mwf/forum_show.pl

    B
     
  5. Tomple thread starter macrumors 6502a

    Tomple

    Joined:
    Dec 12, 2008
    Location:
    New York, New York
    #5
    Signing up now, thanks to the both of you :D

    What I really want to learn is Objective-C or C++ but those are most likely far too complex if I can't even got down ruby
     

Share This Page