-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroid.rb
41 lines (36 loc) · 1.06 KB
/
asteroid.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
class Asteroid
WIDTH = 58
HEIGHT = 61
SPEEDS = (1..4).to_a
ROTATATIONS = (-2..2).to_a
def initialize
scale = 0.5 + (rand * 1.5)
speed = SPEEDS.sample
@rotation = ROTATATIONS.sample
@sprite = Sprite.new(
'asteroid.png',
x: rand(Window.width),
y: rand(Window.height),
width: WIDTH * scale,
height: HEIGHT * scale,
rotate: rand(360)
)
@x_velocity = Math.sin(@sprite.rotate * Math::PI / 180) * speed
@y_velocity = -Math.cos(@sprite.rotate * Math::PI / 180) * speed
end
def move
@sprite.rotate += @rotation
@sprite.x += @x_velocity
@sprite.y += @y_velocity
if @sprite.x > Window.width + @sprite.width
@sprite.x = [email protected]
elsif @sprite.x < [email protected]
@sprite.x = Window.width + @sprite.width
end
if @sprite.y > Window.height + @sprite.height
@sprite.y = [email protected]
elsif @sprite.y < [email protected]
@sprite.y = Window.height + @sprite.height
end
end
end