|
| 1 | +package Dino_Game_java; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Dimension; |
| 5 | +import java.awt.Font; |
| 6 | +import java.awt.Frame; |
| 7 | +import java.awt.Graphics; |
| 8 | +import java.awt.Graphics2D; |
| 9 | +import java.awt.event.ActionEvent; |
| 10 | +import java.awt.event.ActionListener; |
| 11 | +import java.awt.event.KeyEvent; |
| 12 | +import java.awt.event.WindowAdapter; |
| 13 | +import java.awt.event.WindowEvent; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Random; |
| 17 | +import javax.swing.AbstractAction; |
| 18 | +import javax.swing.ActionMap; |
| 19 | +import javax.swing.InputMap; |
| 20 | +import javax.swing.JPanel; |
| 21 | +import javax.swing.KeyStroke; |
| 22 | +import javax.swing.Timer; |
| 23 | +import java.awt.event.KeyListener; |
| 24 | + |
| 25 | +class Game extends Frame implements KeyListener{ |
| 26 | + final int D_W = 1200; |
| 27 | + final int D_H = 550; |
| 28 | + static int unit = 10; |
| 29 | + Color colorDinosaur = Color.GRAY; |
| 30 | + Color colorGameOver1 = Color.black; |
| 31 | + Color colorGameOver2 = Color.yellow; |
| 32 | + Color colorCactus1 = Color.gray; |
| 33 | + Color colorCactus2 = Color.gray; |
| 34 | + int jump = 0; |
| 35 | + int jumpY = 0; |
| 36 | + int y = 0; |
| 37 | + boolean onEnterPresses = false; |
| 38 | + boolean down = false; |
| 39 | + List<MyGraph> myGraphs = new ArrayList<>(); |
| 40 | + int currentDinosaurX = 0; |
| 41 | + int currentDinosaurY = 0; |
| 42 | + boolean gameOver = false; |
| 43 | + DrawPanel drawPanel = new DrawPanel(); |
| 44 | + public static void main(String args[]) { |
| 45 | + new Game(); |
| 46 | + } |
| 47 | + |
| 48 | + public Game() { |
| 49 | + super("Run Dino Run"); |
| 50 | + setSize(1200, 550); // set the size of the window |
| 51 | + setVisible(true); |
| 52 | + addWindowListener(new WindowAdapter() { |
| 53 | + public void windowClosing(WindowEvent e) { |
| 54 | + dispose(); |
| 55 | + System.exit(0); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + addKeyListener(this); |
| 60 | + initCactusG(); |
| 61 | + |
| 62 | + ActionListener listener = new ActionListener() { |
| 63 | + public void actionPerformed(ActionEvent e) { |
| 64 | + if (!gameOver) { |
| 65 | + if (jump >= D_W) { |
| 66 | + jump = 0; |
| 67 | + initCactusG(); |
| 68 | + drawPanel.repaint(); |
| 69 | + } else { |
| 70 | + jump += 10; |
| 71 | + drawPanel.repaint(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + Timer timer = new javax.swing.Timer(40, listener); |
| 78 | + timer.start(); |
| 79 | + ActionListener listenerD = new ActionListener() { |
| 80 | + public void actionPerformed(ActionEvent e) { |
| 81 | + if (!gameOver) { |
| 82 | + if (onEnterPresses) { |
| 83 | + if (down) { |
| 84 | + jumpY -= 20; |
| 85 | + } else { |
| 86 | + jumpY += 20; |
| 87 | + } |
| 88 | + } |
| 89 | + if (jumpY >= 280) { |
| 90 | + down = true; |
| 91 | + } |
| 92 | + if (jumpY <= 0) { |
| 93 | + onEnterPresses = false; |
| 94 | + down = false; |
| 95 | + jumpY = 0; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + }; |
| 100 | + Timer timerD = new javax.swing.Timer(80, listenerD); |
| 101 | + timerD.start(); |
| 102 | + add(drawPanel); |
| 103 | + pack(); |
| 104 | + // setDefaultCloseOperation(EXIT_ON_CLOSE); |
| 105 | + setLocationRelativeTo(null); |
| 106 | + setVisible(true); |
| 107 | + } |
| 108 | + |
| 109 | + // create the cactus on random positions |
| 110 | + private void initCactusG() { |
| 111 | + Random rr = new Random(); |
| 112 | + int nbr = 2;// rr.nextInt(2)+1 ;; |
| 113 | + int x_ = 10; |
| 114 | + int y_ = 100; |
| 115 | + int h_ = 60; |
| 116 | + int p_ = 10; |
| 117 | + myGraphs = new ArrayList<Game.MyGraph>(); |
| 118 | + for (int it = 0; it < nbr; it++) { |
| 119 | + Random r = new Random(); |
| 120 | + int step = r.nextInt(10) + 1; |
| 121 | + MyGraph myGraph = new MyGraph(); |
| 122 | + myGraph.x_ = x_ * 30 + step * 10 + 600; |
| 123 | + myGraph.h_ = 10 + (6 * step) + 2; |
| 124 | + myGraph.y_ = 300 - h_; |
| 125 | + myGraph.p_ = 8 + step / 2; |
| 126 | + myGraphs.add(myGraph); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // draw the cactus |
| 131 | + private void drawCactus(Graphics g) { |
| 132 | + int x = 0; |
| 133 | + int y = 0; |
| 134 | + int h = 0; |
| 135 | + int p = 0; |
| 136 | + for (MyGraph myGraph : myGraphs) { |
| 137 | + x = myGraph.x_; |
| 138 | + h = myGraph.h_; |
| 139 | + y = myGraph.y_; |
| 140 | + p = myGraph.p_; |
| 141 | + int maxH = 180; |
| 142 | + int i = p * 2 + 40; |
| 143 | + int j = p * 2 + 40; |
| 144 | + int y1 = y + 40; |
| 145 | + int y2 = y + 60; |
| 146 | + if (x + j - jump < 0) { |
| 147 | + jump = 0; |
| 148 | + } |
| 149 | + draw(g, x - i - jump, y1, h, p); |
| 150 | + draw(g, x - jump, y, maxH, p * 2); |
| 151 | + draw(g, x + j - jump, y2, h, p); |
| 152 | + drow2(g, x - jump, h, p, i, j, y1, y2); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // on game over draw the game over text |
| 157 | + private void gameOver(Graphics g) { |
| 158 | + Graphics2D graph = (Graphics2D) g; |
| 159 | + graph.setPaint(colorGameOver1); |
| 160 | + graph.setFont(new Font("MV Boli", 20, 50)); |
| 161 | + graph.drawString("Game Over", 550, 150); |
| 162 | + |
| 163 | + //restart |
| 164 | + Graphics2D graph1 = (Graphics2D) g; |
| 165 | + graph1.setPaint(colorGameOver1); |
| 166 | + graph1.setFont(new Font("MV Boli", 20, 50)); |
| 167 | + graph1.drawString("Press Space key to restart!!", 350, 250); |
| 168 | + } |
| 169 | + |
| 170 | + // restart the game |
| 171 | + private void restartGame(Graphics g) { |
| 172 | + new Game(); |
| 173 | + } |
| 174 | + |
| 175 | + // draw the sun on the sky |
| 176 | + private void drawSun(Graphics g) { |
| 177 | + Graphics2D sun1 = (Graphics2D) g; |
| 178 | + sun1.setPaint(new Color(255, 255, 0)); |
| 179 | + sun1.fillArc(900, 70, 80, 80, 90, 180); |
| 180 | + Graphics2D sun2 = (Graphics2D) g; |
| 181 | + sun2.setPaint(new Color(255, 255, 153)); |
| 182 | + sun2.fillArc(900, 70, 80, 80, 270, 180); |
| 183 | + } |
| 184 | + |
| 185 | + // draw the cactus |
| 186 | + private void drow2(Graphics g, int x, int h, int p, int i, int j, int y1, int y2) { |
| 187 | + Graphics2D gsds = (Graphics2D) g; |
| 188 | + gsds.setPaint(colorCactus1); |
| 189 | + gsds.fillRect(x - i + p, y1 + h, i, p); |
| 190 | + Graphics2D gsdds = (Graphics2D) g; |
| 191 | + gsdds.setPaint(colorCactus2); |
| 192 | + gsdds.fillRect(x - i + 2 * p, y1 + h - p, i - 2 * p, p); |
| 193 | + Graphics2D gsd2 = (Graphics2D) g; |
| 194 | + gsd2.setPaint(colorCactus2); |
| 195 | + gsd2.fillRect(x + p * 2, y2 + h, j - p, p); |
| 196 | + Graphics2D gsd3 = (Graphics2D) g; |
| 197 | + gsd3.setPaint(colorCactus1); |
| 198 | + gsd3.fillRect(x + p * 4, y2 + h - p, j - 4 * p, p); |
| 199 | + } |
| 200 | + |
| 201 | + // draw the surface |
| 202 | + private void drawSol(Graphics g, int x, int y, int maxH) { |
| 203 | + Graphics2D sol = (Graphics2D) g; |
| 204 | + sol.setPaint(Color.orange); |
| 205 | + sol.fillRect(0, y + maxH - 20, 1700, 100); |
| 206 | + } |
| 207 | + |
| 208 | + // draw the dinausor |
| 209 | + private void drawDinausor(Graphics g, int y) { |
| 210 | + int xDinausor = 180; |
| 211 | + int step = 1; |
| 212 | + g.setColor(colorDinosaur); |
| 213 | + currentDinosaurX = xDinausor; |
| 214 | + currentDinosaurY = y; |
| 215 | + drawRaw(g, xDinausor, y, 2, 1); |
| 216 | + drawRaw(g, xDinausor + 4 * unit, y, 2, 1); |
| 217 | + drawRaw(g, xDinausor, y - step * unit, 1, 1); |
| 218 | + drawRaw(g, xDinausor + 4 * unit, y - step * unit, 1, 1); |
| 219 | + step++; |
| 220 | + drawRaw(g, xDinausor, y - step * unit, 2, 1); |
| 221 | + drawRaw(g, xDinausor + 3 * unit, y - step * unit, 2, 1); |
| 222 | + step++; |
| 223 | + drawRaw(g, xDinausor, y - step * unit, 5, 1); |
| 224 | + step++; |
| 225 | + drawRaw(g, xDinausor - unit, y - step * unit, 6, 1); |
| 226 | + step++; |
| 227 | + drawRaw(g, xDinausor - 2 * unit, y - step * unit, 8, 1); |
| 228 | + step++; |
| 229 | + drawRaw(g, xDinausor - 3 * unit, y - step * unit, 10, 1); |
| 230 | + step++; |
| 231 | + drawRaw(g, xDinausor - 4 * unit, y - step * unit, 11, 1); |
| 232 | + drawRaw(g, xDinausor + (11 + 1 - 4) * unit, y - step * unit, 1, 1); |
| 233 | + step++; |
| 234 | + drawRaw(g, xDinausor - 4 * unit, y - step * unit, 3, 1); |
| 235 | + drawRaw(g, xDinausor + (5 - 4) * unit, y - step * unit, 8, 1); |
| 236 | + step++; |
| 237 | + drawRaw(g, xDinausor - 4 * unit, y - step * unit, 2, 1); |
| 238 | + drawRaw(g, xDinausor + (6 - 4) * unit, y - step * unit, 5, 1); |
| 239 | + step++; |
| 240 | + drawRaw(g, xDinausor - 4 * unit, y - step * unit, 1, 1); |
| 241 | + drawRaw(g, xDinausor + (7 - 4) * unit, y - step * unit, 4, 1); |
| 242 | + step++; |
| 243 | + drawRaw(g, xDinausor - 4 * unit, y - step * unit, 1, 1); |
| 244 | + drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 7, 1); |
| 245 | + step++; |
| 246 | + drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 4, 1); |
| 247 | + step++; |
| 248 | + drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 8, 1); |
| 249 | + step++; |
| 250 | + drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 2, 1); |
| 251 | + drawRaw(g, xDinausor + (11 - 4) * unit, y - step * unit, 5, 1); |
| 252 | + step++; |
| 253 | + drawRaw(g, xDinausor + (8 - 4) * unit, y - step * unit, 8, 1); |
| 254 | + step++; |
| 255 | + drawRaw(g, xDinausor + (9 - 4) * unit, y - step * unit, 6, 1); |
| 256 | + step++; |
| 257 | + } |
| 258 | + |
| 259 | + private void drawRaw(Graphics g, int Dinausor, int y, int w, int h) { |
| 260 | + Graphics2D sun16 = (Graphics2D) g; |
| 261 | + sun16.fillRect(Dinausor, y, w * unit, h * unit); |
| 262 | + } |
| 263 | + |
| 264 | + private void draw(Graphics g, int x, int y, int h, int p) { |
| 265 | + if (x <= currentDinosaurX && x + p >= currentDinosaurX && y <= currentDinosaurY) { |
| 266 | + gameOver(g); |
| 267 | + gameOver = true; |
| 268 | + return; |
| 269 | + } |
| 270 | + Graphics2D gcd = (Graphics2D) g; |
| 271 | + // Green 0 -204- 0 |
| 272 | + gcd.setPaint(colorCactus1); |
| 273 | + gcd.fillRect(x, y, p, h); |
| 274 | + Graphics2D gsd = (Graphics2D) g; |
| 275 | + // Very dark green 0 -102- 0 |
| 276 | + gsd.setPaint(colorCactus2); |
| 277 | + gsd.fillRect(x + p, y, p, h); |
| 278 | + Graphics2D gssd = (Graphics2D) g; |
| 279 | + // Very dark green 0 -102- 0 |
| 280 | + gssd.setPaint(colorCactus2); |
| 281 | + gssd.fillArc(x, y - p, p * 2, p * 2, 1, 90); |
| 282 | + Graphics2D gzssd = (Graphics2D) g; |
| 283 | + gzssd.setPaint(colorCactus1); |
| 284 | + gzssd.fillArc(x, y - p, p * 2, p * 2, 90, 90); |
| 285 | + Graphics2D ghssd = (Graphics2D) g; |
| 286 | + ghssd.setPaint(colorCactus1); |
| 287 | + ghssd.fillArc(x, y + h - p, p * 2, p * 2, 180, 90); |
| 288 | + Graphics2D ghzssd = (Graphics2D) g; |
| 289 | + ghzssd.setPaint(colorCactus2); |
| 290 | + ghzssd.fillArc(x, y + h - p, p * 2, p * 2, 270, 90); |
| 291 | + } |
| 292 | + |
| 293 | + private class DrawPanel extends JPanel { |
| 294 | + public DrawPanel() { |
| 295 | + MoveAction action = new MoveAction("onEnter"); |
| 296 | + String ACTION_KEY = "onEnter"; |
| 297 | + KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); |
| 298 | + InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW); |
| 299 | + inputMap.put(W, ACTION_KEY); |
| 300 | + ActionMap actionMap = getActionMap(); |
| 301 | + actionMap.put(ACTION_KEY, action); |
| 302 | + } |
| 303 | + protected void paintComponent(Graphics g) { |
| 304 | + super.paintComponent(g); |
| 305 | + drawCactus(g); |
| 306 | + drawSun(g); |
| 307 | + drawSol(g, 100, 250, 180); |
| 308 | + drawDinausor(g, 400 - jumpY); |
| 309 | + if (gameOver) { |
| 310 | + gameOver(g); |
| 311 | + } |
| 312 | + } |
| 313 | + public Dimension getPreferredSize() { |
| 314 | + return new Dimension(D_W, D_H); |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + private class MyGraph { |
| 319 | + int x_ = 10; |
| 320 | + int y_ = 100; |
| 321 | + int h_ = 60; |
| 322 | + int p_ = 10; |
| 323 | + } |
| 324 | + |
| 325 | + class MoveAction extends AbstractAction { |
| 326 | + public MoveAction(String name) { |
| 327 | + putValue(NAME, name); |
| 328 | + } |
| 329 | + public void actionPerformed(ActionEvent actionEvent) { |
| 330 | + onEnterPresses = true; |
| 331 | + drawPanel.repaint(); |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + public void keyTyped(KeyEvent e) { |
| 337 | + // TODO Auto-generated method stub |
| 338 | + } |
| 339 | + |
| 340 | + @Override |
| 341 | + public void keyPressed(KeyEvent e) { |
| 342 | + if(e.getKeyCode() == KeyEvent.VK_SPACE) { |
| 343 | + if(gameOver){ |
| 344 | + gameOver = false; |
| 345 | + restartGame(getGraphics()); |
| 346 | + } |
| 347 | + } |
| 348 | + } |
| 349 | + |
| 350 | + @Override |
| 351 | + public void keyReleased(KeyEvent e) { |
| 352 | + // TODO Auto-generated method stub |
| 353 | + } |
| 354 | +} |
0 commit comments