Play Again?
Let’s add a “Press the Space Bar to Play Again” message and set up the game to play again. You need to set the score back to 0 and give the player a new 100 seconds to play. You add the new message to the screen in the draw method when the game is over:
WhackARuby/WhackARuby_2/whack_a_ruby.rb | |
| def draw |
| if @visible > 0 |
| @image.draw(@x - @width / 2, @y - @height / 2, 1) |
| end |
| @hammer_image.draw(mouse_x - 40, mouse_y - 10, 1) |
| if @hit == 0 |
| c = Gosu::Color::NONE |
| elsif @hit == 1 |
| c = Gosu::Color::GREEN |
| elsif @hit == -1 |
| c = Gosu::Color::RED |
| end |
| draw_quad(0, 0, c, 800, 0, c, 800, 600, c, 0, 600, c) |
| @hit = 0 |
| @font.draw(@time_left.to_s, 20, 20, 2) |
| @font.draw(@score.to_s, 700, 20, 2) |
| unless @playing ... |
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.