Playing with ruby/sdl

  • 0
Pretty tired right now. Been playing around with ruby/sdl most of the day. I looked at some of the examples in '/usr/share/doc/libsdl-ruby1.8/examples'. They have no comments but most are simple enough to run and play with to understand whats going on. There is also a tetris clone that is 275 lines of code. I found www.kmc.gr.jp/~ohai/rubysdl_doc.en.html, the ruby/sdl reference to be useful for looking up a few things.

I had a try at moving an image (in this case a image called icon.bmp, use the one in examples) around with the left, right, up and down keys. Could probably change the IF statements into case statements.

#Writen by DnH500. DnH500.blogspot.com
#you need a smallish icon.bmp image

require 'sdl'

#make the main window
SDL.init( SDL::INIT_VIDEO )
screen = SDL::setVideoMode(600,480,16,SDL::SWSURFACE)
SDL::WM::setCaption('moving around','')

#load image
image = SDL::Surface.loadBMP("icon.bmp")
image.setColorKey( SDL::SRCCOLORKEY || SDL::RLEACCEL ,0)
$image = image.displayFormat

#starting image postion
@x=0
@y=0

#move function
def move

if SDL::Key.press?(SDL::Key::RIGHT)
@x = @x + 10 #moves right
end

if SDL::Key.press?(SDL::Key::DOWN)
@y = @y + 10 #moves down
end

if SDL::Key.press?(SDL::Key::LEFT)
@x = @x - 10 #move left
end

if SDL::Key.press?(SDL::Key::UP)
@y = @y - 10 #move right
end

end

#main loop
while true
#on event
while event = SDL::Event2.poll
#scan keyboard input
SDL::Key.scan
#draw background
screen.fillRect(0,0,600,480,0)

#goto move function
move

#draw bmp image
screen.put(image,@x,@y)
#update
screen.updateRect(0,0,0,0)
end
end


I was surprised how easy sdl is. This stuff makes more sense to me than darkbasic (a windows game development basic). Plus with it being on linux it's an added bonus since I have never played with any graphics programing on linux before.
Now if I could work out a way to create a map with arrays I could start a 2d RPG.

No comments:

Post a Comment