Ruby 横向卷轴游戏中的循环背景图片

Looping background image in Ruby side-scrolling game

我想在 Ruby gosu 侧滚动中循环背景图像 game.I 用于翻译背景的计数器 @k@p 有问题图像和重复的背景 image.I 想不出一个好的方法来加上 them.Here 的代码使它更清晰。

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

它没有用。

假设与分析

我假设 @x1@y1 是最左侧背景原点相对于屏幕的偏移量。您还有 @player_x@player_y,它们表示玩家在屏幕上的位置。您的播放器水平放置在屏幕中央,跳跃或下落时垂直移动,而您的背景仅水平滚动。此外,您的 window 尺寸为 640x440,背景图片宽度为 700 像素,高度至少为 440 像素。

您的 update 步骤通过修改玩家的屏幕坐标来处理诸如跳跃(但仅到达屏幕顶部)和简单的恒速重力之类的事情。它还通过从 @x1 中减去每帧 3 个单位来提供恒定的水平滚动,世界中的水平相机坐标 - space.

您的 draw 步骤然后获取背景图像并绘制其中两个,通过相机偏移 @x1 加上图像的偏移来偏移它们。 然而,这种转变并不重要,因为我们可以相当简单地计算它,而且我们不必操纵额外的状态来记住哪个是哪个。

代码解决方案

我们不再记住计数器值 @k@p,而是仅对图像宽度取模以消除 @x1 中的多余部分并将其获取到我们需要的地方它是。 @k@p 没用,所以删除它们。 SHIFT也可以删除

相反,我们需要执行以下操作:

  • 计算最左边图像的屏幕偏移量
  • 确定是否需要绘制两张图像而不是只绘制一张
  • 绘制图像

我们的目标看起来像这样:

我们的新代码:

require 'gosu'

class GameWindow < Gosu::Window
  attr_accessor :x, :y # side note - are these used at all?

  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
  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}", Gosu.default_font_name, 30)
  end

  def draw
    # the important bits!
    @local_x = @x1 % -@background.width
    @background.draw(@local_x, @y1, 0)
    @background.draw(@local_x + @background.width, @y1, 0) if @local_x < (@background.width - self.width)

    @player.draw(@player_x, @player_y, 0)
    @coordinates.draw(0, 0, 1)
  end

  def button_down(id) # Side note: Does this work correctly?
    $window.close if id == Gosu::KbEscape
  end

end

window = GameWindow.new
window.show

它所做的是,它需要 @x1 模数 它与图像的(负)宽度。这意味着,它给出了两个数字的整数除法的余数。当在任一方向超过限制后尝试确保整数值 "wraps around" 时,模数非常有用;它有效地将其映射到 0(含)和给定数字(不含)之间。结果如上图所示。

请记住,此解决方案仅适用于您的情况;垂直滚动将需要更多代码,如果您的 window 变得比背景图像宽,这将很快中断。