我怎样才能从 Ruby 跳到 Gosu gem?

How can I jump on Gosu gem from Ruby?

我正在尝试重新创建一个简单的游戏,但我在尝试让我的角色(或玩家)跳跃时卡住了。 我一直在检查 gosu 的例子,但对我来说并不是很清楚。 有人能帮我吗?

这是我的 window 代码:

class Game < Gosu::Window
  def initialize(background,player)
    super 640,480
    self.caption = "My First Game!"
    @background = background
    @player = player
    @player.warp(170,352)
    @x_back = @y_back = 0
    @allow_to_press = true
  end
# ...
def update
#...
   if Gosu.button_down? Gosu::KbSpace and @allow_to_press
      @allow_to_press = false
      @player.jump

    end



    end
end

然后我的播放器class:

class Player
  def initialize(image)
    @image = image
    @x = @y = @vel_x = @vel_y = @angle = 0.0
    @score = 0
  end
  def warp(x,y)
    @x,@y = x,y
  end

  def draw

    @image.draw_rot(@x,@y,1,@angle,0.5,0.5,0.3,0.3)
  end
  def jump
    #@y -= 15 # THIS IS NOT CORRECT
  end
end

基本上,这个想法是当你按下 Space 时,然后调用跳转方法,在这里我应该能够重新创建动画以将 "y" 位置向上移动(例如 15 px ) 然后再往下走。 只是我想改变@y变量的值,但我不知道如何用动画来做,然后回到原来的点。

谢谢!

终于明白了!

我所做的是在播放器中拆分跳转和更新方法:

class Player
  def initialize(image)
    @image = image
    @x = @y   = @angle = 0.0
    @score = @vel_y = @down_y = 0
    @allow = true
  end
  def warp(x,y)
    @x,@y = x,y
  end
  def update(jump_max)
      #Jump UP
      if @vel_y > 0
        @allow = false
        @vel_y.times do
          @y-=0.5
        end
        @vel_y-=1
      end

      #Jump DOWN
      if @vel_y < 0
        (@vel_y*-1).times do
          @y+=0.5
        end
        @vel_y+=1
      end
      check_jump
  end
  def draw
    @image.draw_rot(@x,@y,1,@angle,0.5,0.5,0.3,0.3)
  end
  def jump(original)
     @vel_y = original
     @down_y = original * -1

  end
  def check_jump
    if @vel_y == 0 and @allow == false
        @vel_y = @down_y
        @down_y = 0
        @allow = true
      end
    end
end

然后在我的游戏中 class

...

def initialize(background,player,enemy_picture)
    #size of the game
    super 640,480
    
    @original_y = 352
    @original_x = 170
    self.caption = "Le Wagon Game!"
    @background = background


       @player = player
        @player.warp(@original_x,@original_y)
        @enemy_anim = enemy_picture
        @enemy = Array.new
        #Scrolling effect
        @x_back = @y_back = 0
        @jump = true
        @jumpy = 25
      end

def update      
    if Gosu.button_down? Gosu::KbSpace and @jump
      @jump = false
       @player.jump(@jumpy)
    end
    @player.update(@jumpy)
end
def button_up(id)
    if id == Gosu::KbSpace
      @jump = true
    end
    super
  end

end

所以现在我可以按Space它会上升,完成转换后,它会下降