-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.rb
48 lines (40 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
48
require './piece'
require './intersector'
class Player < Piece
attr_reader :bullets
def initialize(game_window)
super(game_window, 'images/chase.png')
@speed =5
@last_shot = Time.now
restart
end
def move_left
@x -= @speed if @x > 0
end
def move_right
@x += @speed if @x < (@game_window.width - @width)
end
def move_up
@y -= @speed if @y > 0
end
def move_down
@y += @speed if @y < (@game_window.height - @height)
end
def hit_by?(obj)
if Intersector.intersects(self, obj) and obj.is_a? Obstacle and !obj.destroyed
@game_window.stop_game!
end
end
def shoot
if @bullets > 0 and (Time.now - @last_shot > 1.5)
@last_shot = Time.now
# unlmited @bullets -= 1
@game_window.add_bullet(self)
end
end
def restart
@bullets = 5
@x = 50
@y = @game_window.height - self.height
end
end