rubylibgosu

Display number of times click on screen using Gosu.button_down


I am working on a ruby program using Gosu. I want to display the number of mouse click times every time I click on a button on the screen. Please help me with this. Thank you so much

require 'rubygems'
require 'gosu'

module ZOrder
  BACKGROUND, MIDDLE, TOP = *0..2
end

WIN_WIDTH = 640
WIN_HEIGHT = 400
class DemoWindow < 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(10)
    @locs = [60,60]
  end

  def draw
 
    Gosu.draw_rect(0, 0, WIN_WIDTH, WIN_HEIGHT, @background, ZOrder::BACKGROUND, mode=:default)
    if area_clicked(mouse_x, mouse_y)
      draw_line(50, 50, Gosu::Color::BLACK, 150, 50, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
      draw_line(50, 50, Gosu::Color::BLACK, 50, 100, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
      draw_line(150, 50, Gosu::Color::BLACK, 150, 101, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
      draw_line(50, 100, Gosu::Color::BLACK, 150, 100, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
    end
    Gosu.draw_rect(50, 50, 100, 50, Gosu::Color::GREEN, ZOrder::MIDDLE, mode=:default)
    @button_font.draw_text("Click me", 60, 60, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
    @info_font.draw_text("mouse_x: #{mouse_x}", 0, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
    @info_font.draw_text("mouse_y: #{mouse_y}", 100, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK )

    if Gosu.button_down? Gosu::MsLeft
        index =0
        button = area_clicked(mouse_x, mouse_y)
        case button
        when 1
            index+=1
            @info_font.draw("Times Click: #{index}", 370, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
  end
end
end
  def needs_cursor?; true; end



  def area_clicked(mouse_x, mouse_y)
    if ((mouse_x > 50 and mouse_x < 150) and (mouse_y > 50 and mouse_y < 100))
      return 1
    else
    end
  end




  def button_down(id)
    if id == Gosu::KB_ESCAPE
        close
    else
        super
    end
end
end


DemoWindow.new.show

Below is the code I have added. When I click on the button, it only displays the number of times click is 1, however, I need it to display the number of times I clicked on.


Solution

  • This is because you're setting index back to zero every time the mouse is clicked (index =0). You should probably set your index to zero in your initialize function, and make it a class variable (add @ to the front).

    Then your initialize code would be:

    def initialize
      super(WIN_WIDTH, WIN_HEIGHT, false)
      @background = Gosu::Color::WHITE
      @button_font = Gosu::Font.new(20)
      @info_font = Gosu::Font.new(10)
      @locs = [60,60]
      @index = 0
    end
    

    and the code for the mouse click would be:

    if Gosu.button_down? Gosu::MsLeft
        button = area_clicked(mouse_x, mouse_y)
        case button
        when 1
            @index += 1
            @info_font.draw("Times Click: #{@index}", 370, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)