我想在 Ruby gosu 侧滚动游戏中循环背景图像。我的计数器有问题,@k
用于@p
翻译背景图像和重复的背景图像。我想不出一个好的方法来加上它们。这里是代码使其更清晰。
require 'gosu'
class GameWindow < Gosu::Window
attr_accessor :x, :y
SHIFT = 700
def initialize
super 640,440
@background = Gosu::Image.new("./images/bg.png")
@player = Gosu::Image.new("./images/000.png")
@x1, @y1 = 0, 0
@player_x, @player_y = 50, 50
@k = 0 #counter
@p = 1 #counter
end
def update
@x1 -= 3
@player_y += 1 if @player_y <= 375
@player_y -= 3 if button_down?(Gosu::KbSpace) and @player_y >= 0
@coordinates = Gosu::Image.from_text(
self, "#{@x1},#{@k}", Gosu.default_font_name, 30)
#here should be the code for @k and @p
end
def draw
@background.draw(@x1 + @k*SHIFT, @y1, 0)
@background.draw(@x1 + @p*SHIFT, @y1, 0)
@player.draw(@player_x, @player_y, 0)
@coordinates.draw(0, 0, 1)
end
def button_down(id)
$window.close if id == Gosu::KbEscape
end
end
window = GameWindow.new
window.show
那么我如何加上计数器@k
和@p
.试过这个
if @x1 > -(SHIFT+5)*@p and @x1 < -SHIFT*@p #705 and 700
@k += 2
end
if @k > 0 and @x1 > -SHIFT*@k - 5 and @x1 < -SHIFT*@k - 3 #1405 and 1403
@p += 2
end
但它只在开始时有效(2-3 图像移位)。也试过这个
if @x1 == -SHIFT*@p
@k += 2
end
它没有用。