board.java
1package breakout; 2 3import javax.swing.JPanel; 4 5 6 7import javax.swing.Timer; 8import java.awt.color.*; 9import java.awt.Font; 10import java.awt.Color; 11import java.awt.Dimension; 12import java.awt.FontMetrics; 13import java.awt.Graphics2D; 14import java.awt.Graphics; 15import java.awt.Point; 16import java.awt.RenderingHints; 17import java.awt.Toolkit; 18import java.awt.event.KeyEvent; 19import java.awt.event.ActionEvent; 20import java.awt.event.KeyAdapter; 21import java.awt.event.ActionListener; 22import java.awt.event.KeyListener; 23 24public class Board extends JPanel { 25 private Timer timer; 26 private String message = "GAME OVER"; 27 private ball Ball; 28 private Paddle paddle; 29 private Brick[] bricks; 30 private boolean Game = true;//run the game or not 31 private TAdapter keyAdapter; 32 private int score = 0; 33 private boolean playing = true; 34 35 36 public Board(){ 37 initBoard(); 38 39 } 40 41 private void initBoard() { 42 43 this.keyAdapter = new TAdapter(); 44 addKeyListener(this.keyAdapter); 45 setFocusable(true); 46 setPreferredSize(new Dimension (Commons.width, Commons.height)); 47 gameInit(); 48 } 49 50 private void gameInit() { 51 bricks = new Brick[Commons.totalBricks]; 52 Ball = new ball(); 53 paddle = new Paddle(); 54 55 int k = 0; 56 57 for (int i =0; i < 5; i++) { 58 for (int j = 0; j < 6; j++) { 59 bricks[k] = new Brick(j*60+30, i*25+80);//place the bricks on the panel 60 k++; 61 } 62 } 63 timer = new Timer(Commons.period, new GameCycle());//perform actions 64 timer.start(); 65 66 } 67 68 69 @Override 70 public void paintComponent(Graphics g) { 71 super.paintComponent(g); 72 73 var g2d = (Graphics2D) g; 74 75 76 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 77 g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 78 79 if(Game) { 80 drawObjects(g2d);//drawing all objects in the game 81 if(!playing) { 82 pausedScreen(g2d);//ここでポーズ画面を表示させたい 83 } 84 } 85 86 else { 87 gameFinished(g2d);//display result 88 } 89 90 91 Toolkit.getDefaultToolkit().sync(); 92 } 93 94 private void drawObjects(Graphics2D g2d) {//基本的なゲーム画面 95 g2d.drawImage(Ball.getImage(), Ball.getX(), Ball.getY(), Ball.getImageWidth(), Ball.getImageHeight(), this); 96 g2d.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(), paddle.getImageWidth(), paddle.getImageHeight(), this); 97 98 g2d.setColor(Color.black); 99 g2d.setFont(new Font("Monospaced",Font.BOLD, 25)); 100 g2d.drawString("Score: "+score, 30,40); 101 102 for (int i =0; i<Commons.totalBricks; i++) { 103 104 if (!bricks[i].isDestroyed()) { 105 g2d.drawImage(bricks[i].getImage(), bricks[i].getX(), bricks[i].getY(), bricks[i].getImageWidth(), bricks[i].getImageHeight(), this); 106 107 } 108 } 109 110 } 111 112 private void pausedScreen(Graphics2D g2d) {//ポーズ画面 113 g2d.setColor(Color.black); 114 g2d.setFont(new Font("Monospaced",Font.BOLD,25)); 115 g2d.drawString("PAUSE",200,200); 116 } 117 118 119 private void gameFinished(Graphics2D g2d) { 120 //called when a player failed 121 var font = new Font("Verdana", Font.BOLD, 18); 122 FontMetrics fontMetrics = this.getFontMetrics(font); 123 124 String scoreMessage = "Score: "+score; 125 String restartMessage = "Press ENTER to restart"; 126 g2d.setColor(Color.black); 127 g2d.setFont(font); 128 129 g2d.drawString(message,(Commons.width - fontMetrics.stringWidth(message))/2, Commons.width/2); 130 g2d.drawString(scoreMessage, (Commons.width - fontMetrics.stringWidth(scoreMessage))/2,250); 131 g2d.drawString(restartMessage,(Commons.width - fontMetrics.stringWidth(restartMessage))/2,300); 132 133 addKeyListener(this.keyAdapter); 134 } 135 136 137 138 //key listeners 139 private class TAdapter extends KeyAdapter { 140 @Override 141 public void keyReleased(KeyEvent e) { 142 paddle.keyReleased(e);//perform when key released 143 } 144 145 @Override 146 public void keyPressed(KeyEvent e) { 147 if(e.getKeyCode()== KeyEvent.VK_ESCAPE){ 148 if(Game) { 149 if(playing) { 150 timer.stop(); 151 playing = false; 152 } 153 else if(!playing) { 154 timer.restart(); 155 playing =true; 156 } 157 158 } 159 } 160 paddle.keyPressed(e);//move the paddle when the key is pressed 161 162 //press enter to restart and initialize the score 163 if (e.getKeyCode() == KeyEvent.VK_ENTER) { 164 if (!Game) { 165 Game = true; 166 score = 0; 167 168 timer.setRepeats(false); 169 initBoard(); 170 } 171 172 } 173 174 } 175 } 176 177 private class GameCycle implements ActionListener{ 178 @Override 179 public void actionPerformed(ActionEvent e) { 180 doGameCycle(); 181 } 182 } 183 184 185 private void doGameCycle() { 186 187 //call all movements 188 Ball.ballmove(); 189 paddle.move(); 190 checkCollision(); 191 repaint(); 192 193 } 194 private void stopGame() { 195 //stop the game 196 Game = false; 197 timer.stop(); 198 199 } 200 201 private void checkCollision() { 202 if (Ball.getRect().getMaxY()>Commons.bottomEdge) { 203 //Game Over 204 stopGame(); 205 } 206 207 for (int i = 0,j = 0; i<Commons.totalBricks; i++) { 208 if (bricks[i].isDestroyed()) { 209 j++; 210 } 211 212 if (j==Commons.totalBricks) { 213 message = "Victory"; 214 stopGame(); 215 } 216 }
0 コメント