Class: Snake
- Inherits:
-
Object
- Object
- Snake
- Defined in:
- lib/escape_to_rubyconf/snake.rb
Instance Attribute Summary collapse
-
#boundary ⇒ Object
Returns the value of attribute boundary.
-
#height ⇒ Object
Returns the value of attribute height.
-
#range ⇒ Object
Returns the value of attribute range.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #at_target? ⇒ Boolean
- #attack! ⇒ Object
- #attack? ⇒ Boolean
- #draw ⇒ Object
- #draw_coordinates ⇒ Object
- #end(x, y) ⇒ Object
- #frame ⇒ Object
- #hiss! ⇒ Object
-
#initialize(level, width = 128, height = 128, boundary = 80) ⇒ Snake
constructor
A new instance of Snake.
- #register_hiss ⇒ Object
- #reverse! ⇒ Object
- #start(x, y) ⇒ Object
- #state ⇒ Object
- #target ⇒ Object
- #update ⇒ Object
- #walk? ⇒ Boolean
Constructor Details
#initialize(level, width = 128, height = 128, boundary = 80) ⇒ Snake
Returns a new instance of Snake.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/escape_to_rubyconf/snake.rb', line 6 def initialize level, width=128, height=128, boundary=80 @level = level @width, @height = width, height # x and y are the center of the bounds @range = @boundary = boundary @tiles = Gosu::Image.load_tiles @level.window, File.dirname(__FILE__) + '/assets/snake.png', @width, @height, false @direction = :right @state = :walk @states = { :walk => { :frames => [0,1], :speed => 0.25, :movement => 6, :time => 0.0 }, :attack => { :frames => [2,3,4], :speed => 0.35, :movement => 6, :time => 0.0 } } self.start @level.window.width-@boundary, @boundary self.end @boundary, @level.window.height-@boundary @target = :start # :start # Register the sound so multiple snakes will use same hiss register_hiss end |
Instance Attribute Details
#boundary ⇒ Object
Returns the value of attribute boundary.
5 6 7 |
# File 'lib/escape_to_rubyconf/snake.rb', line 5 def boundary @boundary end |
#height ⇒ Object
Returns the value of attribute height.
5 6 7 |
# File 'lib/escape_to_rubyconf/snake.rb', line 5 def height @height end |
#range ⇒ Object
Returns the value of attribute range.
5 6 7 |
# File 'lib/escape_to_rubyconf/snake.rb', line 5 def range @range end |
#width ⇒ Object
Returns the value of attribute width.
5 6 7 |
# File 'lib/escape_to_rubyconf/snake.rb', line 5 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/escape_to_rubyconf/snake.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/escape_to_rubyconf/snake.rb', line 5 def y @y end |
Instance Method Details
#at_target? ⇒ Boolean
59 60 61 62 |
# File 'lib/escape_to_rubyconf/snake.rb', line 59 def at_target? ( (@x <= target.first + 0.5 && @x >= target.first - 0.5) && (@y <= target.last + 0.5 && @y >= target.last - 0.5) ) end |
#attack! ⇒ Object
32 33 34 35 36 37 |
# File 'lib/escape_to_rubyconf/snake.rb', line 32 def attack! unless attack? @states[:attack][:time] = @level.window.time hiss! end end |
#attack? ⇒ Boolean
39 40 41 |
# File 'lib/escape_to_rubyconf/snake.rb', line 39 def attack? @states[:attack][:time] + @states[:attack][:speed] > @level.window.time end |
#draw ⇒ Object
97 98 99 |
# File 'lib/escape_to_rubyconf/snake.rb', line 97 def draw @tiles[frame].draw *draw_coordinates end |
#draw_coordinates ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/escape_to_rubyconf/snake.rb', line 101 def draw_coordinates x = Math::round(@x - @width/2) y = Math::round((@y - @boundary/2)/2+200 - @height + @boundary/2) z = 1 + (@y/@level.window.height) fx = 1.0 fy = 1.0 if @direction == :left fx = -1.0 x += @width end [x, y, z, fx, fy] end |
#end(x, y) ⇒ Object
69 70 71 |
# File 'lib/escape_to_rubyconf/snake.rb', line 69 def end x, y @end = [x, y] end |
#frame ⇒ Object
47 48 49 |
# File 'lib/escape_to_rubyconf/snake.rb', line 47 def frame (((@level.window.time - state[:time]) / state[:speed] % 1) * state[:frames].size).to_i + state[:frames].first end |
#hiss! ⇒ Object
80 81 82 83 |
# File 'lib/escape_to_rubyconf/snake.rb', line 80 def hiss! hiss = @level.window.sounds[:hiss] hiss.play if hiss end |
#register_hiss ⇒ Object
73 74 75 76 77 78 |
# File 'lib/escape_to_rubyconf/snake.rb', line 73 def register_hiss if @level.window.sounds[:hiss].nil? hiss = Gosu::Sample.new @level.window, File.dirname(__FILE__) + '/assets/snake-hiss.wav' @level.window.sounds[:hiss] = hiss end end |
#reverse! ⇒ Object
55 56 57 |
# File 'lib/escape_to_rubyconf/snake.rb', line 55 def reverse! @target = (@target == :start) ? :end : :start end |
#start(x, y) ⇒ Object
64 65 66 67 |
# File 'lib/escape_to_rubyconf/snake.rb', line 64 def start x, y @x, @y = x, y @start = [x, y] end |
#state ⇒ Object
28 29 30 |
# File 'lib/escape_to_rubyconf/snake.rb', line 28 def state @states[attack? ? :attack : :walk] end |
#target ⇒ Object
51 52 53 |
# File 'lib/escape_to_rubyconf/snake.rb', line 51 def target (@target == :start) ? @start : @end # patrol state end |
#update ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/escape_to_rubyconf/snake.rb', line 85 def update a = Gosu::angle @x, @y, target.first, target.last xmd = Gosu::offset_x a, state[:movement] xsd = target.first - @x ymd = Gosu::offset_y a, state[:movement] ysd = target.last - @y @direction = (xmd < 0) ? :left : :right @x += (xmd.abs < xsd.abs) ? xmd : xsd @y += (ymd.abs < ysd.abs) ? ymd : ysd reverse! if at_target? end |
#walk? ⇒ Boolean
43 44 45 |
# File 'lib/escape_to_rubyconf/snake.rb', line 43 def walk? !attack end |