Source

robot / robot.rb

require 'chingu'
require 'gosu'
# require 'chipmunk'

class Game < Chingu::Window
  def initialize
    super    
    self.factor = 6
    self.input = { :escape => :exit }
    self.caption = "Dancin' Fool"
    retrofy
    Robot.create(:x => $window.width/2, :y => $window.height/2)
  end
end


class Robot < Chingu::GameObject
  traits :timer
  
  def initialize(options= {})
    super
    @animation = Chingu::Animation.new(:file => "robot2_72x72.bmp")
    @animation.frame_names = { :stand => 0..0, :up => 3..3, :down => 7..8, :left => 1..1, :right => 2..2 }
    self.input = {
      :holding_up => :arms_up, 
      :holding_down => :arms_down,
      :holding_left => :left,
      :holding_right => :right
    }
    
    @frame_name = :stand
    @last_x, @last_y = @x, @y
    update
  end

  
  
  def arms_up
    # @y -=1
    @frame_name = :up
  end
  
  def arms_down
    # @y += 1
    @frame_name = :down
  end
   
  def left
    # @x -= 1
    @frame_name = :left
  end

  def right
    # @x += 1
    @frame_name = :right
  end    

  def update
    
    # Move the animation forward by fetching the next frame and putting it into @image
    # @image is drawn by default by GameObject#draw
    @image = @animation[@frame_name].next
    
    #
    # If droid stands still, use the scanning animation
    #
    @frame_name = :stand if @x == @last_x && @y == @last_y
    
    @x, @y = @last_x, @last_y if outside_window?  # return to previous coordinates if outside window
    @last_x, @last_y = @x, @y                     # save current coordinates for possible use next time
  end
end


Game.new.show   # Start the Game update/draw loop!
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.