-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.rb
77 lines (60 loc) · 1.31 KB
/
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
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
# frozen_string_literal: true
require_relative 'sprite'
# Player
class Player < Sprite
ROTATION_SPEED = 3
ACCELERATION = 2
FRICTION = 0.9
attr_reader :angle
def initialize(window)
@x = 200
@y = 200
@angle = 0
@image = Gosu::Image.new('images/ship.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
not_move_beyond_right_boundary
not_move_beyond_left_boundary
not_move_beyond_bottom_boundary
end
def off_top?
y < radius
end
private
def not_move_beyond_right_boundary
return unless @x > @window.width - @radius
@velocity_x = 0
@x = @window.width - @radius
end
def not_move_beyond_left_boundary
return unless @x < @radius
@velocity_x = 0
@x = @radius
end
def not_move_beyond_bottom_boundary
return unless @y > @window.height - @radius
@velocity_y = 0
@y = @window.height - @radius
end
end