title: Increment an onscreen counter with a ruby/gosu window
body: <p>I am trying to make a very simple click counter and I am stuck on getting the click  counter to increment, both onscreen and in the code itself.</p>
  
  <pre><code>require 'rubygems'
  require 'gosu'
  
  module ZOrder
      BACKGROUND, MIDDLE, TOP = *0..2
  end
  
  WIN_WIDTH = 640
  WIN_HEIGHT = 400
  
  class GameWindow &lt; Gosu::Window
      def initialize
          super(WIN_WIDTH, WIN_HEIGHT, false)
          @background = Gosu::Color::WHITE
          @button_font = Gosu::Font.new(20)
          @info_font = Gosu::Font.new(15)
      end
  
      def needs_cursor?
          true
      end
  
      def draw
          Gosu.draw_rect(0, 0, WIN_WIDTH, WIN_HEIGHT, @background, ZOrder::BACKGROUND, mode=:default)
  
          Gosu.draw_rect(50, 50, 100, 50, Gosu::Color::GREEN, ZOrder::MIDDLE, mode=:default)
          Gosu.draw_rect(50, 150, 100, 50, Gosu::Color::GREEN, ZOrder::MIDDLE, mode=:default)
  
          @button_font.draw("Click me", 65, 65, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
          @button_font.draw("Reset", 75, 165, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
  
          @info_font.draw("Clicks: #{counter}", 160, 50, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
      end
  
      def counter
          i += 1
          i
      end
  
      def mouse_over_button?(mouse_x, mouse_y)
          if ((mouse_x &gt; 50 and mouse_x &lt; 150) and (mouse_y &gt; 50 and mouse_y &lt; 100))
            true
          else
            false
          end
      end
  
      def button_down(id)
  
          case id
          when Gosu::MsLeft
            if mouse_over_button?(mouse_x, mouse_y)
              counter
            else
              counter
            end
          end
      end
  
  
  end
  
  GameWindow.new.show
  </code></pre>
  
  <p>I am aware that my issue most likely lies in the 'counter' function and the 'button_down' function however I have been unable to figure out how to make it work. Any help would be appreciated, thanks.</p>