📄 547-561.html
字号:
// storage for small ast
int tx2[] = new int[px[SMALL].length];
int ty2[] = new int[px[SMALL].length];
// initialize powerup sprite
powerup = new Asteroid(powerx,powery,powerx.length,
0,0,
Color.green,w,h,
8,0);
powerup.setRotationRate(3);
// make large asts
for (int i=0; i<NUM_LARGE; i++) {
MakeVectors(tx0,ty0,LARGE);
a[i] = new Asteroid(tx0,ty0,px[LARGE].length,
MAX_RAD_LARGE-
(int)(MAX_RADIUS/AVAR/2.0),
VAL_LARGE);
}
// make medium asts
for (int i=NUM_LARGE; i<NUM_LARGE+NUM_MEDIUM; i++) {
MakeVectors(tx1,ty1,MEDIUM);
a[i] = new Asteroid(tx1,ty1,px[MEDIUM].length,
0,0,MEDIUM_COLOR,w,h,
MAX_RAD_MEDIUM-
(int)(MAX_RADIUS/AVAR/MRAT/2.0),
VAL_MEDIUM);
// make small asts
for (int i=NUM_MEDIUM;
i<NUM_LARGE+NUM_MEDIUM+NUM_SMALL;
i++) {
MakeVectors(tx2,ty2,SMALL);
a[i] = new Asteroid(tx2,ty2,px[SMALL].length,
0,0,SMALL_COLOR,w,h,
MAX_RAD_SMALL-
(int)(MAX_RADIUS/AVAR/SRAT/2.0),
VAL_SMALL);
}
newGame();
}
/////////////////////////////////////////////////////////////////
// initialize a new Game: set the level, and create
// a new board of asts at that level
/////////////////////////////////////////////////////////////////
public void newGame() {
level = 1;
newBoard(level);
}
/////////////////////////////////////////////////////////////////
//
// The Update method:
// handle collisions between:
// asts and (ship, ship fire, enemies, enemy fire)
// powerup and ship
/////////////////////////////////////////////////////////////////
public void update() {
// update powerup
if (powerup.isActive()) {
powerup.update();
// if ship touches powerup, increase shield
if (powerup.intersect(ship)) {
powerup.suspend();
sm.increaseShield();
}
}
////////////////////////////////
// iterate through all asteroids
////////////////////////////////
boolean asts_left = false;
for (int i=0; i<NUM_ASTS; i++) {
if (a[i].isActive()) {
asts_left = true;
a[i].update();
///////////////////////////////////
// CHECK FOR SHIP INTERSECTION
// if the ship's grace period isn't over,
// update the grace period
if (gracePeriod > 0) {
gracePeriod--;
}
// else check if the ast hits the ship
else {
if (a[i].intersect(ship)) {
// divide Asteroid i
Divide(i);
game.updateScore(a[i].value);
// tell ShipManager to destroy ship
sm.destroyShip();
}
}
///////////////////////////////////
// CHECK FOR ENEMY INTERSECTION
//
for (int j=0; j<enemy.length; j++) {
if (a[i].intersect(enemy[j])) {
Divide(i);
em.destroyEnemy(j);
}
}
////////////////////////////////////////
// CHECK FOR INTERSECTION WITH SHIP FIRE
//
for (int j=0; j<fire.length; j++) {
if (a[i].intersect(fire[j])) {
// stop fire sprite
sm.stopFire(j);
Divide(i);
game.updateScore(a[i].value);
}
}
/////////////////////////////////////////
// CHECK FOR INTERSECTION WITH ENEMY FIRE
//
for (int j=0; j<enemyfire.length; j++) {
if (a[i].intersect(enemyfire[j])) {
// stop fire sprite
em.stopFire(j);
Divide(i);
}
}
}
}
// IF NO ASTEROIDS LEFT, CREATE A NEW BOARD
if (!asts_left) {
newBoard(++level);
}
}
/////////////////////////////////////////////////////////////////
// set the level of play
/////////////////////////////////////////////////////////////////
public void setLevel(int i) {
level = i;
}
/////////////////////////////////////////////////////////////////
// split an asteroid into two smaller ones
/////////////////////////////////////////////////////////////////
private void Divide(int i) {
int child1, child2;
int vertex;
// suspend asteroid i, and create an explosion there
a[i].suspend();
efm.addAstExplosion(a[i].locx,a[i].locy);
// don't do anything if it's a small asteroid
if (i >= NUM_LARGE + NUM_MEDIUM)
return;
// if it's a large ast, split it into two mediums
if (i < NUM_LARGE) {
child1 = NUM_LARGE + 2*i; // indices of med asts
child2 = NUM_LARGE + 2*i + 1;
}
// else medium ast => two smalls
else { // indices of small asts
child1 = (NUM_LARGE+NUM_MEDIUM) + 2*(i-NUM_LARGE);
child2 = (NUM_LARGE+NUM_MEDIUM) + 2*(i-NUM_LARGE) + 1;
}
vertex = a[i].p.npoints - 1; // last vertex
// set location of children to midpt betw center and vertex
a[child1].setPosition(a[i].tx[vertex]/2 + a[i].locx,
a[i].ty[vertex]/2 + a[i].locy);
a[child2].setPosition(a[i].tx[0]/2 + a[i].locx,
a[i].ty[0]/2 + a[i].locy);
// set velocity of children
a[child1].setVelocity(a[i].vx +
GameMath.getRand(2*MAX_VX)-MAX_VX,
a[i].vy +
GameMath.getRand(2*MAX_VY)-MAX_VY);
a[child2].setVelocity(a[i].vx +
GameMath.getRand(2*MAX_VX)-MAX_VX,
a[i].vy +
GameMath.getRand(2*MAX_VY)-MAX_VY);
// restore these sprites
a[child1].restore();
a[child2].restore();
}
/////////////////////////////////////////////////////////////////
// paint the powerup and asteroids
/////////////////////////////////////////////////////////////////
public void paint(Graphics g) {
powerup.paint(g);
for (int i=0; i<NUM_ASTS; i++) {
a[i].paint(g);
}
}
/////////////////////////////////////////////////////////////////
// Accessor methods
/////////////////////////////////////////////////////////////////
public Asteroid[] getAsts() {
return a;
}
public Asteroid getPowerup() {
return powerup;
}
/////////////////////////////////////////////////////////////////
// Create a new board of asteroids
// based on the given level 'n'
/////////////////////////////////////////////////////////////////
// period of invulnerability for ship
// every time there's a new board
static int gracePeriod;
public void newBoard(int n) {
int x,y;
int vx,vy;
// define the number of large asts in this board
int num_asts = n/2 + 1;
if (num_asts >= NUM_LARGE) {
num_asts = NUM_LARGE;
}
// set the position and speed of these large asteroids
// and restore these sprites
for (int i=0; i<num_asts; i++) {
x = GameMath.getRand(width-2*MAX_RADIUS)+MAX_RADIUS ;
y = GameMath.getRand(height-2*MAX_RADIUS)+MAX_RADIUS ;
vx = GameMath.getRand(2*MAX_VX)-MAX_VX;
vy = GameMath.getRand(2*MAX_VY)-MAX_VY;
a[i].setPosition(x,y);
a[i].setVelocity(vx,vy);
a[i].restore();
}
// set the powerup
if (!powerup.isActive()) {
x = GameMath.getRand(width-2*MAX_RADIUS)+MAX_RADIUS ;
y = GameMath.getRand(height-2*MAX_RADIUS)+MAX_RADIUS ;
powerup.setPosition(x,y);
powerup.restore();
}
// set the grace period for the ship
gracePeriod = 50;
// suspend the remaining sprites
for (int i= num_asts; i<NUM_ASTS; i++) {
a[i].suspend();
}
}
/////////////////////////////////////////////////////////////////
// Make the vectors of the individual Asteroids,
// by randomly perturbing the given template
/////////////////////////////////////////////////////////////////
private void MakeVectors(int tpx[], int tpy[],int size) {
int n = tpx.length; // number of points in asteroid
int deviation; // max deviation from template
// compute max deviation from template
if (size == LARGE) {
deviation = (int)(MAX_RADIUS/AVAR);
}
else {
// else a small or medium size asteroid
// so reduce max deviation by the corresponding amount
int factor = size*SIZE_RATIO;
deviation = (int)(MAX_RADIUS/factor/AVAR);
}
// compute the first and final point of asteroid
int fx =
(int)(GameMath.getRand(2*deviation)-deviation) +
px[size][0];
int fy =
(int)(GameMath.getRand(2*deviation)-deviation) +
py[size][0];
// close polygon
tpx[0] = tpx[n-1] = fx;
tpy[0] = tpy[n-1] = fy;
// compute the rest of the points in asteroid
for (int i = 1; i < n-1 ; i++) {
tpx[i] =
(int)(GameMath.getRand(2*deviation)-deviation) +
px[size][i];
tpy[i] =
(int)(GameMath.getRand(2*deviation)-deviation) +
py[size][i];
}
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="543-547.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="561-570.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -