Class: Shape

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

Overview

General Shape class

Direct Known Subclasses

ShapeI, ShapeL, ShapeO, ShapeS, ShapeT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game) ⇒ Shape

Returns a new instance of Shape.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/shape.rb', line 8

def initialize(game)
  @game = game
  @x = @y = 0
  @blocks = []

  # is the shape currently falling?
  @falling = true

  # by which block to rotate
  @rotation_block = @blocks[1]

  # how many rotation states are there
  @rotations = 1

  # rotation state
  @rotation = 0
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

#falling=(value) ⇒ Object (writeonly)

Sets the attribute falling

Parameters:

  • value

    the value to set the attribute falling to.



153
154
155
# File 'lib/shape.rb', line 153

def falling=(value)
  @falling = value
end

#rotationObject

Returns the value of attribute rotation.



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

def rotation
  @rotation
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#collides?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'lib/shape.rb', line 98

def collides?
  if collides_with_walls? || collides_with_blocks?
    return true
  end

  false
end

#collides_with_blocks?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/shape.rb', line 114

def collides_with_blocks?
  get_blocks.each do |block|
    return true if block.collides_with_blocks?
  end

  false
end

#collides_with_walls?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/shape.rb', line 106

def collides_with_walls?
  max_y = maximum_y_block
  min_x = minimum_x_block
  max_x = maximum_x_block

  min_x.x < 0 || max_x.x >= @game.screen_width || max_y.y + Block.height > @game.screen_height
end

#drawObject



145
146
147
# File 'lib/shape.rb', line 145

def draw
  get_blocks.each(&:draw)
end

#falling?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/shape.rb', line 149

def falling?
  @falling
end

#maximum_x_blockObject



131
132
133
# File 'lib/shape.rb', line 131

def maximum_x_block
  get_blocks.max_by(&:x)
end

#maximum_y_blockObject

note that maximum y means the lowest block!



123
124
125
# File 'lib/shape.rb', line 123

def maximum_y_block
  get_blocks.max_by(&:y)
end

#minimum_x_blockObject



127
128
129
# File 'lib/shape.rb', line 127

def minimum_x_block
  get_blocks.min_by(&:x)
end

#rotateObject

Rotates the shape



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/shape.rb', line 58

def rotate
  return if @rotation_block.nil?

  (1..@rotation % @rotations).each do |_i|
    @blocks.each do |block|
      old_x = block.x
      old_y = block.y
      block.x = @rotation_block.x + (@rotation_block.y - old_y)
      block.y = @rotation_block.y - (@rotation_block.x - old_x)
    end
  end
end

#shape_to_arrayObject



71
72
73
74
75
76
77
# File 'lib/shape.rb', line 71

def shape_to_array
  blocks_array = []
  get_blocks.each do |block|
    blocks_array << [block.x, block.y]
  end
  blocks_array
end

#translate_by_yObject

flips the shape by y axis



136
137
138
139
140
141
142
143
# File 'lib/shape.rb', line 136

def translate_by_y
  min = @blocks.min_by(&:x)
  max = @blocks.max_by(&:x)
  center = (min.x + max.x) / 2.0
  @blocks.each do |block|
    block.x = 2 * center - block.x - Block.width
  end
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shape.rb', line 26

def update
  return unless @falling

  old_x = @x
  old_y = @y

  # first handle x change (handle keyboard - movement -
  # and check for collision)
  if update_move_x
    if @game.button_down?(Gosu::KbLeft)
      @x -= 32
    elsif @game.button_down?(Gosu::KbRight)
      @x += 32
    end

    @x = old_x if collides?
  end

  # then handle y change, so we can set @falling to false
  # game speed check

  if update_move_y
    @y += 32

    if collides?
      @y = old_y
      @falling = false
    end
  end
end

#update_move_xObject

updates move counter



80
81
82
83
84
# File 'lib/shape.rb', line 80

def update_move_x
  if @game.elapsed_seconds > @game.last_move_x + 0.05
    @game.last_move_x = @game.elapsed_seconds
  end
end

#update_move_yObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/shape.rb', line 86

def update_move_y
  if @game.button_down?(Gosu::KbDown)
    @game.game_speed *= 0.2
  else
    @game.game_speed = 1
  end

  if @game.elapsed_seconds > @game.last_move_y + @game.game_speed
    @game.last_move_y = @game.elapsed_seconds
  end
end