Class: Carpet

Inherits:
Object
  • Object
show all
Includes:
Positionable
Defined in:
lib/carpet.rb

Constant Summary collapse

CARPET_SPEED =
5

Instance Attribute Summary collapse

Attributes included from Positionable

#x, #y

Instance Method Summary collapse

Methods included from Positionable

#bottom, #right

Constructor Details

#initialize(window, carpet_image_file = 'carpet.png', carpet_image_flipped_file = 'carpet_flipped.png') ⇒ Carpet

Returns a new instance of Carpet.



10
11
12
13
14
15
16
17
# File 'lib/carpet.rb', line 10

def initialize(window, carpet_image_file = 'carpet.png', carpet_image_flipped_file = 'carpet_flipped.png')
  @carpet_image = @carpet_image_right = Gosu::Image.new(window, path_to_media(carpet_image_file))
  @carpet_image_left = Gosu::Image.new(window, path_to_media(carpet_image_flipped_file))
  @window = window
  @x = window.width/2.0 - @carpet_image.width/2.0
  @x += 1 if window.width.odd? && @carpet_image.width.odd?
  @y = window.height/(18/13.0) - @carpet_image.height/2.0
end

Instance Attribute Details

#carpet_imageObject (readonly) Also known as: image

Returns the value of attribute carpet_image.



6
7
8
# File 'lib/carpet.rb', line 6

def carpet_image
  @carpet_image
end

Instance Method Details

#collides_with?(object) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/carpet.rb', line 38

def collides_with?(object)
  object.bottom  * object.scale > top    &&
    object.top                  < bottom &&
    object.right * object.scale > left   &&
    object.left                 < right
end

#draw(counter, score) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/carpet.rb', line 19

def draw(counter, score)
  if score > 3
    @carpet_image.draw(@x, @y, 4)
  elsif score > 0
    @z = counter % 20 == 1 ? 1 : 4
    @carpet_image.draw(@x, @y, @z)
  end
end

#move_leftObject



28
29
30
31
# File 'lib/carpet.rb', line 28

def move_left
  @x = [@x - CARPET_SPEED, 0 - @carpet_image.height / 4].max
  @carpet_image = @carpet_image_left
end

#move_rightObject



33
34
35
36
# File 'lib/carpet.rb', line 33

def move_right
  @x = [@x + CARPET_SPEED, @window.width - @carpet_image.height / 2].min
  @carpet_image = @carpet_image_right
end