//************************************************************************ // You are free to copy this source and use it in any way you deem fit //************************************************************************ // This game is a copy of Load Runner but with lot more flexibility // It provides an editor to create user levels, facility to exchange user // levels with others and also to record and replay the user games along // with the facility to exchange these recordings with other users //************************************************************************ // Classes: // SaveTheCats - Main applet class, provides // - user interface // - timing pulses for continuous motion // - event registration (user activity) // SaveTheCatsDisplay - Game display screen, provides // - drawing functions // - game logic // - event registration for editor // SaveTheCatsItem - For action objects like Damsels and monsters // - store current position and motion // - identifies clash with other items // - provides item movement // SaveTheCatsHoles - it implements fallthrough bricks and the // user created holes while digging // SaveTheCatsRecorder - It records the current play and user levels // //************************************************************************* // Program: Damsels in distress // Programmer: H K Gupta // Version: 1.0 dated 07/30/97 //************************************************************************** import java.awt.*; import java.applet.Applet; public class SaveTheCats extends Applet implements Runnable{ Panel pTop; Label lTitle; Panel pRightCards; Panel pRightStatus; Panel pRightStatusTop; Choice cIcons; Button bEditor; Button bDemo; Button bReplay; Button bShowRecorded; Button bAcceptRecorded; TextArea tRecorded; Panel pRightEditor; // Editor Panel pRightEditorTop; Button bPlay; Button bTryThis; Label lCopyFrom; Choice cCopyLevel; Button bShow; Button bAccept; TextArea tOutput; Panel pLeftCards; Panel pLeftStatus; Label lStringLevel; Label lLevel; Label lStringMenLeft; Label lMenLeft; Label lStringScore; Label lScore; Button bHarakiri; // Sucide option Button bPause; Button bStartAgain; Choice cNewGame; Button bMyLevel; Panel pLeftEditor; // Editor Label lOptions; Button[] bOptions; String[] sOptions = { "Empty", "Brick", "Ladder", "TR Brick", "You", "Monster", "Damsels"}; Label lCurrOption; SaveTheCatsDisplay pMain; boolean levelsLoaded = false; boolean firstTime = true; boolean pauseInEffect = false; Thread runThread = null; public void init() { } public void start() { if (firstTime) { setUpDisplay() ; firstTime = false; } if (runThread == null) { if(!pauseInEffect) { runThread = new Thread(this, "SaveTheCats"); runThread.start(); } } } public void run() { if(!levelsLoaded) { try { // Wait for icons file to load pMain.tracker.waitForAll(); } catch (InterruptedException e) {} pMain.loadLevel(pMain.nLevel); pMain.repaint(); levelsLoaded = true; } while (Thread.currentThread() == runThread) { if(pMain.gameRunning) { pMain.move(); if(pMain.updateStats) updateStats(); } try { Thread.sleep(50); } catch (InterruptedException e){ } } } public void stop() { runThread = null; } void setUpDisplay() { setLayout(new BorderLayout()); pMain = new SaveTheCatsDisplay(getImage(getCodeBase(), "SaveTheCatsImages.gif")); add("Center", pMain); 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); displayTitle(); pRightCards = new Panel(); pRightCards.setLayout(new CardLayout()); add("East", pRightCards); pRightStatus = new Panel(); pRightStatus.setLayout(new GridLayout(0, 1)); pRightCards.add("pRightStatus", pRightStatus); pRightStatusTop = new Panel(); pRightStatusTop.setLayout(new GridLayout(0, 1)); pRightStatus.add(pRightStatusTop); cIcons = new Choice(); for(int i = 0; i < pMain.maxTypesOfIcons; i++) cIcons.addItem(titleNames[i]); pRightStatusTop.add(cIcons); bEditor = addButton(pRightStatusTop, "Editor"); bDemo = addButton(pRightStatusTop, "Demo"); bReplay = addButton(pRightStatusTop, "Replay"); bShowRecorded = addButton(pRightStatusTop, "ShowRec"); bAcceptRecorded = addButton(pRightStatusTop, "AcceptRec"); tRecorded = new TextArea(6, 8); pRightStatus.add(tRecorded); pRightEditor = new Panel(); pRightEditor.setLayout(new GridLayout(0, 1)); pRightCards.add("pRightEditor", pRightEditor); pRightEditorTop = new Panel(); pRightEditorTop.setLayout(new GridLayout(0, 1)); pRightEditor.add(pRightEditorTop); bPlay = addButton(pRightEditorTop, "Play"); bTryThis = addButton(pRightEditorTop, "Try Me"); lCopyFrom = addLabel(pRightEditorTop, "Copy"); cCopyLevel = new Choice(); for(int i = 1; i <= pMain.recorder.getMaxLevels(); i++) cCopyLevel.addItem("Level " + i); pRightEditorTop.add(cCopyLevel); bShow = addButton(pRightEditorTop, "Show"); bAccept = addButton(pRightEditorTop, "AcceptLvl"); tOutput = new TextArea(6, 8); pRightEditor.add(tOutput); pLeftCards = new Panel(); pLeftCards.setLayout(new CardLayout()); add("West", pLeftCards); pLeftStatus = new Panel(); pLeftStatus.setLayout(new GridLayout(0, 1)); pLeftCards.add("pLeftStatus", pLeftStatus); lStringLevel = addLabel(pLeftStatus, "Level"); lLevel = addLabel(pLeftStatus, "" + pMain.nLevel); lStringMenLeft = addLabel(pLeftStatus, "Lives"); lMenLeft = addLabel(pLeftStatus, "" + pMain.nMenLeft); lStringScore = addLabel(pLeftStatus, "Score"); lScore = addLabel(pLeftStatus, "" + pMain.nScore); bHarakiri = addButton(pLeftStatus, "Harakiri"); bPause = addButton(pLeftStatus, "Pause"); bStartAgain = addButton(pLeftStatus, "Start"); cNewGame = new Choice(); for(int i = 1; i <= pMain.recorder.getMaxLevels(); i++) cNewGame.addItem("Level " + i); pLeftStatus.add(cNewGame); bMyLevel = addButton(pLeftStatus, "MyLevel"); pLeftEditor = new Panel(); pLeftEditor.setLayout(new GridLayout(0, 1)); pLeftCards.add("pLeftEditor", pLeftEditor); lOptions = addLabel(pLeftEditor, "Options"); bOptions = new Button[sOptions.length]; for(int i = 0; i < sOptions.length; i++) { bOptions[i] = addButton(pLeftEditor, sOptions[i]); } lCurrOption = addLabel(pLeftEditor, sOptions[pMain.nCurrOption]); validate(); pMain.startNewGame(false); } 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() { if (pMain.nLevel < pMain.recorder.getMaxLevels()) { lLevel.setText("" + pMain.nLevel); } else { lLevel.setText("MY LEVEL"); } if (pMain.nMenLeft <= 0) { lMenLeft.setText("GameOver"); } else { lMenLeft.setText("" + pMain.nMenLeft); } lScore.setText("" + pMain.nScore); } public boolean action(Event e, Object arg) { Object target = e.target; if(target == bHarakiri) { pMain.harakiri(); updateStats(); return true; } if(target == bPause) { if(pauseInEffect) { pauseInEffect = false; bPause.setLabel("Pause"); start(); } else { pauseInEffect = true; bPause.setLabel("Continue"); stop(); } return true; } if(target == bStartAgain) return newGame(pMain.playActual, 1, false); if(target == cNewGame) return newGame(pMain.playActual, cNewGame.getSelectedIndex() + 1, false); if(target == bReplay) return newGame(pMain.playReplay, pMain.recorder.getRecordedLevel(), false); if(target == bDemo) return newGame(pMain.playDemo, 1, false); if(target == bMyLevel) return newGame(pMain.playActual, pMain.recorder.getMaxLevels() + 1, true); if(target == bShowRecorded) return pMain.recorder.showRecordedString(tRecorded); if(target == bAcceptRecorded) return pMain.recorder.acceptRecordedString(tRecorded, pMain.nLevel); if(target == cIcons) { pMain.typeOfIcon = cIcons.getSelectedIndex(); displayTitle(); pMain.iconsCopy(pMain.typeOfIcon); pMain.refresh(); return true; } // Editor if(target == bEditor) { pMain.playType = pMain.playEditor; pMain.gameRunning = false; ((CardLayout)pLeftCards.getLayout()).show(pLeftCards,"pLeftEditor"); ((CardLayout)pRightCards.getLayout()).show(pRightCards,"pRightEditor"); lTitle.setText("H*R*I*D*A*Y*E*S*H S*C*E*N*E*R*Y E*D*I*T*O*R"); pMain.loadLevel(pMain.recorder.getMaxLevels() + 1); pMain.refresh(); return true; } if(target == bPlay) { ((CardLayout)pLeftCards.getLayout()).show(pLeftCards,"pLeftStatus"); ((CardLayout)pRightCards.getLayout()).show(pRightCards,"pRightStatus"); displayTitle(); pMain.packUserLevel(); return newGame(pMain.playActual, 1, false); } if(target == bTryThis) { ((CardLayout)pLeftCards.getLayout()).show(pLeftCards,"pLeftStatus"); ((CardLayout)pRightCards.getLayout()).show(pRightCards,"pRightStatus"); displayTitle(); pMain.packUserLevel(); return newGame(pMain.playActual, pMain.recorder.getMaxLevels() + 1, true); } for(int i = 0; i < sOptions.length; i++) { if(target == bOptions[i]) { pMain.nCurrOption = i; lCurrOption.setText(sOptions[i]); } } if(target == bShow) { pMain.packUserLevel(); pMain.recorder.showMyLevel(tOutput); return true; } if(target == bAccept) { pMain.recorder.acceptMyLevel(tOutput); pMain.loadLevel(pMain.recorder.getMaxLevels() + 1); pMain.refresh(); return true; } if(target == cCopyLevel) { pMain.loadLevel(cCopyLevel.getSelectedIndex() + 1); pMain.updateGrid(); return true; } return false; } String[] titleStrings = {"DAMSELS IN DISTRESS", "CUTE BOYS IN DISTRESS", "FACE THE BULLETS", "SAVE THE BUTTERFLIES"}; String[] titleNames = { "Men", "Ladies", "Kids 1", "Kids 2"}; void displayTitle() { lTitle.setText("HRIDAYESH presents " + titleStrings[pMain.typeOfIcon]); } boolean newGame(int t, int level, boolean flag) { boolean retVal = pMain.newGame(t, level, flag); updateStats(); return retVal; } public boolean keyDown(Event e, int key) { switch (key) { case Event.LEFT : case 'j' : return(pMain.regMotion(1)); case Event.RIGHT: case 'l' : return(pMain.regMotion(2)); case Event.UP : case 'i' : return(pMain.regMotion(3)); case Event.DOWN : case 'k' : return(pMain.regMotion(4)); case ' ' : return(pMain.regMotion(0)); case 'z' : return(pMain.regMotion(5)); case 'x' : return(pMain.regMotion(6)); } return false; } }