|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.mycompany.brick; |
| 7 | + |
| 8 | +import javax.swing.JPanel; |
| 9 | +import javax.swing.Timer; |
| 10 | +import java.awt.Graphics2D; |
| 11 | +import java.awt.Rectangle; |
| 12 | +import java.awt.Graphics; |
| 13 | +import java.awt.Color; |
| 14 | +import java.awt.Font; |
| 15 | +import java.awt.event.ActionEvent; |
| 16 | +import java.awt.event.ActionListener; |
| 17 | +import java.awt.event.KeyEvent; |
| 18 | +import java.awt.event.KeyListener; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * @author chinm |
| 23 | + */ |
| 24 | +public class GamePlay extends JPanel implements KeyListener, ActionListener { |
| 25 | + |
| 26 | + private boolean play = false; |
| 27 | + private int score = 0; |
| 28 | + private int totalbricks = 21; |
| 29 | + private Timer Timer; |
| 30 | + private int delay = 8; |
| 31 | + private int playerX = 310; |
| 32 | + private int ballposX = 120; |
| 33 | + private int ballposY = 350; |
| 34 | + private int ballXdir = -1; |
| 35 | + private int ballYdir = -2; |
| 36 | + private MapGenerator map; |
| 37 | + |
| 38 | + public GamePlay() { |
| 39 | + map = new MapGenerator(3, 7); |
| 40 | + addKeyListener(this); |
| 41 | + setFocusable(true); |
| 42 | + setFocusTraversalKeysEnabled(false); |
| 43 | + Timer = new Timer(delay, this); |
| 44 | + Timer.start(); |
| 45 | + } |
| 46 | + |
| 47 | + public void paint(Graphics g) { |
| 48 | + g.setColor(Color.black); |
| 49 | + g.fillRect(1, 1, 692, 592); |
| 50 | + |
| 51 | + map.draw((Graphics2D) g); |
| 52 | + |
| 53 | + g.setColor(Color.yellow); |
| 54 | + g.fillRect(0, 0, 3, 592); |
| 55 | + g.fillRect(0, 0, 692, 3); |
| 56 | + g.fillRect(691, 0, 3, 592); |
| 57 | + |
| 58 | + g.setColor(Color.white); |
| 59 | + g.setFont(new Font("serif", Font.BOLD, 25)); |
| 60 | + g.drawString("" + score, 590, 30); |
| 61 | + |
| 62 | + g.setColor(Color.yellow); |
| 63 | + g.fillRect(playerX, 550, 100, 8); |
| 64 | + |
| 65 | + //ball |
| 66 | + g.setColor(Color.GREEN); |
| 67 | + g.fillOval(ballposX, ballposY, 20, 20); |
| 68 | + |
| 69 | + if (ballposY > 570) { |
| 70 | + play = false; |
| 71 | + ballXdir = 0; |
| 72 | + ballYdir = 0; |
| 73 | + g.setColor(Color.red); |
| 74 | + g.setFont(new Font("serif", Font.BOLD, 30)); |
| 75 | + g.drawString(" Game Over Score: " + score, 190, 300); |
| 76 | + |
| 77 | + g.setFont(new Font("serif", Font.BOLD, 30)); |
| 78 | + g.drawString(" Press Enter to Restart", 190, 340); |
| 79 | + } |
| 80 | + if(totalbricks == 0){ |
| 81 | + play = false; |
| 82 | + ballYdir = -2; |
| 83 | + ballXdir = -1; |
| 84 | + g.setColor(Color.red); |
| 85 | + g.setFont(new Font("serif",Font.BOLD,30)); |
| 86 | + g.drawString(" Game Over: "+score,190,300); |
| 87 | + |
| 88 | + g.setFont(new Font("serif", Font.BOLD, 30)); |
| 89 | + g.drawString(" Press Enter to Restart", 190, 340); |
| 90 | + |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + g.dispose(); |
| 95 | + |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void actionPerformed(ActionEvent e) { |
| 101 | + Timer.start(); |
| 102 | + |
| 103 | + if (play) { |
| 104 | + if (new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) { |
| 105 | + ballYdir = -ballYdir; |
| 106 | + } |
| 107 | + |
| 108 | + A: |
| 109 | + for (int i = 0; i < map.map.length; i++) { |
| 110 | + for (int j = 0; j < map.map[0].length; j++) { |
| 111 | + if (map.map[i][j] > 0) { |
| 112 | + int brickX = j * map.bricksWidth + 80; |
| 113 | + int brickY = i * map.bricksHeight + 50; |
| 114 | + int bricksWidth = map.bricksWidth; |
| 115 | + int bricksHeight = map.bricksHeight; |
| 116 | + |
| 117 | + Rectangle rect = new Rectangle(brickX, brickY, bricksWidth, bricksHeight); |
| 118 | + Rectangle ballrect = new Rectangle(ballposX, ballposY, 20, 20); |
| 119 | + Rectangle brickrect = rect; |
| 120 | + |
| 121 | + if (ballrect.intersects(brickrect)) { |
| 122 | + map.setBricksValue(0, i, j); |
| 123 | + totalbricks--; |
| 124 | + score += 5; |
| 125 | + if (ballposX + 19 <= brickrect.x || ballposX + 1 >= brickrect.x + bricksWidth) { |
| 126 | + ballXdir = -ballXdir; |
| 127 | + } else { |
| 128 | + ballYdir = -ballYdir; |
| 129 | + } |
| 130 | + break A; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + ballposX += ballXdir; |
| 140 | + ballposY += ballYdir; |
| 141 | + if (ballposX < 0) { |
| 142 | + ballXdir = -ballXdir; |
| 143 | + } |
| 144 | + if (ballposY < 0) { |
| 145 | + ballYdir = -ballYdir; |
| 146 | + } |
| 147 | + if (ballposX > 670) { |
| 148 | + ballXdir = -ballXdir; |
| 149 | + } |
| 150 | + } |
| 151 | + repaint(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void keyTyped(KeyEvent e) { |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + @Override |
| 161 | + public void keyReleased(KeyEvent e) { |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void keyPressed(KeyEvent e) { |
| 167 | + if (e.getKeyCode() == KeyEvent.VK_RIGHT) { |
| 168 | + if (playerX >= 600) { |
| 169 | + playerX = 600; |
| 170 | + } else { |
| 171 | + moveRight(); |
| 172 | + } |
| 173 | + } |
| 174 | + if (e.getKeyCode() == KeyEvent.VK_LEFT) { |
| 175 | + if (playerX < 10) { |
| 176 | + playerX = 10; |
| 177 | + } else { |
| 178 | + moveLeft(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
| 183 | + if (!play) { |
| 184 | + ballposX = 120; |
| 185 | + ballposY = 350; |
| 186 | + ballXdir = -1; |
| 187 | + ballYdir = -2; |
| 188 | + score = 0; |
| 189 | + playerX = 310; |
| 190 | + totalbricks = 21; |
| 191 | + map = new MapGenerator(3, 7); |
| 192 | + |
| 193 | + repaint(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + public void moveRight () |
| 201 | + { |
| 202 | + play = true; |
| 203 | + playerX += 20; |
| 204 | + } |
| 205 | + public void moveLeft () |
| 206 | + { |
| 207 | + play = true; |
| 208 | + playerX -= 20; |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +} |
0 commit comments