O'Reilly logo

Learn Game Programming with Ruby by Mark Sobkowicz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Add the Hammer

To whack the ruby, we use a hammer. You draw the hammer image in the window and have it follow the player’s mouse. The player tries to track the motion of the ruby by moving the hammer. Then, when the ruby becomes visible, bam! The player clicks the mouse, and you detect whether the position of the mouse click is close enough to the position of the ruby for a hit.

Drawing the hammer is similar to drawing the ruby. In the initialize method, you make an instance variable for the hammer image and load the image file.

WhackARuby/WhackARuby_1/whack_a_ruby.rb
 
def​ initialize
 
super​(800, 600)
 
self.caption = ​'Whack the Ruby!'
 
@image = Gosu::Image.new(​'ruby.png'​)
 
@x = 200
 
@y = 200
 
@width = 50
 
@height = 43
 
@velocity_x ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required