-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaddle.rb
More file actions
87 lines (75 loc) · 1.84 KB
/
paddle.rb
File metadata and controls
87 lines (75 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Paddle Class (Player)
#
# Author: Raven Avalon
# Website: ravenavalon.com
# Version: 1.0
require 'gosu'
class Paddle
attr_accessor :spawn_x, :spawn_y, :current_y
attr_reader :section_height, :paddle_height, :paddle_width
# Define the paddles' width, height, and keep a record of it's current_y position
#
# @paddle_colour is used by Gosu to color our paddle
#
# We don't have an image for the paddle, so instead we will use a
# pipe character, as denoted by the @image variable. This is also used
# by the Gosu gaming library
def initialize window
@paddle_width = 20
@paddle_height = (641 / 4).round
@current_y = 200
@paddle_colour = Gosu::Color.new 0xfff1f1f1
@image = Gosu::Image.from_text(window, '|', 'Source Code Pro', 160)
end
# Conditionally draw the left or right paddle based on which player
# is passed into the method
def draw player
if player == 'player_1'
@image.draw((1140 * 0.02).round, @current_y, 500)
elsif player == 'player_2'
@image.draw(1030, @current_y, 500)
end
end
# Move our paddle up towards the top of the table object
# only if the top boundry is not in our way
def move_up
move_succeed = nil
if any_boundries? 'top'
move_succeed = false
else
move_succeed = true
@current_y -= 5
end
move_succeed
end
# Move our paddle down towards the bottom of the table object
# only if the bottom boundry is not in our way
def move_down
move_succeed = nil
if any_boundries? 'bottom'
move_succeed = false
else
move_succeed = true
@current_y += 5
end
move_succeed
end
# Check if we are too close to the top or bottom
# Boundries of the table object.
def any_boundries? direction
case direction
when 'top'
if (@current_y - 5) < -50
true
else
false
end
when 'bottom'
if (@current_y + 5) > 490
true
else
false
end
end
end
end