1日目

この夏に怒涛のように書いてしまったプログラム。
しばらくは、これを出していきます。
ruby/sdlを使って書きました。
インベーダーゲームの宇宙船もどきのBMPを出力する奴。

#!/usr/bin/env ruby

# http://www.kmc.gr.jp/‾ohai/rubysdl_ref.html#label-17

require 'rubygems'
require 'sdl'

module SpaceShip
def load_item
self.get_surface(50,50)
dot =[
[0,1,0,0,0,0,0,0,1,0],
[0,1,0,0,1,1,0,0,1,0],
[0,0,1,1,1,1,1,1,0,0],
[1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,1,0,1,0,1],
[1,0,1,0,1,1,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,1,1,1,1,1,1,0,0],
[0,1,1,0,0,0,0,1,1,0],
[1,0,0,0,0,0,0,0,0,1]
]
for i in 0..9
for j in 0..9
@surface.fill_rect(j * 5,i * 5,5,5,[255,255,0]) if dot[i][j] == 1
end
end
@surface.set_color_key(SDL::SRCCOLORKEY,@surface.get_pixel(0,0))
end
end

class Cell
attr_reader :surface
def initialize(surface)
@home = surface
end
def get_surface(w,h)
@w,@h = w,h
@x = (@home.w - @w) / 2
@y = (@home.h - @h) / 2
@surface = SDL::Surface.new(SDL::SWSURFACE,@w,@h,@home)
end
def blit
SDL.blit_surface(@surface,0,0,@w,@h,@home,@x,@y)
end
end

class Module2Bmp
def initialize
SDL.init( SDL::INIT_VIDEO )
@screen = SDL::setVideoMode(300,300,16,SDL::SWSURFACE)
@screen.fill_rect(0,0,@screen.w,@screen.h,[80,80,80])
end
def set_module(mod)
@cell = Cell.new(@screen)
@cell.extend(mod)
@cell.load_item
end
def item_save(filename="a.bmp")
@cell.surface.save_bmp(filename)
end
def run
loop do
while event=SDL::Event.poll
case event
when SDL::Event::Quit
exit
when SDL::Event::KeyDown
exit if event.sym == SDL::Key::ESCAPE
end
end
@cell.blit
@screen.update_rect(0,0,0,0)
end
end
end

mod = Module2Bmp.new
mod.set_module(SpaceShip)
mod.item_save("space_ship.bmp")
mod.run
[PR]
by gaziya | 2010-08-22 10:49