-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
354 lines (331 loc) · 9.34 KB
/
Game.java
File metadata and controls
354 lines (331 loc) · 9.34 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package Dino_Game_java;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import java.awt.event.KeyListener;
class Game extends Frame implements KeyListener{
final int D_W = 1200;
final int D_H = 550;
static int unit = 10;
Color colorDinosaur = Color.GRAY;
Color colorGameOver1 = Color.black;
Color colorGameOver2 = Color.yellow;
Color colorCactus1 = Color.gray;
Color colorCactus2 = Color.gray;
int jump = 0;
int jumpY = 0;
int y = 0;
boolean onEnterPresses = false;
boolean down = false;
List<MyGraph> myGraphs = new ArrayList<>();
int currentDinosaurX = 0;
int currentDinosaurY = 0;
boolean gameOver = false;
DrawPanel drawPanel = new DrawPanel();
public static void main(String args[]) {
new Game();
}
public Game() {
super("Run Dino Run");
setSize(1200, 550); // set the size of the window
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
addKeyListener(this);
initCactusG();
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!gameOver) {
if (jump >= D_W) {
jump = 0;
initCactusG();
drawPanel.repaint();
} else {
jump += 10;
drawPanel.repaint();
}
}
}
};
Timer timer = new javax.swing.Timer(40, listener);
timer.start();
ActionListener listenerD = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!gameOver) {
if (onEnterPresses) {
if (down) {
jumpY -= 20;
} else {
jumpY += 20;
}
}
if (jumpY >= 280) {
down = true;
}
if (jumpY <= 0) {
onEnterPresses = false;
down = false;
jumpY = 0;
}
}
}
};
Timer timerD = new javax.swing.Timer(80, listenerD);
timerD.start();
add(drawPanel);
pack();
// setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
// create the cactus on random positions
private void initCactusG() {
Random rr = new Random();
int nbr = 2;// rr.nextInt(2)+1 ;;
int x_ = 10;
int y_ = 100;
int h_ = 60;
int p_ = 10;
myGraphs = new ArrayList<Game.MyGraph>();
for (int it = 0; it < nbr; it++) {
Random r = new Random();
int step = r.nextInt(10) + 1;
MyGraph myGraph = new MyGraph();
myGraph.x_ = x_ * 30 + step * 10 + 600;
myGraph.h_ = 10 + (6 * step) + 2;
myGraph.y_ = 300 - h_;
myGraph.p_ = 8 + step / 2;
myGraphs.add(myGraph);
}
}
// draw the cactus
private void drawCactus(Graphics g) {
int x = 0;
int y = 0;
int h = 0;
int p = 0;
for (MyGraph myGraph : myGraphs) {
x = myGraph.x_;
h = myGraph.h_;
y = myGraph.y_;
p = myGraph.p_;
int maxH = 180;
int i = p * 2 + 40;
int j = p * 2 + 40;
int y1 = y + 40;
int y2 = y + 60;
if (x + j - jump < 0) {
jump = 0;
}
draw(g, x - i - jump, y1, h, p);
draw(g, x - jump, y, maxH, p * 2);
draw(g, x + j - jump, y2, h, p);
drow2(g, x - jump, h, p, i, j, y1, y2);
}
}
// on game over draw the game over text
private void gameOver(Graphics g) {
Graphics2D graph = (Graphics2D) g;
graph.setPaint(colorGameOver1);
graph.setFont(new Font("MV Boli", 20, 50));
graph.drawString("Game Over", 550, 150);
//restart
Graphics2D graph1 = (Graphics2D) g;
graph1.setPaint(colorGameOver1);
graph1.setFont(new Font("MV Boli", 20, 50));
graph1.drawString("Press Space key to restart!!", 350, 250);
}
// restart the game
private void restartGame(Graphics g) {
new Game();
}
// draw the sun on the sky
private void drawSun(Graphics g) {
Graphics2D sun1 = (Graphics2D) g;
sun1.setPaint(new Color(255, 255, 0));
sun1.fillArc(900, 70, 80, 80, 90, 180);
Graphics2D sun2 = (Graphics2D) g;
sun2.setPaint(new Color(255, 255, 153));
sun2.fillArc(900, 70, 80, 80, 270, 180);
}
// draw the cactus
private void drow2(Graphics g, int x, int h, int p, int i, int j, int y1, int y2) {
Graphics2D gsds = (Graphics2D) g;
gsds.setPaint(colorCactus1);
gsds.fillRect(x - i + p, y1 + h, i, p);
Graphics2D gsdds = (Graphics2D) g;
gsdds.setPaint(colorCactus2);
gsdds.fillRect(x - i + 2 * p, y1 + h - p, i - 2 * p, p);
Graphics2D gsd2 = (Graphics2D) g;
gsd2.setPaint(colorCactus2);
gsd2.fillRect(x + p * 2, y2 + h, j - p, p);
Graphics2D gsd3 = (Graphics2D) g;
gsd3.setPaint(colorCactus1);
gsd3.fillRect(x + p * 4, y2 + h - p, j - 4 * p, p);
}
// draw the surface
private void drawSol(Graphics g, int x, int y, int maxH) {
Graphics2D sol = (Graphics2D) g;
sol.setPaint(Color.orange);
sol.fillRect(0, y + maxH - 20, 1700, 100);
}
// draw the dinausor
private void drawDinausor(Graphics g, int y) {
int xDinausor = 180;
int step = 1;
g.setColor(colorDinosaur);
currentDinosaurX = xDinausor;
currentDinosaurY = y;
drawRaw(g, xDinausor, y, 2, 1);
drawRaw(g, xDinausor + 4 * unit, y, 2, 1);
drawRaw(g, xDinausor, y - step * unit, 1, 1);
drawRaw(g, xDinausor + 4 * unit, y - step * unit, 1, 1);
step++;
drawRaw(g, xDinausor, y - step * unit, 2, 1);
drawRaw(g, xDinausor + 3 * unit, y - step * unit, 2, 1);
step++;
drawRaw(g, xDinausor, y - step * unit, 5, 1);
step++;
drawRaw(g, xDinausor - unit, y - step * unit, 6, 1);
step++;
drawRaw(g, xDinausor - 2 * unit, y - step * unit, 8, 1);
step++;
drawRaw(g, xDinausor - 3 * unit, y - step * unit, 10, 1);
step++;
drawRaw(g, xDinausor - 4 * unit, y - step * unit, 11, 1);
drawRaw(g, xDinausor + (11 + 1 - 4) * unit, y - step * unit, 1, 1);
step++;
drawRaw(g, xDinausor - 4 * unit, y - step * unit, 3, 1);
drawRaw(g, xDinausor + (5 - 4) * unit, y - step * unit, 8, 1);
step++;
drawRaw(g, xDinausor - 4 * unit, y - step * unit, 2, 1);
drawRaw(g, xDinausor + (6 - 4) * unit, y - step * unit, 5, 1);
step++;
drawRaw(g, xDinausor - 4 * unit, y - step * unit, 1, 1);
drawRaw(g, xDinausor + (7 - 4) * unit, y - step * unit, 4, 1);
step++;
drawRaw(g, xDinausor - 4 * unit, y - step * unit, 1, 1);
drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 7, 1);
step++;
drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 4, 1);
step++;
drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 8, 1);
step++;
drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 2, 1);
drawRaw(g, xDinausor + (11 - 4) * unit, y - step * unit, 5, 1);
step++;
drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 8, 1);
step++;
drawRaw(g, xDinausor + (9 - 4) * unit, y - step * unit, 6, 1);
step++;
}
private void drawRaw(Graphics g, int Dinausor, int y, int w, int h) {
Graphics2D sun16 = (Graphics2D) g;
sun16.fillRect(Dinausor, y, w * unit, h * unit);
}
private void draw(Graphics g, int x, int y, int h, int p) {
if (x <= currentDinosaurX && x + p >= currentDinosaurX && y <= currentDinosaurY) {
gameOver(g);
gameOver = true;
return;
}
Graphics2D gcd = (Graphics2D) g;
// Green 0 -204- 0
gcd.setPaint(colorCactus1);
gcd.fillRect(x, y, p, h);
Graphics2D gsd = (Graphics2D) g;
// Very dark green 0 -102- 0
gsd.setPaint(colorCactus2);
gsd.fillRect(x + p, y, p, h);
Graphics2D gssd = (Graphics2D) g;
// Very dark green 0 -102- 0
gssd.setPaint(colorCactus2);
gssd.fillArc(x, y - p, p * 2, p * 2, 1, 90);
Graphics2D gzssd = (Graphics2D) g;
gzssd.setPaint(colorCactus1);
gzssd.fillArc(x, y - p, p * 2, p * 2, 90, 90);
Graphics2D ghssd = (Graphics2D) g;
ghssd.setPaint(colorCactus1);
ghssd.fillArc(x, y + h - p, p * 2, p * 2, 180, 90);
Graphics2D ghzssd = (Graphics2D) g;
ghzssd.setPaint(colorCactus2);
ghzssd.fillArc(x, y + h - p, p * 2, p * 2, 270, 90);
}
private class DrawPanel extends JPanel {
public DrawPanel() {
MoveAction action = new MoveAction("onEnter");
String ACTION_KEY = "onEnter";
KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
inputMap.put(W, ACTION_KEY);
ActionMap actionMap = getActionMap();
actionMap.put(ACTION_KEY, action);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawCactus(g);
drawSun(g);
drawSol(g, 100, 250, 180);
drawDinausor(g, 400 - jumpY);
if (gameOver) {
gameOver(g);
}
}
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
}
private class MyGraph {
int x_ = 10;
int y_ = 100;
int h_ = 60;
int p_ = 10;
}
class MoveAction extends AbstractAction {
public MoveAction(String name) {
putValue(NAME, name);
}
public void actionPerformed(ActionEvent actionEvent) {
onEnterPresses = true;
drawPanel.repaint();
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
if(gameOver){
gameOver = false;
restartGame(getGraphics());
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}