//************************************************************************ // You are free to copy this source and use it in any way you deem fit //************************************************************************ // Developed this game just to verify that the Monsters animation can be // used as a backdrop // // Program: FlipFlop Monsters // Programmer: H K Gupta //************************************************************************ import java.awt.*; import java.applet.Applet; public class FlipFlop extends Applet implements Runnable{ Panel pTop; Label lTitle; Panel pRightStatus; TextArea tHelp; Panel pLeftStatus; Label lStringScore; Label lScore; Button bNewGame; Label lYouWon; FlipFlopDisplay pMain; boolean firstTime = true; public void init() { } public void start() { if(firstTime) { setUpDisplay(); firstTime = false; } pMain.start(); } public void run() { } public void stop() { pMain.stop(); } void setUpDisplay() { sunBuffer = createImage(sunW, sunW); sunG = sunBuffer.getGraphics(); FFGrid = new boolean[3][3]; setLayout(new BorderLayout()); pTop = new Panel(); pTop.setLayout(new GridLayout(0, 1)); add("North", pTop); lTitle = addLabel(pTop, ""); lTitle.setFont(new Font("TimesRoman", Font.BOLD, 20)); lTitle.setForeground(Color.red); lTitle.setBackground(Color.yellow); lTitle.setText("Flip Flop Talking sun & Monsters by HRIDAYESH"); pRightStatus = new Panel(); pRightStatus.setLayout(new GridLayout(0, 1)); add("East", pRightStatus); tHelp = new TextArea(6, 11); tHelp.setText( " FLIP FLOP " + "\n " + "\nExpose the " + "\nmonsters by" + "\nclicking on" + "\ntalking suns" + "\n but " + "\n BEWARE " + "\nThe suns " + "\non either " + "\nside also " + "\nflip to make" + "\nyour life " + "\ndifficult. " + "\n " + "\n***********" + "\n IDEAL " + "\n HARDWARE " + "\n CRAY " + "\nSuper Computer" + "\nGood Luck "); tHelp.setEditable(false); pRightStatus.add(tHelp); pLeftStatus = new Panel(); pLeftStatus.setLayout(new GridLayout(0, 1)); add("West", pLeftStatus); lStringScore = addLabel(pLeftStatus, "Moves"); lScore = addLabel(pLeftStatus, "0"); bNewGame = addButton(pLeftStatus, "Restart"); lYouWon = addLabel(pLeftStatus, ""); pMain = new FlipFlopDisplay(this); add("Center", pMain); validate(); setupNewGame(); } Button addButton(Panel p, String s) { Button b = new Button(s); p.add(b); return b; } Label addLabel(Panel p, String s) { Label l = new Label(s, Label.CENTER); p.add(l); return l; } void updateStats() { lScore.setText("" + noOfMoves); if(youWon) { lYouWon.setText("You Won"); } else { lYouWon.setText(""); } } public boolean action(Event e, Object arg) { Object target = e.target; if(target == bNewGame) { setupNewGame(); return true; } return false; } // Game logic int noOfMoves = 0; boolean[][] FFGrid; boolean youWon = false; void setupNewGame() { youWon = true; while(youWon) setNew(); // Avoid the probability of a won game at start pMain.refresh(); } private void setNew() { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(((int)(Math.random() * 2)) == 1) { FFGrid[i][j] = true; } else { FFGrid[i][j] = false; } } } noOfMoves = 0; haveYouWon(); updateStats(); } private void haveYouWon() { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(FFGrid[i][j]) { youWon = false; return; } } } youWon = true; } int frameNo = 0; int sunW = 67; Image sunBuffer; Graphics sunG; void maskBuffer(Graphics o) { Dimension d = pMain.size(); int xOffset = (d.width - d.height) / 2; int cellWidth = d.height / 3; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { int x = xOffset + i * cellWidth + (cellWidth - sunW) / 2; int y = j * cellWidth + (cellWidth - sunW) / 2; if(FFGrid[i][j]) { int sX = (frameNo % 5) * sunW; int sY = (frameNo / 5) * sunW; sunG.drawImage(pMain.offImage, - x, - y , this); sunG.drawImage(pMain.sunImage, - sX, - sY, this); o.drawImage(sunBuffer, x, y, sunW, sunW, this); } o.setColor(Color.red); o.drawRect(x, y, sunW, sunW); } } frameNo++; if(frameNo >= 25) frameNo = 0; } public boolean pMainMouseDown(Event event, int x, int y) { Dimension d = pMain.size(); int xOffset = (d.width - d.height) / 2; int cellWidth = d.height / 3; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if((x > (xOffset + i * cellWidth)) && (x < (xOffset + (i + 1) * cellWidth)) && (y > (j * cellWidth)) && (y < ((j + 1) * cellWidth))) { flip(i + 1, j); flip(i - 1, j); flip(i, j+ 1); flip(i, j - 1); flip(i, j); noOfMoves++; updateStats(); } } } haveYouWon(); pMain.refresh(); return false; } void flip(int i, int j) { if((i < 0) || (i > 2)) return; if((j < 0) || (j > 2)) return; FFGrid[i][j] = !FFGrid[i][j]; } } class FlipFlopDisplay extends SaveTheCatsDisplay implements Runnable{ private FlipFlop controller; Image sunImage; public FlipFlopDisplay(FlipFlop controller) { super(controller.getImage(controller.getCodeBase(), "SaveTheCatsImages.gif")); sunImage = controller.getImage(controller.getCodeBase(), "FlipFlopSun.gif"); //tracker.addImage(sunImage, 0); this.controller = controller; } void maskBuffer(Graphics o) { controller.maskBuffer(o); } public boolean mouseDown(Event event, int x, int y) { return(controller.pMainMouseDown(event, x, y)); } // ANIMATION RELATED METHODS private boolean levelsLoaded = false; private boolean firstTime = true; private boolean pauseInEffect = false; private Thread runThread = null; public void start() { if (runThread == null) { if(!pauseInEffect) { runThread = new Thread(this, "FlipFlopDisplay"); //runThread.setPriority(Thread.MIN_PRIORITY); runThread.start(); } } } public void run() { if (firstTime) { playNextLevel(); firstTime = false; } if(!levelsLoaded) { try { tracker.waitForAll(); } catch (InterruptedException e) {} loadLevel(nLevel); repaint(); levelsLoaded = true; } while (Thread.currentThread() == runThread) { if(gameRunning) { move(); } else { playNextLevel(); } try { Thread.sleep(100); } catch (InterruptedException e){ } } } public void stop() { runThread = null; } private String[] sRecordings = { // Level 1 "39,54,18,39,34,17,20,43,26,36,0,27,9,36,33,1,44,19" + ",0,9,18,3,42,11,0,22,9,26,17,10,10,3,41,5,0,105,9," + "48,25,27,18,34,34,1,52,12,0,5,18,3,50,8,0,17,18,22" + ",9,3,49,8,0,106,18,47,26,60,0,109,9,38,33,25,12,40" + ",25,5,19,8,26,0,0,", // Level 2 "20,3,18,45,26,1,3,185,0,6,18,220,26,51,19,4,0,57,9" + ",5,0,13,18,10,0,13,9,5,0,13,18,28,0,6,9,3,41,17,0," + "21,9,0,0,", // Level 3 "30,5,9,3,1,131,0,13,18,10,0,93,9,1,0,45,27,54,0,6," + "18,47,34,18,20,25,34,53,20,1,0,9,27,45,18,45,27,37" + ",0,31,9,66,25,16,0,101,18,45,27,45,0,21,9,41,33,10" + "1,12,15,25,31,19,0,0,", // Level 4 "55,77,9,128,0,90,18,13,34,74,20,1,2,68,0,1,9,10,0," + "1,9,11,0,1,9,4,0,8,27,24,11,1,1,8,0,9,27,4,0,14,9," + "24,25,38,11,8,33,4,4,151,0,68,9,21,25,12,18,62,10," + "18,33,69,28,29,19,57,34,108,20,76,0,53,27,135,0,14" + "1,9,4,0,53,36,114,0,53,27,107,18,27,34,30,20,5,34," + "12,12,10,25,17,11,57,17,1,2,15,0,18,9,8,33,15,12,0" + ",0,", // Level 5 "12,64,9,4,1,127,0,52,18,1,50,11,0,4,18,16,34,29,20" + ",4,42,8,0,66,9,6,18," }; private int currRecording = 1; private TextArea tRec = new TextArea(""); void playNextLevel() { if(currRecording > sRecordings.length) currRecording = 1; tRec.setText(sRecordings[currRecording - 1]); recorder.acceptRecordedString(tRec, currRecording); newGame(playReplay, currRecording, false); currRecording++; } }