[Apologies if this appears as 2 posts -- my last post never seemed to
make it through]
I just setup NetBeans for Ruby on my Mac (quite like it so far). I'm
familiar to Ruby, getting back to it after a break. I will be using it
with my young kids, teaching them basics of programming to go with
their beginning algebra, physics, etc.
Over time I expect to use command line, simple GUIs, perhaps some
graphics/animation, eventually simple games.
Am seeking advice on the set up for this, what libraries / gems to
use, etc. I am not set on any particular ones, do not need awesome
power, just want something that will work without too much effort on
the Mac.
- Which version of Ruby to use? NetBeans shows a jRuby install +
1.8.6. Pros/cons of 1.9.1 for my usage?
- What tool / gems to use for GUI (Tk? Shoes? Other?
I am having a lot of success with Fox right now - I find it possible
to build quite a nice interface
with few lines of code, and there's foxGUIb if you want something to
help you build forms.
Its not seamless like Visual Studio dragging and dropping and then
double-clicking a control,
but you can still get a very nice interface working quite quickly once
you have a bit of experience.
- What tool / gems to use for games (Gosu? Chingu? other?)
Gosu is great. Every now and then I take a photo of one of my
daughter's great works of art
and then write a class to specify the behaviour of this new
"character". Then I add the character
to the existing collection and they all move around together in their own way.
My daughter loves to see her art animated and she always has requests when she
sees it: "can I make the doggie move?" etc.
I'll post a few examples (you'll need to add your own custom artwork):
---- basic "character.rb"
class Character
attr_accessor :x, :y
def initialize(image, x, y, x_scale = 1, y_scale = 1, col = 0xffffffff)
@image, @x, @y, @x_scale, @y_scale, @col = image, x, y, x_scale, y_scale, col
end
def draw
@image.draw(@x, @y, 0, @x_scale, @y_scale, @col)
end
def update
end
def width
@image.width
end
def height
@image.height
end
end
---- a "snail.rb"
require 'character'
class Snail < Character
def initialize(image, x, y)
super(image, x, y, 0.5, 0.5, 0xffffffff)
end
def update
@x += 1 if(rand(10) < 3)
@x -= 1 if(rand(10) < 1)
@x = 0 if @x < 0
@x = 1280 if @x > 1280
end
end
---- a "doggie.rb"
require 'character'
class Doggie < Character
def initialize(image, x, y, x_scale = 1, y_scale = 1, col = 0xffffffff)
super(image, x, y, x_scale, y_scale, col)
@s = 0
@base = 470
@xr = 0
@xl = 0
end
def update
@y = @base - 30 * Math::sin(@s)
@s += 0.1
@s = 0 if @s >= Math:
I
if @xr > 0
@x += 1
@xr -= 1
end
if @xl > 0
@x -= 1
@xl -= 1
end
end
def right
@xr = 50
end
def left
@xl = 50
end
end
---- some "grass.rb"
require 'character'
class Grass < Character
end
----- the "sun.rb"
require 'character'
class Sun < Character
end
----- main "garden.rb"
#!/usr/bin/ruby
require 'rubygems'
require 'gosu'
require 'snail'
require 'doggie'
require 'sun'
require 'grass'
$W = 1280
$H = 800
class MainWindow < Gosu::Window
def initialize
super($W, $H, true)
self.caption = 'Garden'
@characters =
grow_grass(0.6, 10, $H - 226, 0x7fbbbbbb) + #back
grow_grass(0.7, 9, $H - 194, 0x9fbbbbbb) +
grow_grass(0.8, 8, $H - 162, 0xbfbbbbbb) +
grow_grass(0.9, 7, $H - 130, 0xdfbbbbbb) +
grow_grass(1.0, 6, $H - 98, 0xffbbbbbb) + #front
[
Sun.new(Gosu::Image.new(self, 'media/sun-320.png'), $W - 200, 30, 0.5, 0.5),
@dog = Doggie.new(Gosu::Image.new(self, 'media/doggie-320.png'),
500, $H - 270, 0.5, 0.5),
Snail.new(Gosu::Image.new(self, 'media/snail-320.png'), 0, $H - 150)
]
end
def grow_grass(scale, div, y, col)
blades = Grass.new(Gosu::Image.new(self, 'media/grass-320.png'), 0,
0, scale, scale, col)
grass = []
div.times do |x|
b = blades.dup
b.x = (x * ($W / div)) - 20
b.y = y
grass << b
end
grass
end
def update
@characters.each {|c| c.update}
end
def draw
draw_quad(0, 0, 0xdd0080ff,
0, $H, 0x1100ff00,
$W, $H, 0xffffff00, #top right
$W, 0, 0x1100ff00) #bottom right
@characters.each {|c| c.draw}
end
def button_down(id)
close if id == Gosu::Button::KbEscape
@dog.right if id == Gosu::Button::KbRight
@dog.left if id == Gosu::Button::KbLeft
end
end
main = MainWindow.new
main.show