使用 ruby/gosu window 增加屏幕计数器
Increment an onscreen counter with a ruby/gosu window
我正在尝试制作一个非常简单的点击计数器,但我坚持让点击计数器在屏幕上和代码本身中递增。
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' 函数上,但是我一直无法弄清楚如何让它工作。如有任何帮助,我们将不胜感激。
你差一点就拥有了!您需要添加 @counter = 0
进行初始化。然后将您的 def counter
更改为如下所示
def counter
@counter += 1
end
为了让您的重置按钮起作用,我们现在可以制作另一个名为 def reset
的程序,如下所示
def reset
@counter = 0
end
然后将您的 def mouse_over_button?(mouse_x, mouse_y)
更改为以下内容:
def mouse_over_button?(mouse_x, mouse_y)
if ((mouse_x > 50 and mouse_x < 150) and (mouse_y > 50 and mouse_y < 100))
return_value = 1
return return_value
elsif ((mouse_x > 50 and mouse_x < 150) and (mouse_y > 150 and mouse_y < 200))
return_value = 2
return return_value
end
end
最后将 def button_down(id)
更改为以下内容:
def button_down(id)
case id
when Gosu::MsLeft
if mouse_over_button?(mouse_x, mouse_y) == 1
counter
elsif mouse_over_button?(mouse_x, mouse_y) == 2
reset
end
end
end
所有正确的想法都在那里!干得好!如果您不确定为什么要这样做,请告诉我,我很乐意进一步解释!希望我有所帮助:)
我正在尝试制作一个非常简单的点击计数器,但我坚持让点击计数器在屏幕上和代码本身中递增。
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' 函数上,但是我一直无法弄清楚如何让它工作。如有任何帮助,我们将不胜感激。
你差一点就拥有了!您需要添加 @counter = 0
进行初始化。然后将您的 def counter
更改为如下所示
def counter
@counter += 1
end
为了让您的重置按钮起作用,我们现在可以制作另一个名为 def reset
的程序,如下所示
def reset
@counter = 0
end
然后将您的 def mouse_over_button?(mouse_x, mouse_y)
更改为以下内容:
def mouse_over_button?(mouse_x, mouse_y)
if ((mouse_x > 50 and mouse_x < 150) and (mouse_y > 50 and mouse_y < 100))
return_value = 1
return return_value
elsif ((mouse_x > 50 and mouse_x < 150) and (mouse_y > 150 and mouse_y < 200))
return_value = 2
return return_value
end
end
最后将 def button_down(id)
更改为以下内容:
def button_down(id)
case id
when Gosu::MsLeft
if mouse_over_button?(mouse_x, mouse_y) == 1
counter
elsif mouse_over_button?(mouse_x, mouse_y) == 2
reset
end
end
end
所有正确的想法都在那里!干得好!如果您不确定为什么要这样做,请告诉我,我很乐意进一步解释!希望我有所帮助:)