Ruby Gosu如何在单击时开始新时间

Ruby Gosu如何在单击时开始新时间,ruby,game-physics,libgosu,Ruby,Game Physics,Libgosu,我试着简单地将点击后的图像从屏幕的一侧移动到另一侧。但我不太明白如何利用时间。基本上,我需要在Gosu返回后开始移动球 任何帮助都会非常感激 require 'gosu' def media_path(file) File.join(File.dirname(File.dirname( __FILE__)), 'media', file) end class Game < Gosu::Window def initialize @widt

我试着简单地将点击后的图像从屏幕的一侧移动到另一侧。但我不太明白如何利用时间。基本上,我需要在Gosu返回后开始移动球

任何帮助都会非常感激

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
需要“gosu”
def介质路径(文件)
File.join(File.dirname(File.dirname(
__文件“,”媒体“,”文件)
结束
类游戏
首先,键盘事件处理程序位于错误的位置。
update
方法仅在update\u interval期间用作回调,您应该明确地将其放置在Gosu::Window的实例方法中

第二,若你们调用move方法来更新游戏对象的位置,那个么在循环中这样做是毫无意义的。每次通话只需更新一次
@x

第三,在
move
方法中使用
@time
实例变量没有任何意义。如果仅在经过一段时间后才需要限制移动,则只需检查计时器是否超过特定增量,例如使用整数模(具有一定公差):
If(Gosu.millides%@increment)<@epsilon then

更新:按Enter键后更新
@x
10秒

类游戏
已添加

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(Move method)后增加@x 10秒。根据您的建议更新了代码,但仍然不知道如何增加@x.@Richardlonesteen我已经完成了答案,希望对您有所帮助。我还建议通过屏幕尺寸检查
@x
方法中的
边界,因为它可能在10秒超时期间超出限制。请参见
Gosu.screen\u width
Gosu.screen\u height
。为什么每游戏一秒钟都会增加一次?
def update
    @x+=1 # in this method it increments every game second
end 

def move
   @x = 0
   while @x > 0
    do somemthing
   end
end