[英]Ruby/Gosu getting a bouncing image
我正在阅读“用红宝石学习游戏编程”一书,其中一个练习是用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
我认为它与红宝石如何使用图像的右上角作为图像的坐标有关,但我想
@blueheart.draw(@x - @width/2, @y - @height/2, 1)
应该解决这个问题,我怎样才能让它像我想要的那样工作? 提前致谢
问题来自于我创建自己的精灵而没有意识到高度和宽度值会有所不同。
将代码更改为@width = @ blueheart.width给了我一个崩溃,但我只是将值更改为正确的宽度和高度并修复了问题。 值@width = 50和@height = 43指的是书中不同的精灵大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.