Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby侧滚游戏中的循环背景图像_Ruby_Libgosu - Fatal编程技术网

Ruby侧滚游戏中的循环背景图像

Ruby侧滚游戏中的循环背景图像,ruby,libgosu,Ruby,Libgosu,我想在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")

我想在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
但它只在开始时起作用(2-3次图像移动)。 我也试过这个

if @x1 == -SHIFT*@p
  @k += 2
end
而且它不起作用。

假设与分析 我假设
@x1
@y1
是最左侧背景原点相对于屏幕的偏移量。您还有
@player\u x
@player\u y
,它们表示玩家在屏幕上的位置。您的播放器水平放置在屏幕中央,在跳跃或下落时垂直移动,而您的背景仅水平滚动。此外,您的窗口大小为640x440,背景图像的宽度为700px,高度至少为440px

你的
update
步骤通过修改玩家的屏幕坐标来处理跳跃(但仅限于屏幕顶部)和简单的等速重力。它还提供了一个恒定的水平滚动,从世界空间中的水平摄像机坐标
@x1
中减去每帧3个单位

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

代码解决方案 我们不需要记住计数器值
@k
@p
,我们只需要按图像宽度进行模运算,以消除
@x1
中多余的值,并将其取到需要的位置
@k
@p
是无用的,所以请删除它们<代码>移位也可以删除

相反,我们需要做以下工作:

  • 计算最左侧图像的屏幕偏移
  • 确定是否需要绘制两个图像,而不是仅绘制一个图像
  • 绘制图像
我们的目标如下:

我们的新守则:

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
需要“gosu”
类GameWindow
这样做的目的是,它取
@x1
并用图像的(负)宽度对其进行模化。这意味着,它给出了两个数的整数除法的余数。当试图确保一个整数值在任一方向超过限制后“环绕”时,模是非常有用的;它有效地将其映射到0(包含)和给定数字(排除)之间。上图显示了这一结果


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

好的,这看起来太复杂了,通用变量名也没用。我要试着去分析你所拥有的并找出它。我的“简短回答”部分想建议你使用模运算符(你现在没有使用)来计算屏幕的偏移/环绕,只存储一个水平偏移,然后在你的
绘制
步骤中,计算真实图像偏移,并使用这些偏移量渲染两个背景。是否也进行垂直滚动?另外,是否需要两个
Gosu::Image
实例,或者你可以在同一张图上用不同的坐标调用
draw
两次吗?只能水平。从左到右。谢谢关于gosu::image实例。我不知道。我要修复它。这就是我要找的。谢谢!
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