#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2016/12/11 15:49:32 +0900> # # Gosuの動作確認 # スプライト相当の処理をしてみる。 # # 動作確認環境: # Windows10 x64 + Ruby 2.2.6 p396 mingw32 + Gosu 0.10.8 x86-mingw32 require 'gosu' # スプライト相当のクラス class MySpr # コンストラクタ def initialize(x, y, vx, vy, img, scrw, scrh) @fpx = x @fpy = y @fpvx = vx @fpvy = vy @scrw = scrw @scrh = scrh @w = img.width @h = img.height @whf = @w / 2 @hhf = @h / 2 @image = img @x = (@fpx - @whf).to_i @y = (@fpy - @hhf).to_i end # 更新処理 def update @fpx += @fpvx @fpy += @fpvy @x = (@fpx - @whf).to_i @y = (@fpy - @hhf).to_i @fpvx *= -1 if @x + @w >= @scrw or @x <= 0 @fpvy *= -1 if @y + @h >= @scrh or @y <= 0 end # 描画処理 def draw @image.draw(@x, @y, 0) end end class MyWindow < Gosu::Window # コンストラクタ def initialize super 640, 480, false self.caption = "Sprite Test" scrw = self.width scrh = self.height # スプライト発生 pmax = 512 img = Gosu::Image.new("tmp_ufo.png", :tileable => true) x, y = 320, 240 @sprgroup = [] pmax.times do |i| rad = (i * 360 / pmax) * Math::PI / 180 dx = 3.0 * Math::cos(rad) dy = 3.0 * Math::sin(rad) @sprgroup.push(MySpr.new(x, y, dx, dy, img, scrw, scrh)) end @frame = 0 end # 更新処理 def update @sprgroup.each do |spr| spr.update end @frame += 1 end # 描画処理 def draw @sprgroup.each do |spr| spr.draw end end # キーボードチェック def button_down(id) if id == Gosu::KbEscape # ESCキーが押されたらウインドウを閉じて終了する # Gosu::Window.close() を呼ぶとウインドウが閉じる close end end end window = MyWindow.new window.show