Ruby Gosu 如何在点击时开始新的时间
Ruby Gosu how to start new time on click
我正在尝试通过单击将图像从屏幕的一侧简单地移动到另一侧。但我不太清楚如何处理时间。基本上我需要在 Gosu::KbReturn 之后开始移动球。
如有任何帮助,我们将不胜感激
require 'gosu'
def media_path(file)
File.join(File.dirname(File.dirname(
__FILE__)), 'media', file)
end
class Game < Gosu::Window
def initialize
@width = 800
@height = 600
super @width,@height,false
@background = Gosu::Image.new(
self, media_path('background.png'), false)
@ball = Gosu::Image.new(
self, media_path('ball.png'), false)
@time = Gosu.milliseconds/1000
@x = 500
@y = 500
@buttons_down = 0
@text = Gosu::Font.new(self, Gosu::default_font_name, 20)
end
def update
@time = Gosu.milliseconds/1000
end
def draw
@background.draw(0,0,0)
@ball.draw(@x,@y,0)
@text.draw(@time, 450, 10, 1, 1.5, 1.5, Gosu::Color::RED)
end
def move
if ((Gosu.milliseconds/1000) % 2) < 100 then @x+=5 end
end
def button_down(id)
move if id == Gosu::KbReturn
close if id ==Gosu::KbEscape
@buttons_down += 1
end
def button_up(id)
@buttons_down -= 1
end
end
Game.new.show
首先,您将键盘事件处理程序放在了错误的位置。 update
方法仅作为 update_interval 期间的回调,您绝对应该将其放在 Gosu::Window.
的 button_down
实例方法中
其次,如果你调用移动方法来更新游戏对象的位置,那么循环执行它是没有意义的。您应该每次调用只更新 @x
一次。
第三,您在 move
方法中使用 @time
实例变量没有任何意义。如果你只需要在一段时间后才需要限制移动,你可以只检查计时器超过特定的增量,f.E。整数模(有一些公差):if (Gosu.milliseconds % @increment) < @epsilon then
.
更新: 更新 @x
按下 Enter 键后 10 秒
class Game < Gosu::Window
def initialize
…
@trigger = false # if trigger for delayed action was activated
@start_time = 0 # saved time when trigger was started
end
def update
…
if @trigger
if Gosu.milliseconds - @start_time < 10_000
@x += 1 # update x-position for 10 seconds
else
@trigger = false # disable trigger after timeout elapsed
end
end
end
def button_down(key)
case key
when Gosu::KbReturn
@start_time=Gosu.milliseconds # store current elapsed time
@trigger=true # enable trigger
when Gosu::KbEscape
…
end
end
end
已添加
def update
@x+=1 # in this method it increments every game second
end
def move
@x = 0
while @x > 0
do somemthing
end
end
我根本不明白update方法是不断循环的
我正在尝试通过单击将图像从屏幕的一侧简单地移动到另一侧。但我不太清楚如何处理时间。基本上我需要在 Gosu::KbReturn 之后开始移动球。
如有任何帮助,我们将不胜感激
require 'gosu'
def media_path(file)
File.join(File.dirname(File.dirname(
__FILE__)), 'media', file)
end
class Game < Gosu::Window
def initialize
@width = 800
@height = 600
super @width,@height,false
@background = Gosu::Image.new(
self, media_path('background.png'), false)
@ball = Gosu::Image.new(
self, media_path('ball.png'), false)
@time = Gosu.milliseconds/1000
@x = 500
@y = 500
@buttons_down = 0
@text = Gosu::Font.new(self, Gosu::default_font_name, 20)
end
def update
@time = Gosu.milliseconds/1000
end
def draw
@background.draw(0,0,0)
@ball.draw(@x,@y,0)
@text.draw(@time, 450, 10, 1, 1.5, 1.5, Gosu::Color::RED)
end
def move
if ((Gosu.milliseconds/1000) % 2) < 100 then @x+=5 end
end
def button_down(id)
move if id == Gosu::KbReturn
close if id ==Gosu::KbEscape
@buttons_down += 1
end
def button_up(id)
@buttons_down -= 1
end
end
Game.new.show
首先,您将键盘事件处理程序放在了错误的位置。 update
方法仅作为 update_interval 期间的回调,您绝对应该将其放在 Gosu::Window.
button_down
实例方法中
其次,如果你调用移动方法来更新游戏对象的位置,那么循环执行它是没有意义的。您应该每次调用只更新 @x
一次。
第三,您在 move
方法中使用 @time
实例变量没有任何意义。如果你只需要在一段时间后才需要限制移动,你可以只检查计时器超过特定的增量,f.E。整数模(有一些公差):if (Gosu.milliseconds % @increment) < @epsilon then
.
更新: 更新 @x
按下 Enter 键后 10 秒
class Game < Gosu::Window
def initialize
…
@trigger = false # if trigger for delayed action was activated
@start_time = 0 # saved time when trigger was started
end
def update
…
if @trigger
if Gosu.milliseconds - @start_time < 10_000
@x += 1 # update x-position for 10 seconds
else
@trigger = false # disable trigger after timeout elapsed
end
end
end
def button_down(key)
case key
when Gosu::KbReturn
@start_time=Gosu.milliseconds # store current elapsed time
@trigger=true # enable trigger
when Gosu::KbEscape
…
end
end
end
已添加
def update
@x+=1 # in this method it increments every game second
end
def move
@x = 0
while @x > 0
do somemthing
end
end
我根本不明白update方法是不断循环的