//*************************************** // Java script of BlockOut! // << Block.java >> //======================================= // Ver 0.2 96/03/24 T.Yamazaki // avoid mouseMove & thread conflict // Ver 0.1 96/03/17 T.Yamazaki // getCodeBase->getDocumentBase // Ver 0.0 96/02/25 T.Yamazaki //--------------------------------------- // email : yamaza@st.rim.or.jp //*************************************** import java.awt.*; import java.lang.*; import java.applet.*; //=================== // BlockOut! Applet //=================== public class Block extends Applet implements Runnable { //------------------------ // member for this applet //------------------------ Graphics g; Thread theBall; int blockState[][] = new int[10][8]; int pP, pC; AudioClip sWall, sBlock, sPad, sOhmy, sOver; int ballX, ballY; int ballDX,ballDY; int speed; int eraseCount; int padHitCount; int score; int left; boolean onFight; boolean onPlay; Panel pControl; Label lScore, lLeft; Button bControl; int threadProcessing; //------------ // initialize //------------ public void init() { // // set this applet viewing // resize(207, 307 + 34); setLayout(new BorderLayout()); setBackground(Color.yellow); g = getGraphics(); pControl = new Panel(); pControl.setBackground(Color.gray); pControl.setLayout(new FlowLayout()); bControl = new Button(" GO "); lScore = new Label("SCORE:0 "); lLeft = new Label("LEFT:5"); pControl.add(bControl); pControl.add(lScore); pControl.add(lLeft); add("South", pControl); } //------- // start //------- public void start() { // // get audio data // sWall = getAudioClip(getDocumentBase(),"500.au"); sBlock = getAudioClip(getDocumentBase(),"2000.au"); sPad = getAudioClip(getDocumentBase(),"1000.au"); sOhmy = getAudioClip(getDocumentBase(),"ohmygod.au"); sOver = getAudioClip(getDocumentBase(),"gameover.au"); sWall.play(); sBlock.play(); sPad.play(); gameInit(); onFight = false; onPlay = false; System.out.println("START"); } //--------------------- // game initialization //--------------------- void gameInit() { // // set block state // for (int x = 0 ; x <= 9 ; x++) for (int y = 0 ; y <= 7 ; y++) blockState[x][y] = 1; // // set intial pad position // pP = 100; pC = 100; // // score initialization // score = 0; addScore(0); left = 5; subLeft(0); eraseCount = 0; bControl.setLabel(" GO "); threadProcessing = 0; repaint(); } //------- // paint //------- public void paint(Graphics g) { g.drawRect(0, 0, 206, 306); g.setColor(Color.blue); for (int y = 0 ; y <= 7 ; y++) { for (int x = 0 ; x <= 9 ; x++) { if (blockState[x][y] == 1) g.fillRect(6 + x * 20 , 46 + y * 10 , 15, 5); } } } //--------------- // remake blocks //--------------- public void remakeBlock() { g.setColor(Color.blue); for (int x = 0 ; x <= 9 ; x++) for (int y = 0 ; y <= 7 ; y++) { blockState[x][y] = 1; g.fillRect(6 + x * 20 , 46 + y * 10 , 15, 5); } } //------------------ // action handler //------------------ public boolean action(Event e, Object o) { if (e.target instanceof Button) { if (" GO " .equals(o)) { if (onFight == false) { gameInit(); bControl.setLabel("STOP"); onFight = true; } } else if ("STOP" .equals(o)) { stop(); bControl.setLabel(" GO "); onFight = false; onPlay = false; } return true; } return false; } //------------------ // mouse down event //------------------ public boolean mouseDown(java.awt.Event e, int x, int y) { if (onFight == true) { if (onPlay == false) { ballX = (pC / 10) * 10 + 1; if (ballX > 196) ballX = 196; else if (ballX < 6) ballX = 6; if (pC < 100) ballDX = -5; else ballDX = 5; ballY = 281; ballDY = -10; speed = 50; padHitCount = 0; subLeft(1); threadProcessing = 0; onPlay = true; if (theBall != null) { theBall = null; } if (theBall == null) { theBall = new Thread(this); theBall.start(); } } } return true; } //------------------ // mouse move event //------------------ public boolean mouseMove(java.awt.Event e, int x, int y) { pC = x - 7; if (pC < 1) pC = 1; if (pC > 206 - 20) pC = 186; while (threadProcessing == 1) {} g.setColor(Color.yellow); // for erase g.fillRect(1, 286, 205 , 5); g.setColor(Color.green); // for draw g.fillRect(pC, 286, 20 , 5); g.setColor(Color.yellow); // for default return true; } //------------------ // stop this applet //------------------ public void stop() { if (theBall != null) { theBall.stop(); theBall = null; } } //----- // run //----- public void run() { int ballPX,ballPY; while(true) { // // keep current ball position // ballPX = ballX; ballPY = ballY; // // move ball by delta value // ballX = ballX + ballDX; ballY = ballY + ballDY; // // hit pad ? // if ((ballY == 281) && (ballX >= pC - 5) && (ballX <= pC + 20)) { sPad.play(); int padPos = ballX + 5 - pC; if (padPos <= 5) ballDX = -5; else if (padPos <= 10) ballDX = ballDX; else if (padPos <= 15) ballDX = ballDX; else ballDX = 5; ballDY = -ballDY; padHitCount = padHitCount + 1; if (padHitCount <= 5) speed = 50; else if (padHitCount <= 30) speed = 30; else if (padHitCount <= 50) speed = 25; else speed = 15; } // // hit wall ? // if ((ballX >= 201) || (ballX <= 1)) { ballDX = -ballDX; sWall.play(); } if (ballY <= 1) { ballDY = -ballDY; sWall.play(); } // // miss ? // if (ballY >= 296) { if (left == 0) { sOver.play(); onFight = false; bControl.setLabel(" GO "); } else { sOhmy.play(); } g.setColor(Color.yellow); // for erase g.fillRect(ballPX, ballPY, 5 , 5); onPlay = false; theBall.stop(); } // // hit block ? // if (ballDY > 0) // if ball down { if ((ballY >= 46 - 5) && (ballY <= 116 - 5)) { int blockX = ballX / 20; if ((ballX - blockX * 20) >= 5) { int blockY = (ballY - (46 - 5)) / 10; if (blockState[blockX][blockY] == 1) { sBlock.play(); blockState[blockX][blockY] = 0; g.setColor(Color.yellow); // for erase g.fillRect(6 + blockX * 20 , 46 + blockY * 10 , 15, 5); ballDY = -ballDY; eraseCount = eraseCount + 1; addScore(2); } } } } else // if ball up { if ((ballY >= 46 + 5) && (ballY <= 116 + 5)) { int blockX = ballX / 20; if ((ballX - blockX * 20) >= 5) { int blockY = (ballY - (46 + 5)) / 10; if (blockState[blockX][blockY] == 1) { sBlock.play(); blockState[blockX][blockY] = 0; g.setColor(Color.yellow); // for erase g.fillRect(6 + blockX * 20 , 46 + blockY * 10 , 15, 5); ballDY = -ballDY; eraseCount = eraseCount + 1; addScore(1); } } } } // // erase and draw ball for moving // threadProcessing = 1; g.setColor(Color.yellow); // for erase g.fillRect(ballPX, ballPY, 5 , 5); g.setColor(Color.red); // for draw g.fillRect(ballX, ballY, 5 , 5); g.setColor(Color.yellow); // for default threadProcessing = 0; // // if all blocks has gone... // if (eraseCount == 80) { remakeBlock(); eraseCount = 0; } // // set ball speed by thread sleep period // try { Thread.sleep(speed); } catch(InterruptedException e) { System.out.println("Catch Interrupt Exception"); } } } //------------------------------ // add score and update display //------------------------------ void addScore(int delta) { String str; score = score + delta; str = "SCORE:" + String.valueOf(score); lScore.setText(str); } //------------------------------ // sub left and update display //------------------------------ void subLeft(int delta) { String str; left = left - delta; str = "LEFT:" + String.valueOf(left); lLeft.setText(str); } }