我正在尝试重新创建一个简单的游戏,但是我正在努力让我的角色(或玩家)跳跃。 我一直在检查来自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
基本上,这个想法是当你按空格键时,然后调用跳转方法,在这里我应该能够重新创建动画以向上移动“y”位置(例如15 px),然后再次向下移动。 我只是想改变@y变量的值,但我不知道如何用动画做到这一点,然后回到原点。
谢谢!
答案 0 :(得分: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
所以现在我可以按空格键,它会在完成转换后上升