我正在尝试制作一个非常简单的点击计数器,但我一直坚持让点击计数器增加,无论是在屏幕上还是在代码本身中。
require 'rubygems'
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP = *0..2
end
WIN_WIDTH = 640
WIN_HEIGHT = 400
class GameWindow < 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 > 50 and mouse_x < 150) and (mouse_y > 50 and mouse_y < 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
我知道我的问题很可能在于“counter”功能和“button_down”功能,但是我一直无法弄清楚如何使其工作。任何帮助将不胜感激,谢谢。