//*********************** Copy right information ************************* // FAQ's // Q. Is this program copy right protected? // Ans. No. Even if it was you will copy it anyway so what is the point // Q. Can I use it for commercial purposes? // Ans. You are so funny. // Q. How do You feel about my copying this software? // Ans. We think that you are doing us a great honour by copying this // software. We are not worthy of this honour but we will appreciate // your generousity if you will simply e-mail us a thank you note // Q. What do I do if I find a bug? // Ans. Go to KMart. Buy a bug repellent(exterminator preferred) and kill // the beast. We dont like bugs. They are Yuk!!! // Q. Where is the code documentation? // Ans. You should have asked this question before we wrote this program. // you seriously do not expect us to go back and document the code // which we can barely figure out ourselves. We promise to document // our next code. (Cross our hearts. Seriously) // Q. How was the code tested and to what degree? // Ans. Good you asked as we are proud of that part. The code is guaranteed // to work even in a nuclear bomb attack provided (here is the catch) // the computer on which the software is running is able to handle // the extreme temperature and noise and radiation (you got the picture) // We also tested the software for bullet proofing (we will tell you // about that some other time) // // Disclaimer: Although we do not expect you to use this software in any // mission critical application but if you manage to start a // nuclear warhead missile to republic of china or some other // funny place using this piece of code PLEASE do sue us. // You wont get any money out of us but we will really enjoy // the resulting publicity. Kindly break the news to us in a // gentle way otherwise we may die laughing. // If you sue us nice and proper we all may become rich // from TV and film rights and product endorsements. //************************************************************************ // Program: Catch Them All (A game program for catching floating objects) // Programmer: H K Gupta Date: 3 July 1997 // Updated 20 Aug 97 //************************************************************************ import java.awt.*; import java.applet.Applet; import java.lang.Math; public class CatchThemAll extends Applet implements Runnable{ Panel pCard; Label lCatchThemAll; CatchThemAllAnimal[] animal; // animals int nAnimals = 6; // default no. of animals Panel pStatus; // status panel Button pause; // request pause on game boolean pauseInEffect = false; Panel pLevel ; // Panel for holding level buttons int totalLevels = 6; int minLevel = 4; Label levelLabel; Button[] bLevel; // to select level Button bHelp; Label winString; // Winning String Display Label lAnimal; Panel pSnake; // show percents Label lYouAndMe; HridayeshSnake pResult; Label results; Label lBlank; boolean firstTime = true; boolean newHand = true; int missed = 0; // objects missed int missedPoints = 0; int caught = 0; // objects caught int caughtPoints = 0; Thread runThread = null; public void start() { if (firstTime) { setUpDisplay() ; int i; for(i = 0; i < nAnimals; i++) animal[i].begin(); firstTime = false; } if (runThread == null) { if(!pauseInEffect) { runThread = new Thread(this, "CatchThemAll"); runThread.start(); } } } public void run() { while (Thread.currentThread() == runThread) { int i; for(i = 0; i < nAnimals; i++) { animal[i].move(); } try { Thread.sleep(40); } catch (InterruptedException e){ } } } public void stop() { runThread = null; } void setUpDisplay() { int i; setLayout(new BorderLayout()); lCatchThemAll = new Label("Catch Them All by HRIDAYESH", Label.CENTER); lCatchThemAll.setFont(new Font("TimesRoman",Font.BOLD,18)); lCatchThemAll.setForeground(Color.red); lCatchThemAll.setBackground(Color.yellow); add("North", lCatchThemAll); addAnimals(nAnimals); pStatus = new Panel(); add("East", pStatus); pStatus.setLayout(new GridLayout(0, 1, 1, 1)); levelLabel = new Label("", Label.CENTER); pStatus.add(levelLabel); levelLabel.setText("Level " + nAnimals); pLevel = new Panel(); pLevel.setLayout(new GridLayout(2, 3)); pStatus.add(pLevel); bLevel = new Button[totalLevels]; for(i = 0; i < totalLevels; i++) { bLevel[i] = new Button("L" + (i + minLevel)); pLevel.add(bLevel[i]); } bHelp = new Button("Scoring"); pStatus.add(bHelp); winString = new Label("", Label.CENTER); pStatus.add(winString); lAnimal = new Label("", Label.CENTER); pStatus.add(lAnimal); pause = new Button(); pause.setFont(new Font("TimesRoman",Font.BOLD,18)); pStatus.add(pause); pause.setLabel("Pause"); pSnake = new Panel(); pSnake.setLayout(new BorderLayout()); add("West", pSnake); lYouAndMe = new Label("Caught Missed", Label.CENTER); pSnake.add("North", lYouAndMe); pResult = new HridayeshSnake(); pSnake.add("Center", pResult); results = new Label("", Label.CENTER); pSnake.add("South", results); lBlank = new Label(""); pSnake.add("East", lBlank); validate(); } public boolean action(Event e, Object arg) { Object target = e.target; if(target == pause) { if(pauseInEffect) { pauseInEffect = false; pause.setLabel("Pause"); start(); } else { pauseInEffect = true; pause.setLabel("Continue"); stop(); } return true; } if(target == bHelp) { processHelp(); return true; } int i; for(i = 0; i < totalLevels; i++) { // Level change request if(target == bLevel[i]) { stop(); levelChange(i + minLevel); start(); return true; } } return false; } void levelChange(int newLevel) { if (newLevel == nAnimals) return; caught = 0; missed = 0; int i; for(i = 0; i < nAnimals; i++) { // remove old animals pCard.remove(animal[i]); } remove(pCard); addAnimals(newLevel); levelLabel.setText("Level " + nAnimals); validate(); for(i = 0; i < nAnimals; i++) animal[i].begin(); } void addAnimals(int newLevel) { int i; nAnimals = newLevel; pCard = new Panel(); pCard.setLayout(new GridLayout(0, nAnimals)); add("Center", pCard); animal = new CatchThemAllAnimal[nAnimals]; for(i = 0; i < nAnimals; i++) { animal[i] = new CatchThemAllAnimal(this); pCard.add(animal[i]); } } public void updateScore(int i, String anmName, int points) { if (i < 0) { // missed missed += points; winString.setText("Oops Missed"); } else { caught += points; winString.setText("Yeah Caught"); } lAnimal.setText(anmName); results.setText("" + caught + " " + missed + " "); pResult.updateResult(Color.blue, Color.red, caught, missed); } void processHelp() { HridayeshHelp window = null; try { window = new HridayeshHelp(); } catch (Exception e) { bHelp.setLabel("Error"); bHelp.disable(); return; } window.setTitle("Catch Them All Scoring Method"); window.setText( " *** Scoring Method ***\n" + "Every time you catch an object\n" + "Its value is added to your score\n" + "If the Object floats out of Window\n" + "Its value is added to missed score\n" + "Object Value is\n" + " Level * Type * Color\n" + "Type Ball 1, Rectangle 2\n" + " Donkey 3, Spacecraft 4 \n" + "Color Black 1, Green 2\n" + " Blue 3, Red 4 \n" ); window.pack(); window.show(); } } class CatchThemAllAnimal extends Canvas { int aType = -1; // unspecified boolean direction = true; // animal moves up boolean doubleSpeed = false; int xPrev, yPrev; Color aColor; // Color of Animal int nColor; // numeric index of color int points; // Points for this animal int totalTypes = 4; // type of animals final static String[] anmNames = {"Ball","Rectangle", "Donkey", "Spacecraft"}; int colorTypes = 4; final static Color[] anmColors = {Color.black, Color.green, Color.blue, Color.red}; CatchThemAll controller; int s, sBy2, sBy4, sBy8; // s is the size of animal // double buffering implementation here Dimension offDimension; Image offImage; Graphics o; public CatchThemAllAnimal(CatchThemAll controller) { super(); setBackground(Color.white); this.controller = controller; } public void begin() { Dimension d = size(); xPrev = d.width/2; yPrev = d.height; direction = true; if(((int)(Math.random() * 2)) == 1) { yPrev = 0; direction = false; } aType = (int)(Math.random() * totalTypes); // set up type of animal nColor = (int)(Math.random() * colorTypes); aColor = anmColors[nColor]; points = controller.nAnimals * (aType + 1) * (nColor + 1); } public void update(Graphics g) { Dimension d = size(); if ( (o == null) || (d.width != offDimension.width) || (d.height != offDimension.height) ) { offDimension = d; offImage = createImage(d.width, d.height); o = offImage.getGraphics(); } int x, y; if (d.width < d.height) { s = d.width - 2; } else { s = d.height - 2; } x = d.width / 2; y = d.height / 2; sBy2 = s / 2; sBy4 = s / 4; sBy8 = s / 8; o.setColor(Color.white); o.fillRect(0, 0, d.width, d.height); o.setColor(aColor); switch(aType) { case 0: o.fillOval(xPrev - sBy4, yPrev - sBy4, sBy2, sBy2); break; case 1: o.fillRect(xPrev - sBy4, yPrev - sBy4, sBy2, sBy2); break; case 2: o.fillRect(xPrev - sBy4, yPrev - sBy4, sBy2 - sBy8, sBy4 + (int)(sBy8 / 2)); o.fillRect(xPrev - sBy4, yPrev, 2, sBy4); o.fillRect(xPrev + sBy8 - 2, yPrev, 2, sBy4); o.fillRect(xPrev + sBy8, yPrev - sBy4 + 2, sBy8, 2); o.fillRect(xPrev + sBy8 + (int)(sBy8 / 2), yPrev - sBy4, sBy8, sBy4); break; case 3: o.fillOval(xPrev - sBy4, yPrev, sBy2, sBy4); o.drawOval(xPrev - sBy8, yPrev - sBy8, sBy4, sBy4); o.drawLine(xPrev, yPrev, xPrev - sBy4, yPrev + sBy4); o.drawLine(xPrev, yPrev, xPrev + sBy4, yPrev + sBy4); break; default:o.drawOval(xPrev - 3, yPrev, 6, 6); } //Paint the image onto the screen. g.drawImage(offImage, 0, 0, this); } public void paint(Graphics g) { update(g); } public void move() { Dimension d = size(); if ((yPrev < -10) || (yPrev > (d.height + 10))) { controller.updateScore(-1, anmNames[aType], points); begin(); } if (direction) { // move up yPrev--; if (doubleSpeed) yPrev--; } else { yPrev++; if (doubleSpeed) yPrev++; } // change direction randomly if (((int)(Math.random() * 75)) == 50) { direction = !direction; if(((int)(Math.random() * 5)) == 3) doubleSpeed = !doubleSpeed; } repaint(); } public boolean mouseDown(java.awt.Event evt, int x, int y) { int dist; Dimension d = size(); if (((xPrev - sBy4) < x) && ((xPrev + sBy4) > x)) { if (((yPrev - sBy4) < y) && ((yPrev + sBy4) > y)) { controller.updateScore(1, anmNames[aType], points); begin(); } } return true; } }