部品をバウンドさせる 3

よくよく考えたら、
バウンドに関しては行列なんて、いらなかった。
ちょっと、テクニックに走りすぎたぜ。反省。

#!usr/bin/env ruby

require 'sdl'
require 'cairo'

class BackGround
def initialize(w, h)
@w, @h = w, h
end

def update(c)
c.set_source_color(:black)
c.rectangle(0, 0, @w, @h)
c.fill
end
end

class Ball
def initialize(w, h)
@x, @y = w / 2, h / 2
@r = 10
@erx = @r .. w - @r
@ery = @r .. h - @r
@vx, @vy = rand(5) + 2, rand(5) + 2
@vx *= -1 if rand(9) < 5
@vy *= -1 if rand(9) < 5
end

def update(c)
@vx *= -1 if !@erx.include?(@x + @vx)
@vy *= -1 if !@ery.include?(@y + @vy)
@x += @vx
@y += @vy
c.matrix = Cairo::Matrix.translate(@x, @y)
c.set_source_color(:orange)
c.circle(0, 0, @r)
c.fill
end
end

class Phase
def initialize(screen)
@screen = screen
@w, @h = @screen.w, @screen.h
@image = Cairo::ImageSurface.new(@w, @h)
@bg = BackGround.new(@w, @h)
@ball = Ball.new(@w, @h)
end

def update
context = Cairo::Context.new(@image)
@bg.update(context)
@ball.update(context)
surface = SDL::Surface.new_from(@image.data, @image.width, @image.height, 32, @image.stride, 0, 0, 0, 0)
@screen.put(surface, 0, 0)
end

def run
loop do
while e=SDL::Event.poll
exit if e.kind_of?(SDL::Event::Quit) || (e.kind_of?(SDL::Event::KeyDown) && e.sym == SDL::Key::ESCAPE)
end
self.update
@screen.flip
SDL.delay(20)
end
end
end

SDL.init(SDL::INIT_VIDEO)
@screen = SDL::Screen.open(350, 250, SDL::Screen.info.bpp, SDL::SWSURFACE)
Phase.new(@screen).run
[PR]
by gaziya | 2011-05-20 22:49