gpt4 book ai didi

ruby - Gosu 中的居中文本

转载 作者:数据小太阳 更新时间:2023-10-29 07:33:44 24 4
gpt4 key购买 nike

我一直无法将 Gosu 库中的文本居中放置在屏幕的绝对中间。

require 'gosu'

class GameWindow < Gosu::Window
def initialize (width=800, height=600, fullscreen=false)
super
self.caption = 'Hello'
@message = Gosu::Image.from_text(
self, 'HELLO WORLD', Gosu.default_font_name, 45)
end

def draw
@message.draw(377.5,277.5,0)
end
end

window = GameWindow.new
window.show


我的第一个方法是获取屏幕的 height,用它减去文本 45 的高度,然后除以 2。现在这似乎在垂直对齐时起作用.

enter image description here

然而,横向是另一回事......似乎取文本的左上角并将其居中,这是我期望的,而不是文本的中间。

enter image description here

有人知道这个的公式吗?我尝试了一大堆东西,但只差一点。

最佳答案

class GameWindow < Gosu::Window
def initialize (width=800, height=600, fullscreen=false)
super
self.caption = 'Hello'
@message = Gosu::Image.from_text(
self, 'HELLO WORLD', Gosu.default_font_name, 45)
end

def draw
@message.draw(377.5,277.5,0)
end
end

您的@messageGosu::Image 的一个实例

据我所知,该类有一个方法可以让您将图像的旋转中心与指定点对齐,draw_rot

一旦找到框架的中心,使用 draw_rot 而不是 draw 应该对你有用。

关于ruby - Gosu 中的居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32169245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com