-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplant.rb
54 lines (47 loc) · 1003 Bytes
/
plant.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
49
50
51
52
53
54
class Plant
ROTATION_SPEED = 3
ACCELERATION = 2
FRICTION = 0.9
attr_reader :x, :y, :angle, :radius
def initialize(window)
@x = 300
@y = 550
@angle = 0
@image = Gosu::Image.new('images/lava-guava.png')
@velocity_x = 0
@velocity_y = 0
@radius = 20
@window = window
end
def draw
@image.draw_rot(@x, @y, 1, @angle)
end
def turn_right
@angle += ROTATION_SPEED
end
def turn_left
@angle -= ROTATION_SPEED
end
def accelerate
@velocity_x += Gosu.offset_x(@angle, ACCELERATION)
@velocity_y += Gosu.offset_y(@angle, ACCELERATION)
end
def move
@x += @velocity_x
@y += @velocity_y
@velocity_x *= FRICTION
@velocity_y *= FRICTION
if @x > @window.width - @radius
@velocity_x = 0
@x = @window.width - @radius
end
if @x < @radius
@velocity_x = 0
@x = @radius
end
if @y > @window.height - @radius
@velocity_y = 0
@y = @window.height - @radius
end
end
end