-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroids.rb
92 lines (73 loc) · 1.3 KB
/
asteroids.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'ruby2d'
require './lib/ship'
require './lib/asteroid'
##
# Window
##
set title: 'Asteroids',
background: 'black',
with: 640,
height: 480,
resizable: false
##
# Score
##
score = 0
score_text = Text.new(
"%03d" % score,
x: 10,
y: 10,
font: 'assets/fonts/Audiowide.ttf',
color: 'white',
size: 20,
opacity: 1
)
##
# Bullets
##
bullets = []
##
# Asteroids
##
asteroids = []
##
# Ship
##
ship = Ship.new(
x1: get(:width) / 2, y1: get(:height) / 2 - 20,
x2: get(:width) / 2 + 10, y2: get(:height) / 2 + 12,
x3: get(:width) / 2 - 10, y3: get(:height) / 2 + 12,
window: get(:window),
color: 'white'
)
# Ship move
on :key_held do |event|
ship.move(event, left: 'left', right: 'right', up: 'up')
end
# Ship shoot
on :key_down do |event|
if event.key == 'space'
bullets << ship.shoot
end
end
update do
ship.fly
(5 - asteroids.size).times do
asteroids << Asteroid.new(window: get(:window))
end
bullets.each do |bullet|
if asteroid = bullet.scored.first
asteroid.source.remove
asteroids.delete(asteroid)
score += 20
score_text.text = "%03d" % score
bullets.delete(bullet)
else
bullet.move(window: get(:window), asteroids: asteroids)
end
end
asteroids.each do |asteroid|
asteroid.move
end
end
show