#!##!# MyGame.rb#!## Application shizrequire 'Application.rb'# Let's dance... put on your red shoes and dance the bluesapp = Application.new()app.run()#!##!# Application.rb#!## Ruby Librariesrequire 'rubygame'# My shizrequire 'Game.rb'# This class is our main app class, and controls basically everythingclass Application # Constructor def initialize() # Start Rubygame Rubygame.init() # Initialize some variables @exit = false @bpp = 16 # Create the screen Rubygame::GL.set_attrib(Rubygame::GL::RED_SIZE, 5) Rubygame::GL.set_attrib(Rubygame::GL::GREEN_SIZE, 5) Rubygame::GL.set_attrib(Rubygame::GL::BLUE_SIZE, 5) Rubygame::GL.set_attrib(Rubygame::GL::DEPTH_SIZE, 16) Rubygame::GL.set_attrib(Rubygame::GL::DOUBLEBUFFER, 1) @screen = Rubygame::Screen.new([640, 480], @bpp, [Rubygame::OPENGL]) @screen.title = 'MyGame' end # This function runs our application def run() game = Game.new() game.run() endend#!##!# Game.rb#!## Ruby Librariesrequire 'rubygame'require 'gl'# My Ssizrequire 'Timer.rb'# This class is an instance of our gameclass Game # This is our constructor (hurr!) def initialize() @exit = false # This is what we use to know if we're still running the game loop @events = nil # Our event queue #@timer = nil # Our logic timer # Creating our event queue @events = Rubygame::EventQueue.new() @events.autofetch = true # Fire up the timer @timer = Timer.new() end # This is our game loop :) def run() while(!@exit) @timer.newFrame() input() logic() output() @timer.sync() end end # Input function for our game loop def input() # Iterate through our event queue @events.each do |event| case event when Rubygame::QuitEvent @exit = true end end end # Logic function for our game loop def logic() end # Output function for our game loop def output() # Swap the GL buffers, yo Rubygame::GL.swap_buffers() endend#!##!# Timer.rb#!#require 'rubygame'# This is my own little timer class. I like it a lot.class Timer # Constructor def initialize(fps = 100) @clock = Rubygame::Clock.new() @lastTick = @clock.lifetime() @framesPerSecond = fps end # Tells the timer that we're on a new frame now def newFrame() @lastTick = @clock.lifetime() end # Syncs the program to our desired FPS def sync() while((@clock.lifetime() - @lastTick) < (1000 / @framesPerSecond)) Rubygame::Clock.delay(1) end endend