有没有一种方法可以使用Gosu无限期地使用光标滚动浏览地图

时间:2019-11-21 04:38:11

标签: ruby scroll graphics libgosu

我正在使用Gosu(Ruby 2.5.5版)来创建我的第一款游戏。我创建了一个地图,并且想要实现使用光标的滚动。以Gosu示例"Cptn Ruby“作为指导,我在某种程度上成功了。

这是我到目前为止所拥有的。

def update 
    # map.width is the number of background tiles in each row and map.height is the number of tiles in 
    # each column. Each tile is 14x14 pixels. 

    @camera_x = [[self.mouse_x - (WIDTH / 2), 0].max, @map.width * 14 - WIDTH].min
    @camera_y = [[self.mouse_y - (HEIGHT / 2), 0].max, @map.height * 14 - HEIGHT].min

end 

def draw

    @cursor.draw(self.mouse_x, self.mouse_y, 100, scale_x = 0.65, scale_y = 0.65)
    Gosu.translate(-@camera_x, -@camera_y) {@map.draw}

end

这确实会滚动,但只会滚动到最大点。一旦光标到达屏幕底部,camera_y的值将不会大于239(camera_x也会出现同样的问题)。我可以通过将值乘以2来增加滚动距离,如下所示:

@camera_y = [[(self.mouse_y - (HEIGHT / 2) * 2, 0].max, @map.height * 14 - HEIGHT].min

但是,当我使用此方法进一步滚动时,它仍然停止。我想在鼠标位于屏幕底部(或侧面)时连续滚动。自从gosu :: update每秒运行60次以来,为什么还没有这样做,我感到困惑。我本以为,如果光标在正确的位置,那么每次运行它都会添加到我的@camera_y和/或@camera_x变量中,但这没有发生。

我也尝试过:

if self.mouse_y > (HEIGHT * 0.67)  # if mouse is in lower 3rd of screen
    @camera_y += 10
end

这只会滚动一次滚动10个像素,而不是连续滚动。

我可以轻松地通过循环执行此操作,但是我发现Gosu中的循环更新或绘制会导致程序崩溃。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我想出了一种方法。

def initialize

        super WIDTH, HEIGHT, :fullscreen => false

        self.caption = "Ecosystem Beta"
        @map = Map.new("c:/users/12035/.atom/Ecosystem/eco_map.txt", :tileable => true)
        @cursor = Gosu::Image.new("c:/users/12035/.atom/media/cursor.png")

        @camera_x = 0
        @camera_y = 0

    end

   def update 

       if self.mouse_y > (HEIGHT * 0.67) # if mouse is in lower 3rd of screen
          @camera_y = [@camera_y += 10, 740].min # scrolls to a max of 740 pixels 

       elsif self.mouse_y < (HEIGHT * 0.33)
           @camera_y = [@camera_y -= 10, 0].max # will not scroll past the begginining of the map
       end    
   end