Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Tomple

macrumors 6502a
Original poster
Dec 12, 2008
604
0
New York, New York
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
 
Last edited:
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.
 
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.

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.