人気ブログランキング | 話題のタグを見る

8日目

ruby/sdlのevent=SDL::Event.pollに手を入れてみます。
rubygameと言うライブラリーを使ってみた。
eventの管理方法は良いが、使い方が複雑だった。
だから必要な部分だけ、いただいちゃいました。ごちそうさま。
簡単にしたと言っても直ぐにピンとこないと思います。
ここは、習うより慣れろ。だ!
Don't think,Feel!
@handle_list にHandleクラスをキー、Procをバリューにして入れと置く。
この使い方だけ解れば、そうパターンが多いものじゃないから、大丈夫でしょ。
ジョイスティックをもってないので、ジョイスティックのイベントを作ってません。
ジョイスティックのイベントを使いたい人はHandle#initializeの中で@mathod = :whichにするようにプログラムを書いてやれば良いと思う。
後、SDL::Event.push(event)を使えばwiiリモコンやwii ボードに繋げそうな匂いがしますね。
今回のプログラムは矢印キーの右左で四角が移動。マウスの左クリックしながらの移動。


#!/usr/bin/env ruby

require 'rubygems'
require 'sdl'

class Handle
def initialize(evnt_class,event_sym_or_button=nil)
@event_class = evnt_class
@const = event_sym_or_button
if @const
if @event_class == SDL::Event::KeyDown || @event_class == SDL::Event::KeyUp
@method = :sym
elsif @event_class == SDL::Event::MouseButtonDown || @event_class == SDL::Event::MouseButtonUp
@method = :button
end
end
end
def match?(event)
if @const
event.kind_of?(@event_class) && @const == event.method(@method).call
else
event.kind_of?(@event_class)
end
end
end

class Cell
attr_accessor :x,:y,:w,:h,:bg_color
def initialize(surface)
@home = surface
@x,@y,@w,@h = 200,200,30,30
@surface = SDL::Surface.new(SDL::SWSURFACE,@w,@h,@home)
@bg_color = [255,255,100]
end
def update
@surface.fill_rect(0,0,@w,@h,@bg_color)
end
def blit
SDL::Surface.blit(@surface,0,0,@w,@h,@home,@x,@y)
end
def collide_point?(x,y)
@x < x && @x + @w > x && @y < y && @y + @h > y
end
end

class Phase
def initialize(screen)
@surface = screen
@w,@h = @surface.w,@surface.h
@cell = Cell.new(@surface)
@dx = 0
@handle_list = {
Handle.new(SDL::Event::KeyDown,SDL::Key::LEFT) => proc{@dx -= 1},
Handle.new(SDL::Event::KeyDown,SDL::Key::RIGHT) => proc{@dx += 1},
Handle.new(SDL::Event::KeyUp,SDL::Key::LEFT) => proc{@dx += 1},
Handle.new(SDL::Event::KeyUp,SDL::Key::RIGHT) => proc{@dx -= 1},
Handle.new(SDL::Event::MouseMotion) => proc{|event| self.mouse_move(event)},
Handle.new(SDL::Event::MouseButtonDown,SDL::Mouse::BUTTON_LEFT) => proc{|event| self.mouse_down(event)},
Handle.new(SDL::Event::MouseButtonUp,SDL::Mouse::BUTTON_LEFT) => proc{|event| self.mouse_up(event)},
Handle.new(SDL::Event::Quit) => proc{exit},
Handle.new(SDL::Event::KeyDown,SDL::Key::ESCAPE) => proc{exit}
}
@catch = false
end
def mouse_move(event)
if @cell.collide_point?(event.x,event.y)
if @catch
@cell.x = event.x + @mx
@cell.y = event.y + @my
end
end
end
def mouse_down(event)
if @cell.collide_point?(event.x,event.y)
@cell.bg_color = [255,0,0]
@catch = true
@mx,@my = @cell.x - event.x,@cell.y - event.y
end
end
def mouse_up(event)
if @cell.collide_point?(event.x,event.y)
@cell.bg_color = [255,255,100]
@catch = false
end
end
def handling(event)
@handle_list.each do |event_handle,method|
if event_handle.match?(event)
method.call(event)
end
end
end
def update
@surface.fill_rect(0,0,@surface.w,@surface.h,[50,50,50])
@cell.x += @dx * 8
@cell.x = 0 if @cell.x < 0
@cell.x = @w - @cell.w if @cell.x >= @w - @cell.w
@cell.update
@cell.blit
end
def run
loop do
while event=SDL::Event.poll
self.handling(event)
end
self.update
@surface.update_rect(0,0,0,0)
SDL.delay(20)
end
end
end

SDL.init( SDL::INIT_VIDEO )
screen = SDL::setVideoMode(400,400,16,SDL::SWSURFACE)
Phase.new(screen).run
by gaziya | 2010-08-29 09:35