📄 aster.java
字号:
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.net.*;
/* This is all original material, with the exception of the double buffering, which
* I hacked out of a quote-ticker program. The game is a GREAT example of object-heirarchy.
* I also supported multithreading, but it just made things run slower, actually!
* For this reason the multithreading was removed...
* PARAMETERS:
* wid: the width of the applet (integer)
* hei: the height of the applet (integer)
* Feel free to modify or distribute as you wish, as long as you include:
Original programmed by: Ben Sigelman, sigelman@crocker.com
http://www.crocker.com/~sigelman/ ||| FOR HIRE! ALWAYS!
* Appletviewer has its bugs.... netscape _is_ a bug.
*/
public class aster extends java.applet.Applet implements Runnable { //this is the main program, aster(noid)
ShotHandler shotH; //this is an implementation of my "shothandler" class, which keeps track of all your shots.
PlayerHandler playH; //this is an implementation of my "playerhandler" class, which keeps track of your players needs... leaves open support for multi-player
AstHandler astH; //same deal: handling class for all of the "asteroids" in the game. (the things you shoot, that is)
Color col; //i like to define things like this in case their needed... this one is used to change colors
Image im; //this is used in the double-buffering lines.
Graphics offscreen; //more double-buffering crap
int keyp, i1, i2, i3, i4; //keyp was used for debugging. the rest are just temp. integers
public static int score; //self-explanatory... its the score accumulated
int level = 0; //this is the current level
int levelend = 0; //this is used in the pause at the end of every level
int levelstart = 0; //this is used in the invulnerability period that starts each level.
double d1, d2, d3, d4; //temp double things
int keys[] = new int[3]; //VERY IMPORTANT! this has to do with overriding the default keyboard handling
AudioClip /*shotsound, */explode; //the explosion sound in the game... shotsound was too obnoxious to include
URL codeb; //code base URL. this is the documents url... a kind of base directory
int width, height; //width and height as specified by the html file
public boolean keyDown(Event ev, int key) { //keyDown event automatically calls this function.
int li1; //temp integer
if (key == 32) { //space bar: shoot
if (shotH.shotdown == 1) {shotH.shotdown = 2;} //these lines dont allow you to hold down
if (shotH.shotdown == 0) {shotH.shotdown = 1;} //space to shoot. no shots fired at "2" or "0"
}
keyp = key; //for debugging of key codes only... deleteable
for (li1 = 0; li1 < 3; li1++) { //cycles through all three key variables
if ((keys[li1] == -1)&&((keys[2] != key)&&(keys[0] != key)&&(keys[1] != key))) {keys[li1] = key;} //assigns the key to any free spaces... no duplicate key refs. "-1" = free
}
return true; //required by java
}
public boolean keyUp(Event ev, int key) { //keyUp event automatically calls this function
int li1; //temp integer
for (li1 = 0; li1 < 3; li1++) { //cycle through keys
if (keys[li1] == key) {keys[li1] = -1;} //"unpresses" the key from the games perspective
}
if (key == 122) {playH.players.shield = false;} //turns off the shield immediatly after key is released
shotH.shotdown = 0; //lets shots be fired on next keydown
return true; //required by java
}
public void checkKeys(int key) { //checks for the given key (in the array)
playH.keyDown(key); //tells the playerhandler that the key has been pressed.
shotH.keyDown(key, playH.players.ang, playH.players.xco, playH.players.yco); //tells the shothandler the given key has been pressed
}
public void init() {
level++; //change level to 1
score = 0; //obvious...
keys[0] = -1; //unpresses key #1
keys[1] = -1; //unpresses key #2
keys[2] = -1; //unpresses key #3
width = 640; //default width
height = 480; //default height
width = Integer.valueOf(getParameter("wid")).intValue(); //gets the width of the applet
height = Integer.valueOf(getParameter("hei")).intValue(); //gets the height of the applet
shotH = new ShotHandler(); //starts up shotH
playH = new PlayerHandler(width, height); //starts up playerH
astH = new AstHandler(); //starts up astH
astH.AstCreate(level, width, height); //makes the first few asteroids... #asteroids relates to #level
/*these next lines regard double buffer initialization.*/
try {
im = createImage(width,height); //applet size is width*height
offscreen = im.getGraphics();
}
catch (Exception e) {
offscreen = null;
}
/*end of double buffering*/
codeb = getCodeBase(); //gets the code base, described in variable definition of "codeb"
explode = getAudioClip(codeb, "explode.au"); //gets the explosion audio clip in the applet's base directory
}
public void start() {
(new Thread(this)).start(); //initializes main thread
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); //its necessary... try changing it sometime!
}
public void run() {
while (true) { //always runs until stopped
levelstart++; //50 ticks of invulnerability to begin each level
if (levelstart>75) {levelstart = 51;} //doesn't let the number get out of hand.
checkKeys(keys[0]); //checks key#1
checkKeys(keys[1]); //checks key#2
checkKeys(keys[2]); //checks key#3
try {
astH.move(); //tells astH to move all its asteroids
i2 = astH.check(shotH, level, score, explode, width, height); //tells it to check its asteroids... needs all shot locations, the level of difficulty, needs soundfx, RETURNS score or a "level-over indicator"
shotH.move(); //tells shotH to move all the shots
shotH.check(width, height); //tells shotH to check the shots' movement
playH.move(); //moves player(s)
playH.check(astH, levelstart, width, height); //checks the player... need invulnerability info, and location of all asteroids.
} catch(NullPointerException e) {}
repaint(); //repaints the screen
if (i2 == -1) {levelend++;} //i2 is -1 when all asteroids are destroyed - it will increase levelend ticker
if (i2 != -1) {score = i2;} //i2 is usually the score (returned by astH.check). updates the score
if (levelend > 50) {nextlevel();} //if enough ticks have gone by, next level is initialized
try {Thread.currentThread().sleep(35);} catch (InterruptedException e){} //adds some time to keep the speed normal
Thread.currentThread().yield(); //REALLY REALLY REALLY REALLY IMPORTANT! THIS WILL DRAMATICALLY ENHANCE NETSACAPE PERFORMANCE!
}
}
public void nextlevel() {
score = score + (100*level); //"finish level" bonus
levelend = 0; //resets levelend ticker
playH.ss = playH.ss + (level/2)*10; //gives a shield bonus for the player
if (playH.ss>100) {playH.ss = 100;} //caps shield at 100
level++; //next level (finally)
keys[0] = -1; //resets key#1
keys[1] = -1; //resets key#2
keys[2] = -1; //resets key#3
astH = new AstHandler(); //makes a new set of asts
astH.AstCreate(level, width, height); //creates more asteroids in relation to the level
levelstart = 0; //allows 50 more ticks of invulnerability at start of next level
}
public void update(Graphics g) {
paint(g); //overrides update
}
public void paint(Graphics g) {
if (offscreen!=null) { //more double-buffering crap
paintApplet(offscreen); //the REAL paint method
g.drawImage(im, 0, 0, this);
}
else {
paintApplet(g); //the REAL paint method
}
}
public void paintApplet(Graphics g) {
g.setColor(col.black); //prepares screen wash
g.fillRect(0,0,width,height); //makes big, black rectangle washover
playH.paint(g, width, height); //paints the player
astH.paint(g); //paints the asteroids
try {shotH.paint(g);} catch(NullPointerException e) {} //shotH.paint likes to crash applet, for some reason
g.setColor(col.white); //you know this, hopefully
this.showStatus("level: " + level); //puts the level number in the status bar
g.drawString("SCORE: " + score, 0, 30); //shows the current score
}
}
class ShotHandler extends java.lang.Object/* implements Runnable*/ { //handles basic instructions and breaks them down to a shot-by-shot level
Shot shots[] = new Shot[17]; //makes all 17 shots (program only uses 16: 17 is to prevent buggy array access exceptions)
Color col1; //temp color variable
int shotdown = 0; //is there a shot being fired? 1 is true
private int li1, li2; //temp integers
public ShotHandler() { //constructor when a new instance is called for
for (li1 = 0; li1 < 16; li1++) { //cycles through the shots
shots[li1] = new Shot(); //new instance of shot for each segment of array
}
}
public void keyDown(int key, double ang, double xcor, double ycor) { //handles what is passed in from the main applet's keydown event
int freeshot = -1; //initializes as nonexistent
if (key == 32) { //if the key is space bar,
for(li1=0; li1<16; li1++) { //cycle shots
if (shots[li1].active==false) {freeshot = li1;} //if shot isn't being used, make temp = shot #
}
if ((shotdown == 1)&&(freeshot != -1)) { //if this is the FIRST pressing of space (not held) and freeshot "found a home" then:
shots[freeshot].shoot(16, ang, xcor, ycor); //makes a shot of velocity 16, the players current angle, player's xcord, and player's ycord
}
shotdown = 2; //space has been held down
}
}
public void check(int wid, int hei) {
for (li1 = 0; li1 < 16; li1++) { //cycle shots
if (shots[li1].active) { //if shot exists
shots[li1].check(wid, hei); //call shot's individual check method
if (shots[li1].cycles>16) { //if shot has been alive for 16 cycles
shots[li1].stop(); //kill the friggin' thing
}
}
}
}
public void move() { //obvious...
for (li1 = 0; li1 < 16; li1++) { //cycle shots
if (shots[li1].active) { //if alive
shots[li1].move(); //call upon individual move method
}
}
}
public void paint(Graphics g) { //the shotH paint method.
li1 = 0; //init temp var... this solves a NullPointerException often encountered... not TOTALLY sure why
for (li1 = 0; li1 < 16; li1++) { //cycle shots (again :-[)
if (shots[li1].active) { //if alive
shots[li1].paint(g); //call individual paint method
}
}
}
}
class Shot extends java.lang.Object { //an individual shot method, owned by ShotH's "shots" array
boolean active = false; //default: shot is non-existent
Color col1; //temp color variable
int cycles, rot; //sequence of color
double pxc, pyc, xco, yco; //prev xcord, prev ycord, curr xcord, curr ycord
double ang, vel; //shot angle direction (in radians), velocity in units moved per program tick
public Shot() { //the constructor
active = false; //yeah, its redundant
}
public void shoot(double veloc, double angle, double xcor, double ycor) { //make an active shot with the given attributes
xco = xcor; //transfer x coordinate
yco = ycor; //transfer y coordinate
vel = veloc; //transfer velocity
ang = angle; //transfer angle
active = true; //make shot active, or visible.
cycles = 0; //after 16 cycles, the shot is destroyed.
rot = 0; //color rotation variable
pxc = xco; //initializes the previous xcord variable
pyc = yco; //initializes the previous ycord variable
}
public void stop() { //kills the shot
active = false; //take a guess...
}
public void paint(Graphics g) { //this is the shots paint method... draws a line from its last position to its current position, basically
rot++; //advances color rotation
if (rot == 16) {rot = 0;} //keeps rotation below 17
col1 = new Color(255-(rot*8),0,127+rot*8); //makes the new color
g.setColor(col1); //sets the current color to the rotation color
g.drawLine((int)xco, (int)yco, (int)(xco-(xco-pxc)), (int)(yco-(yco-pyc))); //draws a line from the current coords to the previous coords
}
public void move() { //moves the shot
cycles++; //advances the shot's "age" in cycles
pxc = xco; //makes a new prev. xcord
pyc = yco; //makes a new prev. ycord
xco = pxc + (Math.cos(ang)*vel); //defines the new xcord as a trig function utilizing angle and velocity (hypothenuse)
yco = pyc + (Math.sin(ang)*vel); //defines the new ycord as a trig function utilizing angle and velocity (hypothenuse)
}
public void check(int wi, int he) { //checks the shot's coords
if (xco>wi + 10) { //if xcord is more than 10 off the right side
xco = xco - (wi+20); //move to the left side of screen
pxc = xco; //disable the possibility of drawing a line across the screen
}
if (yco>he + 10) { //if ycord is more than 10 below the bottom
yco = yco - (he+20); //move to the top of screen
pyc = yco; //disable the possibility of drawing a line across the screen
}
if (xco<-10) { //if xcord is more than 10 off the left side
xco = xco + wi+20; //move to the right side of screen
pxc = xco; //disable the possibility of drawing a line across the screen
}
if (yco<-10) { //if ycord is more than 10 off the top
yco = yco + he+20; //move to the bottom of screen
pyc = yco; //disable the possibility of drawing a line across the screen
}
}
}
class Player extends java.lang.Object { //a player object
private int li1, li2; //some temp ints
boolean active = true; //is player alive?
boolean shield = false; //is shield on?
int rot; //color rotation
Color col2; //temp color variable
double pxc, pyc, xco, yco; //prev xcord, prev ycord, curr xcord, curr ycord
double ang, angdriftx, angdrifty, vel; //player angle direction, actual movement direction (x), actual movement direction (y), speed
public Player() { //the default constructor
active = true; //redundant? yes...
}
public void start(int wi, int he) { //the REAL constructor
xco = (Math.random()*wi); //chooses a random x value
yco = (Math.random()*he); //chooses a random y value
pxc = xco; //inits the prev xcord
pyc = yco; //inits the prev ycord
vel = 0; //sets velocity to 0
ang = .01; //makes angle a little past default: this eliminates trigonometry errors computing the angle (ie tan(0))
angdriftx = 0; //no movement
angdrifty = 0; //no movement
rot = (int) (Math.random()*15); //gets a random value for the color rotation
}
public void thrust(double amount) { //if the letter "k" is pressed, the ship must make movement adjustments
double lld1, lld2, lld3, lld4; //temp doubles
lld1 = angdriftx; //assigned to the x angle-drift
lld2 = angdrifty; //assigned to the y angle-drift
lld3 = (Math.cos(ang)*amount); //assigned to the amount of x-change there must be
lld4 = (Math.sin(ang)*amount); //assigned to the amount of y-change there must be
angdriftx = (lld1+lld3); //just what you'd expect next
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -