2017-01-28 36 views
0

我正在尝试重新创建一个简单的游戏,但当我尝试跳转我的角色(或玩家)时,我被卡住了。 我一直在检查gosu的例子,但对我来说并不是很清楚。 有人可以帮我吗?如何从Ruby跳上Gosu宝石?

这里是我的窗口代码:

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 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 

基本上,这个想法是当u按空格键,然后跳转方法被调用,在这里,我要能够重新创建动画以将“y”位置向上移动(例如15px),然后再次下移。 只是我在我的脑海里改变了@y变量的值,但我不知道如何用动画做到这一点,然后回到原点。

谢谢!

回答

0

最后我明白了!

我所做的是分裂的内线球员跳跃和更新方法:

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 

,然后在我的游戏类

...

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 

所以,现在我可以按空间和它上升,完成过渡后,它下降