By Developers, For Developers

Historical errata for Learn Game Programming with Ruby

PDF PgPaper PgTypeDescriptionFixed onComments
47ERROR

Without require_relative ‘player’ in sector_five.rb, the game will not start but throw the error

sector_five.rb:10:in `initialize’: uninitialized constant SectorFive::Player (NameError)
\tfrom sector_five.rb:18:in `new’
\tfrom sector_five.rb:18:in `

’

Did I miss it whily reading?

2015-04-21
19SUGGEST

In general, Rubyists like to add spaces between parameters. It is better readable. So I suggest to change

super 800,600,false

to

super 800, 600, false

Or

image.draw(xwidth/2, y@height/2,1)

to

image.draw(x - width / 2, y - @height / 2, 1)

2015-05-09
48ERROR

sorry - last one should be page 42

2015-04-21
35SUGGEST

I think the more Ruby like syntax for

if not @playing

is

unless @playing

2015-05-09
34SUGGEST

Basically using the operators ‘or’, ‘and’, ‘not’ is absolutely correct, in my experience more often ‘||’, ‘&&’, ‘unless …’ is used. Furthermore the precedence is higher compared to the ‘words’. But this could also be seen as ‘code style’.

2015-05-09
51SUGGEST

@enemy = Enemy.new(self)

was not discussed yet at this point so I think it could lead to confusion

2015-04-21
50ERROR

You ask the reader to start the program. If I did not oversee anything, the ship will fly out of the window. I think this will decrease the joy of the reader when trying out the programmed code. I am not sure if this is by intention and if it will be discussed later. At least an info should be given here (maybe declaring this as a technical error is not entirely correct but I think it is more than a suggestion)

2015-04-21
51ERROR

ok - there is the explanation for flying out of the window. I still think that it is confusing and an info should be added on page 50 …

2015-04-21
52ERROR

what is vx and vy? I assume it should be velocity_x and velocity_y . If yes, it is a bug ;-)

2015-04-21
54ERROR

when asked to run the game at the end of the page it will fail because SectorFive does not know anything about Enemy. It has to be required with

require_relative ‘enemy’

2015-04-21
52OK

The method for hitting the top edge is missing. That means the space ship is flying out of the window. Adding

if y < radius
vy = 0 # remember that it should read velocity_y
y = radius
end

will fix this.

2015-07-21The text states that if you fly off the top you're destroyed by the mother ship.
32TYPO

“In Whack-A-Ruby, we start with zero point” -> should be “points”.

2015-04-20
65SUGGEST

Anywhere that we are iterating through the enemies, bullets, and @explosions arrays and moving them or drawing them, I think it would be better to use a more Rubyist convention like so:

enemies.each(&:draw) bullets.each(&:draw)
@explosions.each(&:draw)

enemies.each(&:move) bullets.each(&:move)

I did this in mine and it works perfectly. It uses less lines and is more compact.

2015-07-21
65TYPO

A more Rubyist way of writing conditionals that only have one line would be to put the `if` on the same line as the action that is fired after the conditional like so:

player.turn_left if button_down? Gosu::KbLeft player.turn_right if button_down? Gosu::KbRight
@player.accelerate if button_down? Gosu::KbUp

It works fine the way it is in the book, but since this is a book on Ruby, it may make sense to use the conventions commonly used in the community.

2015-05-09
61SUGGEST

“The each() method of the Array class goes through the array.”

Maybe us the word iterates instead of goes through?

2015-05-09
61SUGGEST

Rubyists love oneliner. So you may want to change

@enemies.each do |enemy|
enemy.move
end

to

@enemies.each { |enemy| enemy.move }

Same for the draw method.

2015-07-21In quite a few places, though not in the one you cited, the statement begins as a one command iteration but later in the project another line or two gets added. For consistency, and to address my less experienced audience I decided to stick with do ... end.
66TYPO

There is an extra space before @player.x

bullets.push Bullet.new(self, player.x, player.y, player.angle)

2015-05-10
72TYPO

the variable was called @image_number but is now written as

@image_index = 0

2015-05-10
75SUGGEST

more the Ruby way would be

@bullets.delete bullet unless bullet.onscreen?

instead of

@bullets.delete bullet if not bullet.onscreen?

2015-05-09
105TYPO

When calculating where to draw the number
x_text = x_center - @font.text_width("#{number}")/2
should be
x_text = x_center - @font.text_width(number)/2

2015-07-21Both ways work on my system. The documentation for text_width say that the first parameter is a string. So I send a string, even though it is more verbose.
32TYPO

“zero point” should be “zero points”

2015-05-09
90SUGGEST

For the code snippet:

if @enemies_appeared > MAX_ENEMIES
initialize_end(:count_reached)
end

It isn’t explicitly stated where this code should be placed. Presumably it goes in update_game, after new enemies are created, but it would help to explicitly say where this code should go.

2015-05-09
89TYPO

“enemy_count" should be "enemies_appeared”

2015-05-10
93TYPO

“ise” should be “is”

2015-05-09
83SUGGEST

you already discussed filling the draw_game method on page #81 so it’s duplicated content. I suggest to remove it on page #81

2015-05-09
86SUGGEST

It’s not clear where this code has to go to:

if @enemies_appeared > MAX_ENEMIES initialize_end(:count_reached)
end

2015-05-09
89SUGGEST

opengameart.com should be openclipart.org

2015-07-21The "Whack-A-Ruby" game uses art from openclipart.org \nSector Five uses art from opengameart.org \n \nSince its all CC0 stuff it could well be on both sites, but I listed the site where I found it. \n \nThey are both great sites for finding game art.
91ERROR

The reader should be aware, that he has to require the Credit class with

require_relative ‘credit’

2015-05-09
40TYPO

• Move a sprite by pressing the keys the keys.
should be
• Move a sprite by pressing the keys.

2015-05-09
28ERROR

So I’m not sure what i’ve done wrong. I’m building the whack a ruby game. When I miss the ruby everything goes red as expected but when I get a hit it throws and exception and closes.

This is my button down method (its the same as the one shown in the book )

def button_down(id)
if(id == Gosu::MsLeft)
if Gosu.distance(mouse_x, mouse_y, x, y) < 50 && visible >= 0 hit = 1
else
@hit = –1
end
end
end

and the exception is here

whack_a_ruby.rb:46:in `button_down’: undefined method `>=’ for nil:NilClass (NoMethodError)
\tfrom whack_a_ruby.rb:57:in `

’

I dont think I have any typos. Im running
macOS 10.10.3
ruby 2.0.0p481
gosu (0.9.2)

2015-08-01It looks like @visible is maybe not initialized in your initialize method. Make sure you have a line that creates and sets the @visible variable there. Make sure '@visible' is spelled the same in both places, too! \n \n
59SUGGEST

The footnote that reads “You can see all the methods of the Array class by looking in the Ruby documentation at link” should probably point to the Ruby 2.2.2 documentation (instead of the 2.0.0 docs) since that is the Ruby version you suggest to the user to use in the beginning of the book during the setup and installation. The link also has an unnecessary extra ‘/’ between ‘org’ and ‘core-2.0.0’

33TYPO

Example code near the top is missing a line from the previous example, specifically the font.draw() method call to display the score has been replaced with the method call to draw the time-left, rather than adding in a second font.draw() method call. Both lines appear as they should on the following page of the book.

12SUGGEST

For anyone using Gosu 0.10.0 or later, there is no longer a requirement to install libogg or libvorbis, i.e. only sdl2 needs to be installed using homebrew.

13SUGGEST

Regarding the note on running ruby programs from within Sublime Text, this can be a bit buggy if the computer contains several versions of ruby (e.g. through use of a ruby version manager). Particularly using RVM can cause the code to error if run inside Sublime Text. If you are using RVM, running the code through your terminal “$ruby filename.rb” will work perfectly i.e. you won’t get the errors you see if using Sublime.

46TYPO

The third sentence in the second paragraph incorrectly reads, “One method of the SectorFive class, accelerate()…”. This method actually belongs in the Player class (as per the Player class diagram and rest of the code in the book), i.e. “SectorFive” needs to be replaced with “Player” in this sentence.

72TYPO

In the Explosion class diagram, one of the instance variables in incorrectly written as “image_number". In the code and descriptive paragraphs the same variable is called "image_index”.

131+ERROR

On the enlarged boulder image, one of the vertices marked does not match the x and y co-ordinates provided for it. The affected vertex is the top left most one, labelled as (–13,–6) when it should really be (–13, –10) as per the book image and the sprite available from this website. The rest of the code in the book incorrectly follows this misplaced vertex, which means that the physics engine is viewing the boulder as having a cut-off flat side in the top-left corner.

65TYPO

The second paragraph says:
“… the Bullet class uses the Gosu.offet_x() …”
It should be:
“… the Bullet class uses the Gosu.offset_x() …”

106TYPO

The diagram with the large ‘3’ text has incorrect code written next to it to determine the top left corner x value, where it says “text+width/2” it should be “text.width/2”.

128SUGGEST

The code segment on this page is missing the marker on the line “attr_reader :space” to say that this is a new piece of code. Also, this same line of code is missing from the provided source code on your website.

142/3ERROR

There are 8 sprites in the Chip sprite sheet. The last paragraph on page 142 says to rotate through 7 images, and the draw method of the Chip class on the following page also uses %7 twice. All of these should be changed to 8 to use the last sprite from the Chip spritesheet.

146TYPO

Within the jump method definition, the second nested conditional which checks the @action instance variable is checking equality to an incorrect symbol name. Where it says “:left” it should be “:run_left” in order to be consistent with the rest of the code.

154ERROR

The new lines of code make a call to the x method of the Chip class “player.x". This requires that the Chip class makes the x method available e.g. by defining the x method "def x; body.p.x; end”, or making the body accessible from outside the class and calling “@player.body.p.x”. Whilst the final source code from your website contains the x method definition, it is not shown anywhere in the book text up to this point, so anyone following along would not have this code, and so this would cause an undefined method error.

161-9TYPO

The markup of which lines of code are new gets a bit messed up for 10 pages in the escape2.rb code snippets:

- p161: “require_relative ‘camera’” should be marked as new

- p162: “#END HIGHLIGHT” appears as text instead of markup
: lines beginning with “sign" and "font” are not new so should have marks removed

- p163: The only new line is “draw_credits” the rest should be unmarked
- p168/9: There are alot of redundant markings (i.e. not new code), only “quake_time" and "quake_sound” are new
: “#END_HIGHLIGHT” incorrectly appears again

170TYPO

The paragraph beginning with, “Finally, you write”, contains the sentence, “Note that you read just the camera…” which I believe should be “readjust the camera” i.e. a space has crept into the middle of the word.

145TYPO

On pages 145 the book describes the #check_footing method. The book never tells the reader to implement the #check_footing method within the Escape#update method.

Users following along with the book and testing the game on pg 146, as instructed, will notice that Chip does not “run and jump” as the book suggests Chip should at this point. This is because the book never tells the user to add the following line to Escape#update:

\t player.check_footing(platforms + @boulders)

11SUGGEST

The sentence “Who wants to give screens full of painfully handwritten code?”, while perfectly fine English, doesn’t seem to go with the previous sentences in the paragraph. I think a word was deleted, as in: “Who wants to give [up] screens full of painfully handwritten code?” This might be even better: “Who wants to just give up screens full of painfully handwritten code?” (Note to PragProg’s Webmaster: Does this box support Markdown? If not, why not?)

125SUGGEST

Each time the book says to “test”, it would be helpful if there was a version of the game’s code at that particular moment for the reader to reference.

This might be overkill for the first three games, But it would be very helpful for the Escape game where I ran into the most difficulty. There are so many code changes between and around Escape_1 and Escape_2, that it is not easy for the user to reference those versions because the reader may not have coded his/her way to that part of the book yet.

65ERROR

I don’t think running the project (ie, “ruby sector_five.rb”) will show any bullet.rb errors until after the next section, when you require it in the sector_five.rb file.

Categories: