|
Raiden
Ruby is a great scripting language, especially when you want to make the complex simple. Nothing is more complex than game programming... and few things are more simple than the Gosu game development library. This is a modification of the default tutorial/demo that comes with the library. I used the Ruby version, which required some additional downloads to support... but once it was done it worked wonderfully. (note: the Ruby version seems to have been developed more for Windows, and although using the source code to compile on other platforms is possible, my friend had to rewrite the make file for his Debian system) The original demo created a window with a star field background, and allowed a ship to fly around picking up randomly generated jewels. Within a half hour, I had modified it to be more arcade like. The background scrolls down as if flying through space, the gems drop from the top to bottom and the ship moves from side to side. Scoring is based on the speed of the gem dropping and will count against you if missed. Nothing too exciting, but a good example of code on how animation, sound, movement, etc. is done in Gosu's Ruby library... and how easy it is to modify.
Source Code
require 'gosu'
module ZOrder Background, Stars, Player, UI = *0..3 end class Star attr_reader :x, :y, :kill, :speed attr_writer :kill def initialize(animation) @animation = animation @color = Gosu::Color.new(0xff000000) @color.red = rand(255 - 40) + 40 @color.green = rand(255 - 40) + 40 @color.blue = rand(255 - 40) + 40 @x = rand * 640 @y = 0 @speed = (1+(rand*6)).round @kill = false end def draw @y = (@y+@speed) % 480 img = @animation[Gosu::milliseconds / 100 % @animation.size]; img.draw(@x - img.width / 2.0, @y - img.height / 2.0, ZOrder::Stars, 1, 1, @color, :additive) end end class Player def initialize(window) @image = Gosu::Image.new(window, "media/Starfighter.bmp", false) @beep = Gosu::Sample.new(window, "media/Beep.wav") @x = @y = @vel_x = @vel_y = @angle = 0.0 end def warp(x, y) @x, @y = x, y end def turn_left @vel_x -= 1.5 end def turn_right @vel_x += 1.5 end def move if @vel_x.abs < 10 then @x += @vel_x else @x += 10*(@vel_x<=>0) end @x %= 640 @vel_x *= 0.95 end def collect_stars(stars) stars.reject! do |star| dist_x = @x - star.x dist_y = @y - star.y dist = Math.sqrt(dist_x * dist_x + dist_y * dist_y) if dist < 35 then yield star.speed*10 @beep.play true else if star.kill then yield -star.speed*10 true else false end end end end def draw @image.draw_rot(@x, @y, ZOrder::Player, @angle) end end class GameWindow < Gosu::Window def initialize super(640, 480, false, 20) self.caption = "Raiden v.0.1" @font = Gosu::Font.new(self, Gosu::default_font_name, 20) @background_image = Gosu::Image.new(self, "media/Space.png", true) @star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false) @player = Player.new(self) @player.warp(320, 400) @stars = Array.new @score = 0 @phase = 0 end def update @phase = (@phase+2) % 480 if button_down? Gosu::Button::KbLeft or button_down? Gosu::Button::GpLeft then @player.turn_left end if button_down? Gosu::Button::KbRight or button_down? Gosu::Button::GpRight then @player.turn_right end @player.move @player.collect_stars(@stars) { |gain| @score += gain } if rand(100) < 3 and @stars.size < 25 then @stars.push(Star.new(@star_anim)) end end def button_down(id) if id == Gosu::Button::KbEscape close end end def draw @font.draw("Score: #{@score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00) @background_image.draw(0, @phase, ZOrder::Background) @background_image.draw(0, @phase-480, ZOrder::Background) @player.draw @stars.each { |star| if star.y > 470 then star.kill = true else star.draw end } end end GameWindow.new.show |
|
Copyright © 2005-2006 Joby Bednar, JobyBednar.com, All Rights Reserved
|
|