?? fallingstars.java
字號:
import java.applet.*;
import java.awt.*;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
public class FallingStars extends Applet
implements Runnable
{
Thread runner;
MediaTracker tracker;
Image star;
int starX[];
int starY[];
int starSize[];
int starXAdd[];
int starYAdd[];
int maxStars;
int numberOffStars;
int numberOffShips;
int hiscore[];
int score;
int level;
boolean exploding;
int tempInt;
int tempInt2;
AudioClip explosion;
AudioClip shipExploding;
AudioClip alert;
AudioClip laser;
Image offscreenI;
Graphics offscreenG;
Image ship;
int shipX;
Image shipexplode;
boolean shiphit;
boolean started;
boolean starting;
String startString;
Font f;
FontMetrics fm;
String introText1;
String introText2;
String introText3;
String introText4;
String introText5;
String introText6;
String introText7;
String introText8;
String introText9;
String introText10;
String introText11;
String introText12;
String introText13;
String introText14;
String introText15;
String introText16;
String introText17;
String levelText1;
String levelText2;
String levelText3;
String levelText4;
String levelText5;
String levelText6;
int introY;
Font f2;
FontMetrics fm2;
Font f3;
FontMetrics fm3;
boolean firing;
boolean sound;
String marker;
Object frame;
int mouseX;
int mouseY;
boolean mouseMoved;
boolean choice;
String marker2;
boolean clicked;
boolean drawScoreBoard;
boolean ended;
URL url;
int flicker;
public void init()
{
tracker = new MediaTracker(this);
f = new Font("TimesRoman", 1, 36);
fm = getFontMetrics(f);
f2 = new Font("TimesRoman", 2, 18);
fm2 = getFontMetrics(f2);
f3 = new Font("TimesRoman", 1, 20);
fm3 = getFontMetrics(f3);
System.out.println("Falling stars by RJHM van den Bergh (c)1996");
System.out.println("From : Java applet download site , http://www.nedernet.nl/~rvdb\nrvdb@nedernet.n" +
"l"
);
laser = getAudioClip(getCodeBase(), "laser.au");
laser.play();
explosion = getAudioClip(getCodeBase(), "explosion.au");
explosion.play();
pause(2000);
shipExploding = getAudioClip(getCodeBase(), "shipexplode.au");
shipExploding.play();
alert = getAudioClip(getCodeBase(), "alert.au");
alert.loop();
star = getImage(getCodeBase(), "star.gif");
ship = getImage(getCodeBase(), "ship.gif");
shipexplode = getImage(getCodeBase(), "shipexplode.gif");
tracker.addImage(star, 0);
tracker.addImage(ship, 0);
tracker.addImage(shipexplode, 0);
getAppletContext().showStatus("loading the pictures");
try
{
tracker.waitForID(0);
}
catch(InterruptedException _ex)
{
return;
}
starX = new int[maxStars + 1];
starY = new int[maxStars + 1];
starSize = new int[maxStars + 1];
starXAdd = new int[maxStars + 1];
starYAdd = new int[maxStars + 1];
for(tempInt = 1; tempInt <= numberOffStars; tempInt++)
{
replaceStar(tempInt);
starSize[tempInt] = 1;
}
while(marker != "nhgube EWUZ" || marker2 != "ina qra Oretu") ;
shipX = (size().width - ship.getWidth(this)) / 2;
offscreenI = createImage(size().width, size().height);
offscreenG = offscreenI.getGraphics();
offscreenG.setColor(Color.black);
offscreenG.fillRect(0, 0, size().width, size().height);
introY = size().height + 10;
for(frame = this; frame != null && !(frame instanceof Frame); frame = ((Component)frame).getParent()) { }
try
{
url = new URL("http://www.nedernet.nl/~rvdb");
return;
}
catch(MalformedURLException _ex)
{
System.out.println("Bad Url = " + url);
}
}
public boolean keyDown(Event event, int i)
{
switch(i)
{
case 1006:
shipX -= 7;
if(shipX - ship.getWidth(this) / 2 < 5)
{
shipX = ship.getWidth(this) / 2 + 5;
}
repaint();
return true;
case 1007:
shipX += 7;
if(shipX + ship.getWidth(this) / 2 > size().width - 5)
{
shipX = size().width - ship.getWidth(this) / 2 - 5;
}
repaint();
return true;
case 32: // ' '
firing = true;
laser.play();
hitCheck();
return true;
case 1004:
firing = true;
laser.play();
hitCheck();
return true;
case 83: // 'S'
if(sound)
{
alert.stop();
sound = false;
} else
{
alert.play();
sound = true;
}
return true;
case 115: // 's'
if(sound)
{
alert.stop();
sound = false;
} else
{
alert.loop();
sound = true;
}
return true;
case 27: // '\033'
shiphit = true;
numberOffShips = 0;
return true;
}
return false;
}
public void mouseMove()
{
shipX = mouseX;
((Frame)frame).setCursor(1);
mouseMoved = false;
if(shipX + ship.getWidth(this) / 2 > size().width - 5)
{
shipX = size().width - ship.getWidth(this) / 2 - 5;
}
if(shipX - ship.getWidth(this) / 2 < 5)
{
shipX = ship.getWidth(this) / 2 + 5;
}
repaint();
}
public boolean handleEvent(Event event)
{
switch(event.id)
{
case 503: // Event.MOUSE_MOVE
mouseMoved = true;
mouseX = event.x;
mouseY = event.y;
return true;
case 501: // Event.MOUSE_DOWN
if(started)
{
firing = true;
laser.play();
hitCheck();
} else
{
if(starting)
{
choice = true;
}
starting = true;
}
clicked = true;
return true;
case 502: // Event.MOUSE_UP
default:
return super.handleEvent(event);
}
}
public void start()
{
if(runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if(runner != null)
{
if(sound)
{
alert.stop();
}
runner.stop();
runner = null;
}
}
public void destroy()
{
explosion.stop();
alert.stop();
runner = null;
System.gc();
super.destroy();
}
public void run()
{
while(Thread.currentThread() == runner)
{
if(started)
{