screen cast 2 making the player move

61 views
Skip to first unread message

anthony

unread,
Mar 3, 2011, 8:03:03 PM3/3/11
to ruby4kids
i have followed every single line of code until he does his first run
in the terminal where he shows the player when it moves. i try to run
it but i get this:

my_game.rb:25: syntax error, unexpected $end, expecting keyword_end
window.show
^

tried this several times but get the same thing.

here is my code:

my_game.rb


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

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

def update
if button_down? Gosu::Button::kbLeft
@player1.move_left
end

def draw
@player.draw

end

end

window = MyGame.new
window.show

player.rb code


class Player

def initialize(game_window)
@game_window = game_window
@icon = Gosu::Image.new(@game_window, "images/player1.png",
true)
@x = 50
@y = 50
end

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

def move_left
@x = @x - 10
end

end


if there is anything i am doing wrong please reply.
-thank you

Johnny M.

unread,
Mar 3, 2011, 8:09:04 PM3/3/11
to ruby4kids
At the code below ↓....
>     def update
>         if button_down? Gosu::Button::kbLeft
>                 @player1.move_left
>     end
>
.... You need to end your 'if' statement. It should read:
>     def update
>         if button_down? Gosu::Button::kbLeft
>                 @player1.move_left
> end
>     end
>


Keep goin' at it! It gets a lot easier as you keep learning.

anthony

unread,
Mar 3, 2011, 8:23:18 PM3/3/11
to ruby4kids
thank you for pointing out the mistake
-thank you

p.s. i got a response almost immediately.

Jonathan McNeil

unread,
Mar 3, 2011, 8:24:17 PM3/3/11
to ruby...@googlegroups.com
Yeah no prob. I was checking my email when your post popped up....

anthony

unread,
Mar 3, 2011, 8:29:23 PM3/3/11
to ruby4kids
oh darn >:(
now i get a other message saying:

my_game.rb:13:in 'update': undefined method 'kbLeft' for
Guso::Button::Module (No MethodError)
from my_game.rb:26:in 'show'
from my_game.rb:26:in '<main>'
help?

Alberto Morales

unread,
Mar 3, 2011, 8:36:04 PM3/3/11
to ruby...@googlegroups.com
Ruby is case sensitive.

KbLeft not kbLeft

Alberto.

anthony

unread,
Mar 4, 2011, 7:11:57 PM3/4/11
to ruby4kids
not AGAIN!!! ok..calm
now i get a message saying:
my_game.rb:14:in 'update': undefined 'move_left' for nil:NilClass
(NoMethodError)
from my_game.rb:26:in 'show'
from my_game.rb:26:in '<main>'
:-/

Alberto Morales

unread,
Mar 4, 2011, 7:30:10 PM3/4/11
to ruby...@googlegroups.com
Can you post more of the code around line 14. It seems you have mis-spelled the variable in front of .move_left.
Ruby is complaining because since the variable does not exist, it assumes it is nil, and nil does not have a method called move_left.

Alberto.

anthony

unread,
Mar 4, 2011, 7:45:51 PM3/4/11
to ruby4kids
sure

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

def update
if button_down? Gosu::Button::KbLeft
@player1.move_left
end
end

def draw
@player.draw

end

end

Alberto Morales

unread,
Mar 4, 2011, 7:48:17 PM3/4/11
to ruby...@googlegroups.com
You initialize @player

but in the update method you use @player1

Change @player1 to @player

Alberto.

anthony

unread,
Mar 4, 2011, 7:52:54 PM3/4/11
to ruby4kids
ae muy bueno (ah much better)
yet again another careless mistake :-/
thanks

anthony

unread,
Mar 4, 2011, 8:59:36 PM3/4/11
to ruby4kids
hello again lol
now i am having some other issues done wat the vid said and now i get
this:

my_game.rb:25:in 'update': in initialized constant Gosu::Button
(NameError)
from my_game.rb:37:in 'show'
from my_game.rb:37:in '<main>'

And thank you for all the attention
here is my latest code:

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

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

def update
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::Buttton::KbDown
@player.move_down
end
end

def draw
@player.draw
end

end

window = MyGame.new
window.show
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class Player

def initialize(game_window)
@game_window = game_window
@icon = Gosu::Image.new(@game_window, "images/player1.png",
true)
@x = 50
@y = 50
end

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

def move_left
if @x < 0
@x = 0
else
@x = @x - 10
end
end

def move_right
if @x > (@game_window.width - 75)
@x = @game_window.width - 75
else
@x = @x + 10
end
end

def move_up
if @y < 0
@y = 0
else
@y = @y - 10
end
end
def move_down
if @y > (@game_window.height - 75)
@y = @game_window.height - 75
else
@y = @y + 10
end
end

end

Ynes Alarcon Morales

unread,
Mar 4, 2011, 11:03:16 PM3/4/11
to ruby...@googlegroups.com
You have to fix Buttton in this part, it has 3 t 
-----------------------------------

anthony

unread,
Mar 5, 2011, 10:41:18 AM3/5/11
to ruby4kids
wait don't go there's more!
now i get this message:

<internal:lib/ruygems/custom_require>:29:in 'require': C:/User/Anthony/
Desktop/my game/ball.rb:1: class/module name must be CONSTANT
(SyntaxError)
from <internal:lib/rubygems/custom_require>:29:in
'require'
from my_game.rb:5:in '<main>'

On Mar 4, 11:03 pm, Ynes Alarcon Morales <ynesalar...@gmail.com>
wrote:

anthony

unread,
Mar 5, 2011, 10:43:44 AM3/5/11
to ruby4kids
oops heres mi code

my_game.rb:

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

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

def update
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

@ball.update
end

def draw
@player.draw
@ball.draw
end

end

window = MyGame.new
window.show
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
player.rb:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 updtae
@y = @y + 1
end

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

end

and again thank u

NeutralAngel

unread,
Mar 5, 2011, 11:15:17 AM3/5/11
to ruby4kids
In Ruby, class names begin with a capital letter.

Change class ball to class Ball

Also, I noticed a typo when you are instantiating the ball (creating a
new one): Change @ball = Ball.nerw(self) to @ball = Ball.new(self)

anthony

unread,
Mar 9, 2011, 4:06:51 PM3/9/11
to ruby4kids
ok! im on the 2nd lesson...finally X-D

k back to topic i get this mesage:

my_game.rb:34:in 'update': undefined method 'update' for #<Ball:
0x283aa78) (NoMethodError)
from my_game.rb:54:in 'show'
from my_game.rb:54:in '<main>'
i know it has something to do with my update method maybe because
there is no else in the if statement? anyways heres my code:

my_game.rb:

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


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

def update
if @running
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

@ball.update

if @player.hit_by? @ball
stop_game!
end
end

end

def draw
@player.draw
@ball.draw
end

def stop_game!
@running = false
end
end

window = MyGame.new
window.show

player.rb:

class Player

def initialize(game_window)
@game_window = game_window
@icon = Gosu::Image.new(@game_window, "images/player1.png",
true)
@x = 50
@y = 150
def hit_by?(ball)
Gosu::distance(@x,@y,ball.x,ball.y) <50
end

end

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 updtae
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

def x
@x
end

def y
@x
end

end

soooo thats all the code that i have and thank you all the ppl out
there for helping me out :-)

Ynes Alarcon Morales

unread,
Mar 9, 2011, 4:22:56 PM3/9/11
to ruby...@googlegroups.com
Hi,

Change in ball.rb  "updtae" to "update"


class Ball
......
............................

def update
if @y >@game_window.height
@y = 0
@x = rand(@game_window.width)
else
@y = @y + 10
end
end
...................
......
end
Reply all
Reply to author
Forward
0 new messages