-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplayer.rb
47 lines (38 loc) · 894 Bytes
/
player.rb
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
class Player
attr_reader :x, :y, :image
def initialize(window, side)
@image = Gosu::Image.new("media/paddle.png")
@window = window
@side = side
@score = 0
@font = Gosu::Font.new(20)
if side == 'left'
@x = 50
else
@x = window.width - @image.width - 50
end
@y = (window.height - @image.height) / 2
end
def wins
@score += 1
end
def update
if Gosu::button_down?(up_button)
@y -= 5 unless @y == 0
elsif Gosu::button_down?(down_button)
@y += 5 unless @y == (@window.height - @image.height)
end
end
def draw
@image.draw(@x, @y, 1)
x = @side == 'left' ? @x : @x - 40
@font.draw("Score: #{@score}", x, 10, 2, 1.0, 1.0, 0xff_ffff00)
end
private
def up_button
@side == 'left' ? Gosu::KbW : Gosu::KbUp
end
def down_button
@side == 'left' ? Gosu::KbS : Gosu::KbDown
end
end