⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aster.java

📁 java编程的一些Applets例子。值得深入研究一下。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		angdrifty = (lld2+lld4); //same thing...
		vel = Math.pow((angdriftx*angdriftx+angdrifty*angdrifty), .5); //distance formula modification.  pow(x, .5) takes the square root of x
		if (angdriftx>8) { //if fast x movement
			angdrifty = 8*(angdrifty/angdriftx); //keeps things in proportion
			angdriftx = 8; //speed limiter
			}
		if (angdrifty>8) { //if fast y movement
			angdriftx = 8*(angdriftx/angdrifty); //keeps things in proportion
			angdrifty = 8; //speed limiter
			}
		if (angdriftx<-8) { //if fast x movement
			angdrifty = -8*(angdrifty/angdriftx); //keeps things in proportion
			angdriftx = -8; //speed limiter
			}
		if (angdrifty<-8) { //if fast y movement
			angdriftx = -8*(angdriftx/angdrifty); //keeps things in proportion
			angdrifty = -8; //speed limiter
			}
		}
	public void rotate(double degree) { //change angle of ship
		ang = ang + degree; //simple math
		}
	public boolean alive() { //function to check if player is alive
		return active; //returns the active variable
		}
	public void paint(Graphics g) { //paints the player to the screen
		if (shield) { //if shield is on
			col2 = new Color(0, 64+rot*4, 0); //define a color based on the color rotater
			g.setColor(col2); //set the new color
			g.fillOval((int)(xco-12-(rot/2)), (int)(yco-12-(rot/2)), 24+rot, 24+rot); //make a quickly expanding circle
			}
		for (li1 = 0; li1 < 13; li1++) { //draw all thirteen "speed indicator" (?) lines
			rot++; //cycle through another color rotation
			if (rot == 15) {rot = 0;} //put cieling on color rotation
			col2 = new Color(0,255-(rot*16),rot*16); //define new color
			g.setColor(col2); //set new color
			g.drawLine((int) (xco-(Math.cos(ang-Math.PI/8)*li1*vel/2)), (int) (yco-(Math.sin(ang-Math.PI/8)*li1*vel/2)), (int) (xco-(Math.cos(ang+Math.PI/8)*li1*vel/2)), (int) (yco-(Math.sin(ang+Math.PI/8)*li1*vel/2))); //draw a line from a distance&-angle to distance&+angle behind the ship
			}
		col2 = new Color(0, 172, 0); //make another new color
		g.setColor(col2); //set new color
		g.drawLine((int) (xco-(Math.cos(ang)*14)), (int) (yco-(Math.sin(ang)*14)), (int) (xco+(Math.cos(ang)*5)), (int) (yco+(Math.sin(ang)*5))); //make another trig line going behind player
		g.drawLine((int) (xco-(Math.cos(ang-Math.PI/6)*14)), (int) (yco-(Math.sin(ang-Math.PI/6)*14)), (int) xco, (int) yco); //make another trig line going diagonally behind player
		g.drawLine((int) (xco-(Math.cos(ang+Math.PI/6)*14)), (int) (yco-(Math.sin(ang+Math.PI/6)*14)), (int) xco, (int) yco); //make another trig line going diagonally behind player
		}
	public void move() { //moves the player
		pxc = xco; //redefines prev xcord
		pyc = yco; //redefines prev ycord
		xco = pxc + angdriftx; //adds the drift to the current xcord
		yco = pyc + angdrifty; //adds the drift to the current ycord
		}
	public void check(AstHandler ah, int starting, int wi, int he) { //checks for hits, position (using the list of asteroids)
		int li1;
		if (xco>wi + 10) { //if player is off right side of screen
			xco = xco - (wi+20); //put player on left side of screen
			}
		if (yco>he + 10) { //if player is off bottom of screen
			yco = yco - (he+20); //put player on top of screen
			}
		if (xco<-10) { //if player is off left side of screen
			xco = xco + wi+20; //put player on right side of screen
			}
		if (yco<-10) { //if player is off top of screen
			yco = yco + he+20; //put player on bottom of screen
			}
		if ((shield==false)&&(starting > 50)) { //if the shield ain't on...
			for (li1 = 0; li1 < 40; li1++) { //cycle through all possible asteroids
				if ((ah.asts[li1].state==1)&&(Math.abs(ah.asts[li1].xco-xco)<ah.asts[li1].size)&&(Math.abs(ah.asts[li1].yco-yco)<ah.asts[li1].size)) { //if the asteroid exists and is touching the player, then:
					active = false; //player is DEAD!
					}
				}
			}
		}
	public void stop() { //if program is stopped
		active = false; //player is killed... oh, the humanity...
		}
	}
class PlayerHandler extends java.lang.Object/* implements Runnable*/ { //handles the player(s)
	Player players = new Player(); //make the new player
	int ss; //shield strength remaining
	private int li1, li2; //temp integers
	public PlayerHandler(int wid, int hei) { //constructor
		players.start(wid, hei); //define player
		ss = 100; //put the shield at 100
		}
	public void keyDown(int key) {
		if (key == 107) {players.thrust((double) .4);} //if key is "k", use the thrust(double) method to move player
		if (key == 106) {players.rotate((double) (Math.PI / -24));} //if key is "j", rotate left
		if (key == 108) {players.rotate((double) (Math.PI / 24));} //if key is "k", rotate right
		if (key == 122) { //if key is "z"
			players.shield = true; //turn on shield
			ss = ss - 1; //take of the shield strength by one unit
			if (ss < 0) { //if there isn't any shield left....
				ss = -1; //"turn off" shield strength
				players.shield = false; //turn off shield
				}
			}
		}
	public void check(AstHandler ah, int sta, int wid, int hei) { //check for asteroid hits, etc
		players.check(ah, sta, wid, hei); //let the player object do the REAL work... just passes arguments through the "chain of command"
		if (players.alive()==false) { //if our boy is wounded...
			players.stop(); //kill him, have mercy!
			}
		}
	public void move() { //move the player
		if (players.active) { //if its alive...
			players.move(); //it should be moving, right?
			}
		}
	public void paint(Graphics g, int wid, int hei) { //paint the player, do the shield display
		Color col2 = new Color(0); //make a color... must instantiate for future references
		if (players.active) { //if player is alive
			g.setColor(col2.white); //set the color to white
			g.drawString("Shield strength: ", 0, 10); //type shield strength on the graphics window
			g.drawRect(100, 0, wid-101, 10); //make a bar shaped rectangle
			g.setColor(col2.red); //set color to red
			g.fillRect(101, 1, (int) (ss*((wid-101.5)/100)), 9); //make a rectangle with a width of the "ss" shield strength variable
			players.paint(g); //call the player paint method
			}
		}
	}

///////////////////////////////////////

class AstHandler extends java.lang.Object/* implements Runnable*/ { //asteroid handling class
	Ast asts[] = new Ast[41]; //make the asteroids! extra to avoid array exceptions
	Color col1; //temp color variable
	private int li1, li2; //temp integers
	public AstHandler() { //asteroid handler constructor
		for (li1 = 0; li1 < 41; li1++) { //cycle asteroids
			asts[li1] = new Ast(); //make a new asteroid
			}
		}
	public void AstCreate(int lev, int wid, int hei) { //create the asteroids with speed and number matching level
		for (li1 = 0; ((li1 < (int) (Math.random()*4+2+lev*2))&&(li1<40)); li1 ++) { //cycle through a number of asteroids related to level number
			asts[li1].create(Math.random()*wid, Math.random()*hei, Math.random()*2*Math.PI, Math.random()*(lev/2 + 1), Math.random()*28 + 16); //make that asteroid with random xcord, ycord, angle, velocity (defined w/ level #), and random size
			}
		}

	public int check(ShotHandler shotH, int leve, int sco, AudioClip expl, int wid, int hei) { //check using the shot handler, level number, score, and explosion audio clip.  Returns the score or a level end value (-1)
		int li2 = -1; //start li2 out as "-1"
		for (li1 = 0; li1 < 40; li1++) { //cycle asteroids
			if (asts[li1].state == 1) { //if its alive and kicking:
				try {asts[li1].check(shotH, wid, hei);} catch(NullPointerException e) {} //try to call the individual asteroids check method using the shot handler
				li2++; //advance li2
				}
			if (asts[li1].state == 2) { //if asteroid has JUST been hit (ready to be split)
				try {split(li1, leve);} catch(NullPointerException e) {} //split the asteroid into two smaller ones
				sco = sco+((int) (5*leve)); //advance score
				expl.play(); //play the audio clip
				}
			if (asts[li1].state == 3) { //if asteroid is exploding...
				try {asts[li1].kill();} catch(NullPointerException e) {} //kill the asteroid
				}
			}
		li1 = 0; //to descrease array exceptions
		if (li2==-1) {sco = li2;} //if no asteroids were detected, tell main app. that the level is over
		return sco; //return the score (or indication that level is over)
		}
	public void split(int num, int lev) { //splits an asteroid into two new ones
		int li1, li2; //temp ints
		int freespot[] = new int[2]; //array for free asteroid spaces
		freespot[0] = -1; //make first space null
		freespot[1] = -1; //make second space also null
		for(li1=0; li1<40; li1++) { //cycle asteroids
			if (asts[li1].state == -1) { //if the space is free
				if (freespot[0] == -1) {freespot[0] = li1;} //claim it if the freespot has not already been defined
				}
			}
		for(li1=0; li1<40; li1++) { //cycle asteroids
			if (asts[li1].state == -1) { //if the space is free
				if ((freespot[0] != li1)&&(freespot[1] == -1)) {freespot[1] = li1;} //if this isn't freespot[0]'s spot, and freespot[1] is still undefined, claim the space
				}
			}
		for(li1 = 0; li1<2; li1++) { //cycle freespots
			if (freespot[li1]!=-1) { //if this freespot found a place to take
				if (asts[num].size > 12) {asts[freespot[li1]].create(asts[num].xco, asts[num].yco, Math.random()*2*Math.PI, Math.random()*(lev/2 + 2), asts[num].size/2);} //IF THIS ASTEROID ISN'T TOO SMALL, make the (li1)'th new asteroid
				}
			}
		asts[num].state = 3; //mark this asteroid for certain death
		}
	public void move() { //moves the asteroids
		for (li1 = 0; li1 < 40; li1++) { //cycle through asteroids
			if (asts[li1].state == 1) { //if asteroid is alive...
				asts[li1].move(); //move it.
				}
			}
		}
	public void paint(Graphics g) { //paint the asteroids
		for (li1 = 0; li1 < 40; li1++) { //cycle asteroids
			if (asts[li1].state != -1) { //if asteroid isn't totally dead
				asts[li1].paint(g); //draw the asteroid
				}
			}
		}
	}

class Ast extends java.lang.Object { //a single asteroid object
	int state = -1; //start off as non-existent
	Color col1; //temp color variable
	int dierot, rot;  //the "death color rotation", normal color rotation
	double size, pxc, pyc, xco, yco; //asteroid size, prev xcord, prev ycord, current xcord, current ycord
	double ang, vel; //asteroid angle direction, asteroid velocity
	public Ast() { //asteroid constructor... not much goin' on here, really
		state = -1; //asteroid is dead/non-existent
		}
	public void create(double xcor, double ycor, double angle, double veloc, double siz) { //makes a new asteroid based on paramaters
		xco = xcor; //transfer xcord
		yco = ycor; //transfer ycord
		vel = veloc; //transfer velocity
		ang = angle; //transfer angle
		state = 1; //make the asteroid alive
		size = siz; //transfer size
		rot = 0; //make a new color rotator
		dierot = 0; //make a new death color rotator
		pxc = xco; //define a new prev xcord
		pyc = yco; //define a new prev ycord
		}
	public void kill() { //destroy the asteroid
		dierot++; //move the dierot up one
		if (dierot > 32) { //if its been 32 ticks off dierot,
			state = -1; //finish the job and destroy asteroid for good
			}
		}
	public void paint(Graphics g) { //draws asteroid
		int li1; //temp integer
		if (state == 1) { //if asteroid is in normal state
			rot++; //advance color rotation
			if (rot == 16) {rot = 0;} //restart color rot at 16
			col1 = new Color(64+(Math.abs(rot-8)*6),64+Math.abs(rot-8)*5,255-(64+Math.abs(rot-8)*6)); //define grey-blue asteroid color
			g.setColor(col1); //set the new color
			g.fillOval((int)(xco-(size/2)), (int)(yco-(size/2)), (int)(size), (int)(size)); //draw the asteroid
			}
		if ((state == 2)||(state == 3)) { //if asteroid is splitting or dying
			if (dierot<17) { //first half of death
				col1 = new Color(255, dierot*8, 0); //new color (red->orange->yellow)
				g.setColor(col1); //set the new color
				g.fillOval((int)(xco-dierot/2-4), (int)(yco-dierot/2-4), (int)((dierot+8)), (int)((dierot+8))); //make the explosion circle
				}
			if (dierot>16) { //second half of death
				col1 = new Color(255, (dierot-16)*5+127, 0); //new color (red->orange->yellow)
				g.setColor(col1); //set the new color
				g.fillOval((int)(xco-(32-dierot)/2-4), (int)(yco-(32-dierot)/2-4), (int)((32-dierot)+8), (int)((32-dierot)+8)); //make the explosion circle
				}
			}
		}
	public void move() { //moves the asteroid
		pxc = xco; //new prev xcord
		pyc = yco; //new prev ycord
		xco = pxc + (Math.cos(ang)*vel); //new xcord
		yco = pyc + (Math.sin(ang)*vel); //new ycord
		}
	public void check(ShotHandler sh, int wi, int he) { //check asteroid coordinates, see if its been hit (uses the shot handler)
		int li1, li2; //temp integers
		double sx[] = new double[16]; //array of shot xcords
		double sy[] = new double[16]; //array of shot ycords
		if (xco>wi + 10) { //if asteroid is off the right side of screen
			xco = xco - (wi+20); //put asteroid on left side of screen
			pxc = xco; //define new prev xcord to prevent drawing problems
			}
		if (yco>he + 10) { //if asteroid is off the bottom of screen
			yco = yco - (he+20); //put asteroid on top of screen
			pyc = yco; //define new prev ycord to prevent drawing problems
			}
		if (xco<-10) { //if asteroid is off the left side of screen
			xco = xco + wi+20; //put asteroid on right side of screen
			pxc = xco; //define new prev xcord to prevent drawing problems
			}
		if (yco<-10) { //if asteroid is off the top of screen
			yco = yco + he+20; //put asteroid on bottom of screen
			pyc = yco; //define new prev ycord to prevent drawing problems
			}
		for (li1 = 0; li1 < 16; li1++) { //cycle through shots in the imported shot handler
			sx[li1] = sh.shots[li1].xco; //move the xcord to the local array
			sy[li1] = sh.shots[li1].yco; //move the ycord to the local array
			if (sh.shots[li1].active!=true) {sx[li1] = -127;} //if shot is non-existent, raise red flag w/ the -127 value.  -1 is a possible xcord, so I couldn't use that
			}
		for (li1 = 0; li1 < 16; li1++) { //cycle local "virtual shot array"
			if ((sx[li1] != -127)&&(Math.abs(sx[li1]-xco)<size)&&(Math.abs(sy[li1]-yco)<size)) { //if the shot exists, and is close enough to the asteroid...
				state = 2; //prepare asteroid for splitting
				sh.shots[li1].stop(); //kill the shot, as well
				}
			}
		}
	}

/* THATS ALL, FOLKS!
* once again, sigelman@crocker.com is my email address, and you 
* can see this applet (crash netscape) at my home page, 
* "Ben Sigelman's Black Russian Page (JAVA)" - http://www.crocker.com/~sigelman/
*
* Thanx for looking at my applet!
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -