我试图简单地将点击时的图像从屏幕的一侧移动到另一侧。但我无法完全弄清楚如何与时间合作。基本上我需要在 Gosu::KbReturn 之后开始移动球。
任何帮助将不胜感激
require 'gosu'
def media_path(file)
File.join(File.dirname(File.dirname(
__FILE__)), 'media', file)
end
class Game < Gosu::Window
def initialize
@width = 800
@height = 600
super @width,@height,false
@background = Gosu::Image.new(
self, media_path('background.png'), false)
@ball = Gosu::Image.new(
self, media_path('ball.png'), false)
@time = Gosu.milliseconds/1000
@x = 500
@y = 500
@buttons_down = 0
@text = Gosu::Font.new(self, Gosu::default_font_name, 20)
end
def update
@time = Gosu.milliseconds/1000
end
def draw
@background.draw(0,0,0)
@ball.draw(@x,@y,0)
@text.draw(@time, 450, 10, 1, 1.5, 1.5, Gosu::Color::RED)
end
def move
if ((Gosu.milliseconds/1000) % 2) < 100 then @x+=5 end
end
def button_down(id)
move if id == Gosu::KbReturn
close if id ==Gosu::KbEscape
@buttons_down += 1
end
def button_up(id)
@buttons_down -= 1
end
end
Game.new.show