Class: Rong::Elements::Paddle

Inherits:
Object
  • Object
show all
Includes:
Entity
Defined in:
lib/rong/elements/paddle.rb

Constant Summary collapse

WIDTH =
10
HEIGHT =
50
SPEED =
10

Instance Attribute Summary collapse

Attributes included from Entity

#height, #width, #x, #y

Instance Method Summary collapse

Methods included from Entity

#bottom, #intersects?, #left, #move_to, #right, #top

Constructor Details

#initialize(name, side, start_x, start_y) ⇒ Paddle

Returns a new instance of Paddle.



13
14
15
16
17
# File 'lib/rong/elements/paddle.rb', line 13

def initialize(name, side, start_x, start_y)
  self.name = name
  self.side = side
  super(start_x, start_y)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/rong/elements/paddle.rb', line 11

def name
  @name
end

#sideObject

Returns the value of attribute side.



11
12
13
# File 'lib/rong/elements/paddle.rb', line 11

def side
  @side
end

Instance Method Details

#hit(ball) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/rong/elements/paddle.rb', line 31

def hit(ball)
  ball_x = left? ? right : left
  ball.reflect_x(ball_x)
  ball.reflect_y if ball.y < y

  strength = (y - ball.y).abs / (height / 2)
  ball.angle = 45 * strength
end

#left?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rong/elements/paddle.rb', line 27

def left?
  side == :left
end

#move_downObject



23
24
25
# File 'lib/rong/elements/paddle.rb', line 23

def move_down
  self.y += SPEED
end

#move_upObject



19
20
21
# File 'lib/rong/elements/paddle.rb', line 19

def move_up
  self.y -= SPEED
end