Search code examples
ruby2dlibgosu

How can I jump on Gosu gem from Ruby?


I'm trying to recreate a simple game but I'm stuck at the moment that I'm trying to make jump my character( or player). I've been checking the example from gosu but it's not really clear for me. Can someone help me?

Here is my window code:

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

Then my player 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

Basically, the idea is when u press Space, then the jump method is invoked and here i should be able to recreate an animation to move the "y" position up ( for example 15 px) and then go down again. Just i have on my mind change the value of @y variable but i don't know how to do it with an animation and then come back at the original point.

Thanks!


Solution

  • Finally I got it!

    What I did is split the jump and an update method inside player:

    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
    

    And then on my Game 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
    

    So now I can press Space and it goes up, after finish the transition, it goes down