我正在学习“用ruby学习游戏编程”这本书,其中一个练习是用gosu加载一个图像,并让它从屏幕的边缘弹出来。我跟着练习,图像在上角和左角弹得很好,但在弹出底部和右侧之前,会在屏幕边缘下沉一段时间。
require 'gosu'
class Window < Gosu::Window
def initialize
super(800, 600)
self.caption = 'First Game'
@blueheart = Gosu::Image.new('blueheart.png')
@x = 200
@y = 200
@width = 50
@height = 43
@velocity_x = 2
@velocity_y = 2
@direction = 1
end
def update
@x += @velocity_x
@y += @velocity_y
@velocity_x*= -1 if @x + @width /2 > 800 || @x - @width / 2 < 0
@velocity_y*= -1 if @y + @height /2 > 600 || @y - @height / 2 < 0
end
def draw
@blueheart.draw(@x - @width/2, @y - @height/2, 1)
end
end
window = Window.new
window.show 我认为这与ruby如何使用图像的右上角作为图像的坐标有关,但我认为
@blueheart.draw(@x - @width/2, @y - @height/2, 1)应该解决这个问题,我怎么才能让它像我想要的那样工作呢?提前感谢
发布于 2017-05-22 01:29:41
问题来自于我创建自己的精灵,而没有意识到高度和宽度值是不同的。
将代码更改为@width = @blueheart.width会导致崩溃,但我只是将值更改为适当的宽度和高度,并修复了该问题。值@width = 50和@height = 43指的是书中不同的精灵大小。
https://stackoverflow.com/questions/44090156
复制相似问题