Class: Snake

Inherits:
Object
  • Object
show all
Defined in:
lib/escape_to_rubyconf/snake.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#boundaryObject

Returns the value of attribute boundary.



5
6
7
# File 'lib/escape_to_rubyconf/snake.rb', line 5

def boundary
  @boundary
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/escape_to_rubyconf/snake.rb', line 5

def height
  @height
end

#rangeObject

Returns the value of attribute range.



5
6
7
# File 'lib/escape_to_rubyconf/snake.rb', line 5

def range
  @range
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/escape_to_rubyconf/snake.rb', line 5

def width
  @width
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/escape_to_rubyconf/snake.rb', line 5

def x
  @x
end

#yObject

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

Returns:

  • (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

Returns:

  • (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

#drawObject



97
98
99
# File 'lib/escape_to_rubyconf/snake.rb', line 97

def draw
  @tiles[frame].draw *draw_coordinates
end

#draw_coordinatesObject



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

#frameObject



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_hissObject



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

#stateObject



28
29
30
# File 'lib/escape_to_rubyconf/snake.rb', line 28

def state
  @states[attack? ? :attack : :walk]
end

#targetObject



51
52
53
# File 'lib/escape_to_rubyconf/snake.rb', line 51

def target
  (@target == :start) ? @start : @end # patrol state
end

#updateObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/escape_to_rubyconf/snake.rb', line 43

def walk?
  !attack
end