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

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.