Adding the dodgeball

10 views
Skip to first unread message

PastorJeffJ

unread,
Mar 2, 2011, 10:09:21 PM3/2/11
to ruby4kids
Unfortunately I'm still struggling with this screencast.
Here is my_game.rb code

$: << File.expand_path(File.dirname(__FILE__))
require 'rubygems'
require 'gosu'
require 'player'
require 'ball'

class MyGame < Gosu::Window
def initialize
super(300, 400, false)
@player1 = Player.new(self)
@ball = Ball.new(self)
end

def update
if button_down? Gosu::Button::KbLeft
@player1.move_left
end
if button_down? Gosu::Button::KbRight
@player1.move_right
end
if button_down? Gosu::Button::KbUp
@player1.move_up
end
if button_down? Gosu::Button::KbDown
@player1.move_down
end
@ball.update
end


def draw
@player1.draw
@ball.draw
end

end

window = MyGame.new
window.show

Here is my ball.rb
class Ball

def initialize(game_window)
@game_window = game_window
@icon = Gosu::Image.new(@game_window, "images/ball.png", true)
@x = rand(@game_window.width)
@y = 0
end

def update
if @y > @game.window.height
@y = 0
@x = rand(@game_window.width)
else
@y = @y + 10
end
end

def draw
@icon.draw(@x, @y, 2)
end
end

when I try to execute my_game.rb
this is what I get
ball.rb:11:in 'update': undefined method 'window' for nil:NilClass
<NoMethodError>
from my_game.rb:27:in 'update'
from my_game.rb:39:in 'show'
from my_game.rb:30:in '<main>'

I think I have everything exactly as you do.

Please help

Johnny M.

unread,
Mar 2, 2011, 10:51:16 PM3/2/11
to ruby4kids
I'm not sure, but I think that at the code below ↓....

>         def update
>                 if @y > @game.window.height
>............................................................

.... I think you should make it "if @y >= @game_window.height

In your initialize method, you have the variable @game_window, but you
referenced it as @game.window. Also, it might help if you add the
equal sign where I did above.

PastorJeffJ

unread,
Mar 2, 2011, 10:59:32 PM3/2/11
to ruby4kids
Spot on Johnny thanks.
these simple things are killing me.

Jonathan McNeil

unread,
Mar 2, 2011, 11:08:52 PM3/2/11
to ruby...@googlegroups.com

Happens to everyone.
Reply all
Reply to author
Forward
0 new messages