#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2016/12/09 02:35:48 +0900> # # Gosuの動作確認 # キー入力を試してみる。 # # Gusuはキーボード入力に加えてゲームパッド入力にも対応しているが、 # ゲームパッド入力に関しては注意点がある。 # # Windows環境では利用できるUSBゲームパッド種類に制約がある。 # DirectInputタイプ(旧規格・安価)には非対応で、 # XInputタイプ(新規格・高価)にのみ対応してるように見えた。 # また、左右のアナログスティック・十字ボタンのどれを押しても # 8方向のデジタル入力として扱われる模様。 # # 動作確認環境: # Windows10 x64 + Ruby 2.2.6 p396 mingw32 + Gosu 0.10.8 x86-mingw32 # # USB接続ゲームパッドの動作確認状況: # ELECOM JC-U3613M (XInput) : OK (ドライバのインストールが必要) # ELECOM JC-U3613M (DirectInput) : NG # ELECOM JC-U2410TWH (DirectInput) : NG # BUFFALO BSGP801GY (DirectInput) : NG require 'gosu' class MyWindow < Gosu::Window # コンストラクタ def initialize super 640, 480, false self.caption = "Input Test" @img = Gosu::Image.new("tmp_ufo.png", :tileable => true, :retro => true) @x = 320 @y = 240 @scale = 1.0 end # 更新処理 def update spd = 6 # 座標の変化量 # キー入力、またはゲームパッド入力に応じて座標を増減させる # 左キーが押された? if button_down?(Gosu::KbLeft) or button_down?(Gosu::GpLeft) @x -= spd end # 右キーが押された? if button_down?(Gosu::KbRight) or button_down?(Gosu::GpRight) @x += spd end # 上キーが押された? if button_down?(Gosu::KbUp) or button_down?(Gosu::GpUp) @y -= spd end # 下キーが押された? if button_down?(Gosu::KbDown) or button_down?(Gosu::GpDown) @y += spd end # Zキー or ボタン1 が押された? if button_down?(Gosu::KbZ) or button_down?(Gosu::GpButton0) @scale += (8.0 - @scale) * 0.1 else @scale += (1.0 - @scale) * 0.3 end end # 描画処理 def draw x = @x - (@img.width / 2) * @scale y = @y - (@img.height / 2) * @scale @img.draw(x, y, 0, @scale, @scale) end # キーボードチェック def button_down(id) # ESCキーが押されたらウインドウを閉じて終了する # Gosu::Window.close() を呼ぶとウインドウが閉じる close if id == Gosu::KbEscape end end window = MyWindow.new window.show