2017-01-28 5 views
0

나는 간단한 게임을 다시 만들려고 노력하고 있지만, 나는 캐릭터 (또는 플레이어)를 뛰어 넘기 위해 노력하고있다. 나는 고수 (고수)의 예를보고 있었지만 나에게는 분명하지 않다. 누군가 나를 도울 수 있습니까? 여기 Ruby에서 Gosu gem으로 어떻게 이동할 수 있습니까?

내 창 코드 :

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 

기본적으로 생각이되며, 여기에 내가해야 애니메이션을 재현하여 "y"위치를 위로 이동시킨 다음 (예 : 15 픽셀) 다시 움직일 수 있습니다. 그냥 내 마음에 @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 

를 내가 누를 수 있습니다 나는 플레이어 내부의 점프 및 업데이트 방법을 분할 했는가

공간과 그것이 올라간다, 전환을 마친 후, 내려 간다