hyderman
Posts: 11
Nickname: salem99
Registered: Oct, 2002
trying to update this applet
Posted: Mar 15, 2003 11:31 PM
Advertisement
hi, i am trying to update this program in order to write an applet to play the "Cat and mouse" game. In this game, the user is a "mouse" who has to find their way safely to a mousehole drawn in the middle of the top of the applet pane. Several cats appear at random positions in the pane and will try to catch the mouse by chasing after it. You lose the game if the cat gets too close to your mouse. You win if you get to the hole before they get near you. here is mainpart of the program import java.applet.*; import java.awt.*; import java.awt.event.*; public class CatMouseChase extends Applet implements ActionListener { private Cat myCat; private Mouse myMouse; private Statue myStatue; private Position myOldCatPosition; private boolean chaseIsOver = false; private int timeElapsed = 0; private final int TIME_LIMIT = 30; private Button nextStep; private String statusString = "Not yet started."; private final int BORDER = 50; // margin for buttons and status strings private static int ourWindowWidth, ourWindowHeight; private int myXCenter, myYCenter, myUnit; private final int WAIT_TIME = 500; //milliseconds public void init ( ) { super.init ( ); nextStep = new Button ("Move once"); add (nextStep); nextStep.addActionListener (this); ourWindowWidth = getIntegerParam ("width"); ourWindowHeight = getIntegerParam ("height"); myXCenter = ourWindowWidth/2; myYCenter = ourWindowHeight/2; int smallestDimension = ourWindowWidth < ourWindowHeight? ourWindowWidth: ourWindowHeight; double statueRadius = getDoubleParam("statueradius"); double mouseAngle = getDoubleParam("mouseangle"); double catRadius = getDoubleParam("catradius"); double catAngle = getDoubleParam("catangle"); myStatue = new Statue(statueRadius); myUnit = (smallestDimension/2 - BORDER)/((int) (catRadius+myStatue.getRadius())); myCat = new Cat (new Position (catRadius, catAngle)); myOldCatPosition = myCat.getPosition(); myMouse = new Mouse (new Position (myStatue.getRadius(), mouseAngle)); statusString = "Chase is in progress."; } private int getIntegerParam (String name) { String value = this.getParameter (name); if (value == "") { return 0; } try { return Integer.parseInt(value); } catch (Exception e) { return 0; } } private double getDoubleParam (String name) { String value = this.getParameter (name); if (value == "") { return 0.0; } try { return Double.valueOf (value).doubleValue ( ); } catch (Exception e) { return 0.0; } } public void actionPerformed (ActionEvent event) { //students complete rest of method repaint(); } public void paint (Graphics g) { // The magic numbers used here should really depend on the height of the // type face used to display the window annotations. // We'll fix that for next semester. g.drawOval (myXCenter+5-myUnit, myYCenter+5-myUnit, 2*myUnit-10, 2*myUnit-10); g.drawString ("CO", myOldCatPosition.xCoord (myXCenter, myUnit)-5, myOldCatPosition.yCoord (myYCenter, myUnit)+5 ); g.drawString ("C", myCat.getPosition ( ).xCoord (myXCenter, myUnit)-5, myCat.getPosition ( ).yCoord (myYCenter, myUnit)+5 ); g.drawString ("M", myMouse.getPosition ( ).xCoord (myXCenter, myUnit)-5, myMouse.getPosition ( ).yCoord (myYCenter, myUnit)+5 ); g.drawString ("Mouse at " + myMouse.getPosition ( ) + "; cat at " + myCat.getPosition ( ), 50, ourWindowHeight-30); g.drawString (statusString, 50, ourWindowHeight-15); } public void pause() { try {Thread.sleep(WAIT_TIME); } catch(Exception e) {} } }