Skip to content

Instantly share code, notes, and snippets.

@whity-82
Created April 19, 2013 17:32
Show Gist options
  • Save whity-82/5421869 to your computer and use it in GitHub Desktop.
Save whity-82/5421869 to your computer and use it in GitHub Desktop.
Sample of keyboard event handling on Chingu
#coding:utf-8
require 'chingu'
class GameWindow < Chingu::Window
# 1行の高さ(=フォントサイズ)
LINE_HEIGHT =20
# ウィンドウの高さ(行数)
LINES_IN_WINDOW = 24
# 標準フォントサイズ
FONT_SIZE = 20
def initialize
super(640, LINE_HEIGHT * LINES_IN_WINDOW, false)
end
def setup
self.caption = "ゲーム画面サンプル"
# テキストの設定
@title = Chingu::Text.create(:text=>"サンプルゲームタイトル", :x=>0, :y=>0, :size=>(FONT_SIZE * 1.5).to_i, :color => Gosu::Color::RED)
@menu1_text = Chingu::Text.create(:text=>"1. ダンジョンに潜る", :x=>30, :y=> LINE_HEIGHT * 3, :size=>FONT_SIZE, :color => Gosu::Color::WHITE)
@menu2_text = Chingu::Text.create(:text=>"2. 宿屋に泊まる", :x=>30, :y=> LINE_HEIGHT * 4, :size=>FONT_SIZE, :color => Gosu::Color::WHITE)
@menu3_text = Chingu::Text.create(:text=>"3. 商店街に行く", :x=>30, :y=> LINE_HEIGHT * 5, :size=>FONT_SIZE, :color => Gosu::Color::WHITE)
@menu4_text = Chingu::Text.create(:text=>"4. お城に行く", :x=>30, :y=> LINE_HEIGHT * 6, :size=>FONT_SIZE, :color => Gosu::Color::WHITE)
@menu5_text = Chingu::Text.create(:text=>"5. 街はずれに行く", :x=>30, :y=> LINE_HEIGHT * 7, :size=>FONT_SIZE, :color => Gosu::Color::WHITE)
# 選択中メニュー
@menu_id = 1
# キー入力イベントの定義
self.input = {
:escape => :close,
:up => :menu_up,
:down => :menu_down
}
end
def update
end
def draw
# タイトル描画
@title.draw
# 選択されてるメニューの設定
@menu1_text.color = (@menu_id == 1) ? Gosu::Color::CYAN : Gosu::Color::WHITE
@menu2_text.color = (@menu_id == 2) ? Gosu::Color::CYAN : Gosu::Color::WHITE
@menu3_text.color = (@menu_id == 3) ? Gosu::Color::CYAN : Gosu::Color::WHITE
@menu4_text.color = (@menu_id == 4) ? Gosu::Color::CYAN : Gosu::Color::WHITE
@menu5_text.color = (@menu_id == 5) ? Gosu::Color::CYAN : Gosu::Color::WHITE
# メニュー描画
@menu1_text.draw
@menu2_text.draw
@menu3_text.draw
@menu4_text.draw
@menu5_text.draw
end
# メニューを上へ
def menu_up
menu_move(-1)
end
# メニューを下へ
def menu_down
menu_move(+1)
end
# メニュー移動
def menu_move(direction)
@menu_id += direction
@menu_id = 1 if @menu_id < 1
@menu_id = 5 if @menu_id > 5
end
end
window = GameWindow.new
window.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment