-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asteroids.rb
211 lines (167 loc) · 4.08 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
include Java
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.Timer
import java.awt.Color
import java.awt.Dimension
import java.awt.event.ActionListener
import java.awt.event.MouseListener
import java.awt.event.MouseMotionListener
import java.awt.Graphics
import java.awt.Point
import java.awt.Polygon
class Asteroid
attr_accessor :xPos, :yPos, :clr, :width
def initialize
self.xPos = Random.rand(600);
self.yPos = Random.rand(600);
self.clr = Color.gray;
self.width = Random.rand(15) + 5
end
def draw(g)
g.setColor(self.clr)
g.fillOval(xPos, yPos, self.width, self.width)
end
def move
self.xPos = self.xPos + Random.rand(16) - Random.rand(16)
self.yPos = self.yPos + Random.rand(16) - Random.rand(16)
end
end
class SpaceShip
attr_accessor :xPos, :yPos, :clr, :shooting
WIDTH = 40
HEIGHT = 20
WIN_SIZE = 5
def initialize
self.xPos = Random.rand(500);
self.yPos = Random.rand(500);
self.clr = Color.red;
self.shooting = false
end
def setShooting(s)
self.shooting = s
end
def move(p)
self.xPos = p.getX()
self.yPos = p.getY()
end
def draw(g, width)
g.setColor(clr);
g.fillOval(xPos - WIDTH/2, yPos - HEIGHT/2, WIDTH, HEIGHT);
g.setColor(Color.blue);
g.drawLine(xPos - WIDTH/2, yPos, xPos + WIDTH/2, yPos);
g.setColor(Color.cyan);
g.fillRect(xPos - (WIDTH/3 - WIN_SIZE/2), yPos - 5, WIN_SIZE, WIN_SIZE);
g.fillRect(xPos - WIN_SIZE/2, yPos - 5, WIN_SIZE, WIN_SIZE);
g.fillRect(xPos + (WIDTH/3 - WIN_SIZE), yPos - 5, WIN_SIZE, WIN_SIZE);
g.setColor(Color.green);
g.drawArc(xPos - WIDTH - 5, yPos-HEIGHT, WIDTH, HEIGHT, 0, 75);
g.drawArc(xPos + 5, yPos-HEIGHT, WIDTH, HEIGHT, 180, - 75);
if self.shooting
g.setColor(Color.pink);
g.drawLine(xPos + WIDTH/2, yPos, width, yPos);
end
end
end
class Star
attr_accessor :xPos, :yPos, :clr
def initialize
self.xPos = Random.rand(600);
self.yPos = Random.rand(600);
self.clr = Color.yellow;
end
def draw(g)
g.setColor(self.clr)
p1 = Polygon.new();
p1.addPoint(xPos, yPos + 10)
p1.addPoint(xPos + 5, yPos)
p1.addPoint(xPos + 10, yPos + 10)
p2 = Polygon.new();
p2.addPoint(xPos, yPos + 5)
p2.addPoint(xPos + 10, yPos + 5)
p2.addPoint(xPos + 5, yPos + 10)
g.fillPolygon(p1)
g.fillPolygon(p2)
end
def move (width)
self.xPos = self.xPos - 3
if self.xPos <= 0
self.xPos = width
end
end
end
class Screen < JPanel
include MouseListener, MouseMotionListener, ActionListener
def initialize
super
@stars = []
(1 .. 25).each do |n|
@stars.push(Star.new())
end
@asteroids = []
(1 .. 10).each do |n|
@asteroids.push(Asteroid.new())
end
@ship = SpaceShip.new
@starTimer = Timer.new 10, self
@starTimer.start
@asteroidTimer = Timer.new 100, self
@asteroidTimer.start
self.addMouseMotionListener self
self.addMouseListener self
end
def paintComponent g
super g
self.setBackground Color.black
@ship.draw(g, self.getWidth())
@stars.each do |star|
star.draw(g)
end
@asteroids.each do |a|
a.draw(g)
end
end
# ActionListener
def actionPerformed e
if e.getSource == @starTimer
@stars.each do |star|
star.move(self.getWidth())
end
end
if e.getSource == @asteroidTimer
@asteroids.each do |a|
a.move()
end
end
self.repaint()
end
# MouseMotionListener abstract methods
def mouseMoved e
@ship.move(e.getPoint())
self.repaint()
end
def mouseDragged e
@ship.move(e.getPoint())
self.repaint()
end
# MouseListener abstract methods
def mousePressed e
@ship.setShooting(true)
end
def mouseReleased e
@ship.setShooting(false)
end
def mouseClicked e
end
def mouseEntered e
end
def mouseExited e
end
end
frame = JFrame.new("Asteroids")
frame.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
frame.setPreferredSize Dimension.new 600, 600
panel = Screen.new
frame.add panel
frame.pack
frame.setVisible(true)