Keep Score

Keeping track of the player’s score is one way to make Whack-A-Ruby a game. Players can try to beat their friends’ scores or their own previous best score. They can brag about their high score and post it on social media.

In Whack-A-Ruby, we start with zero points. Every time we whack the ruby, let’s add five points, and every time we miss, we’ll take away one point.

You draw the score on the screen using another Gosu class, Gosu::Font. To create an instance of Gosu::Font, you tell Gosu what size characters you want in the window. The size of the font is the height of the tallest character in pixels. In the initialize method, you create your @font instance variable, along with another variable to keep track of the score.

WhackARuby/WhackARuby_1/whack_a_ruby.rb ...

Get Learn Game Programming with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.