asteroids.java
来自「很有趣的射击游戏」· Java 代码 · 共 1,251 行 · 第 1/3 页
JAVA
1,251 行
asteroidsCounter = STORM_PAUSE; asteroidsLeft = MAX_ROCKS; if (asteroidsSpeed < MAX_ROCK_SPEED) asteroidsSpeed++; } public void initSmallAsteroids(int n) { int count; int i, j; int s; double tempX, tempY; double theta, r; int x, y; // Create one or two smaller asteroids from a larger one using inactive asteroids. The new // asteroids will be placed in the same position as the old one but will have a new, smaller // shape and new, randomly generated movements. count = 0; i = 0; tempX = asteroids[n].currentX; tempY = asteroids[n].currentY; do { if (!asteroids[i].active) { asteroids[i].shape = new Polygon(); s = MIN_ROCK_SIDES + (int) (Math.random() * (MAX_ROCK_SIDES - MIN_ROCK_SIDES)); for (j = 0; j < s; j ++) { theta = 2 * Math.PI / s * j; r = (MIN_ROCK_SIZE + (int) (Math.random() * (MAX_ROCK_SIZE - MIN_ROCK_SIZE))) / 2; x = (int) -Math.round(r * Math.sin(theta)); y = (int) Math.round(r * Math.cos(theta)); asteroids[i].shape.addPoint(x, y); } asteroids[i].active = true; asteroids[i].angle = 0.0; asteroids[i].deltaAngle = (Math.random() - 0.5) / 10; asteroids[i].currentX = tempX; asteroids[i].currentY = tempY; asteroids[i].deltaX = Math.random() * 2 * asteroidsSpeed - asteroidsSpeed; asteroids[i].deltaY = Math.random() * 2 * asteroidsSpeed - asteroidsSpeed; asteroids[i].render(); asteroidIsSmall[i] = true; count++; asteroidsLeft++; } i++; } while (i < MAX_ROCKS && count < 2); } public void updateAsteroids() { int i, j; // Move any active asteroids and check for collisions. for (i = 0; i < MAX_ROCKS; i++) if (asteroids[i].active) { asteroids[i].advance(); asteroids[i].render(); // If hit by photon, kill asteroid and advance score. If asteroid is large, // make some smaller ones to replace it. for (j = 0; j < MAX_SHOTS; j++) if (photons[j].active && asteroids[i].active && asteroids[i].isColliding(photons[j])) { asteroidsLeft--; asteroids[i].active = false; photons[j].active = false; if (sound) explosionSound.play(); explode(asteroids[i]); if (!asteroidIsSmall[i]) { score += BIG_POINTS; initSmallAsteroids(i); } else score += SMALL_POINTS; } // If the ship is not in hyperspace, see if it is hit. if (ship.active && hyperCounter <= 0 && asteroids[i].active && asteroids[i].isColliding(ship)) { if (sound) crashSound.play(); explode(ship); stopShip(); stopUfo(); stopMissle(); } } } public void initExplosions() { int i; for (i = 0; i < MAX_SCRAP; i++) { explosions[i].shape = new Polygon(); explosions[i].active = false; explosionCounter[i] = 0; } explosionIndex = 0; } public void explode(AsteroidsSprite s) { int c, i, j; // Create sprites for explosion animation. The each individual line segment of the given sprite // is used to create a new sprite that will move outward from the sprite's original position // with a random rotation. s.render(); c = 2; if (detail || s.sprite.npoints < 6) c = 1; for (i = 0; i < s.sprite.npoints; i += c) { explosionIndex++; if (explosionIndex >= MAX_SCRAP) explosionIndex = 0; explosions[explosionIndex].active = true; explosions[explosionIndex].shape = new Polygon(); explosions[explosionIndex].shape.addPoint(s.shape.xpoints[i], s.shape.ypoints[i]); j = i + 1; if (j >= s.sprite.npoints) j -= s.sprite.npoints; explosions[explosionIndex].shape.addPoint(s.shape.xpoints[j], s.shape.ypoints[j]); explosions[explosionIndex].angle = s.angle; explosions[explosionIndex].deltaAngle = (Math.random() * 2 * Math.PI - Math.PI) / 15; explosions[explosionIndex].currentX = s.currentX; explosions[explosionIndex].currentY = s.currentY; explosions[explosionIndex].deltaX = -s.shape.xpoints[i] / 5; explosions[explosionIndex].deltaY = -s.shape.ypoints[i] / 5; explosionCounter[explosionIndex] = SCRAP_COUNT; } } public void updateExplosions() { int i; // Move any active explosion debris. Stop explosion when its counter has expired. for (i = 0; i < MAX_SCRAP; i++) if (explosions[i].active) { explosions[i].advance(); explosions[i].render(); if (--explosionCounter[i] < 0) explosions[i].active = false; } } public boolean keyDown(Event e, int key) { // Check if any cursor keys have been pressed and set flags. if (key == Event.LEFT) left = true; if (key == Event.RIGHT) right = true; if (key == Event.UP) up = true; if (key == Event.DOWN) down = true; if ((up || down) && ship.active && !thrustersPlaying) { if (sound && !paused) thrustersSound.loop(); thrustersPlaying = true; } // Spacebar: fire a photon and start its counter. if (key == 32 && ship.active) { if (sound & !paused) fireSound.play(); photonIndex++; if (photonIndex >= MAX_SHOTS) photonIndex = 0; photons[photonIndex].active = true; photons[photonIndex].currentX = ship.currentX; photons[photonIndex].currentY = ship.currentY; photons[photonIndex].deltaX = MIN_ROCK_SIZE * -Math.sin(ship.angle); photons[photonIndex].deltaY = MIN_ROCK_SIZE * Math.cos(ship.angle); photonCounter[photonIndex] = Math.min(AsteroidsSprite.width, AsteroidsSprite.height) / MIN_ROCK_SIZE; } // 'H' key: warp ship into hyperspace by moving to a random location and starting counter. if (key == 104 && ship.active && hyperCounter <= 0) { ship.currentX = Math.random() * AsteroidsSprite.width; ship.currentX = Math.random() * AsteroidsSprite.height; hyperCounter = HYPER_COUNT; if (sound & !paused) warpSound.play(); } // 'P' key: toggle pause mode and start or stop any active looping sound clips. if (key == 112) { if (paused) { if (sound && misslePlaying) missleSound.loop(); if (sound && saucerPlaying) saucerSound.loop(); if (sound && thrustersPlaying) thrustersSound.loop(); } else { if (misslePlaying) missleSound.stop(); if (saucerPlaying) saucerSound.stop(); if (thrustersPlaying) thrustersSound.stop(); } paused = !paused; } // 'M' key: toggle sound on or off and stop any looping sound clips. if (key == 109 && loaded) { if (sound) { crashSound.stop(); explosionSound.stop(); fireSound.stop(); missleSound.stop(); saucerSound.stop(); thrustersSound.stop(); warpSound.stop(); } else { if (misslePlaying && !paused) missleSound.loop(); if (saucerPlaying && !paused) saucerSound.loop(); if (thrustersPlaying && !paused) thrustersSound.loop(); } sound = !sound; } // 'D' key: toggle graphics detail on or off. if (key == 100) detail = !detail; // 'S' key: start the game, if not already in progress. if (key == 115 && loaded && !playing) initGame(); return true; } public boolean keyUp(Event e, int key) { // Check if any cursor keys where released and set flags. if (key == Event.LEFT) left = false; if (key == Event.RIGHT) right = false; if (key == Event.UP) up = false; if (key == Event.DOWN) down = false; if (!up && !down && thrustersPlaying) { thrustersSound.stop(); thrustersPlaying = false; } return true; } public void paint(Graphics g) { update(g); } public void update(Graphics g) { Dimension d = size(); int i; int c; String s; // Create the offscreen graphics context, if no good one exists. if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height) { offDimension = d; offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); } // Fill in background and stars. offGraphics.setColor(Color.black); offGraphics.fillRect(0, 0, d.width, d.height); if (detail) { offGraphics.setColor(Color.white); for (i = 0; i < numStars; i++) offGraphics.drawLine(stars[i].x, stars[i].y, stars[i].x, stars[i].y); } // Draw photon bullets. offGraphics.setColor(Color.white); for (i = 0; i < MAX_SHOTS; i++) if (photons[i].active) offGraphics.drawPolygon(photons[i].sprite); // Draw the guided missle, counter is used to quickly fade color to black when near expiration. c = Math.min(missleCounter * 24, 255); offGraphics.setColor(new Color(c, c, c)); if (missle.active) { offGraphics.drawPolygon(missle.sprite); offGraphics.drawLine(missle.sprite.xpoints[missle.sprite.npoints - 1], missle.sprite.ypoints[missle.sprite.npoints - 1], missle.sprite.xpoints[0], missle.sprite.ypoints[0]); } // Draw the asteroids. for (i = 0; i < MAX_ROCKS; i++) if (asteroids[i].active) { if (detail) { offGraphics.setColor(Color.black); offGraphics.fillPolygon(asteroids[i].sprite); } offGraphics.setColor(Color.white); offGraphics.drawPolygon(asteroids[i].sprite); offGraphics.drawLine(asteroids[i].sprite.xpoints[asteroids[i].sprite.npoints - 1], asteroids[i].sprite.ypoints[asteroids[i].sprite.npoints - 1], asteroids[i].sprite.xpoints[0], asteroids[i].sprite.ypoints[0]); } // Draw the flying saucer. if (ufo.active) { if (detail) { offGraphics.setColor(Color.black); offGraphics.fillPolygon(ufo.sprite); } offGraphics.setColor(Color.white); offGraphics.drawPolygon(ufo.sprite); offGraphics.drawLine(ufo.sprite.xpoints[ufo.sprite.npoints - 1], ufo.sprite.ypoints[ufo.sprite.npoints - 1], ufo.sprite.xpoints[0], ufo.sprite.ypoints[0]); } // Draw the ship, counter is used to fade color to white on hyperspace. c = 255 - (255 / HYPER_COUNT) * hyperCounter; if (ship.active) { if (detail && hyperCounter == 0) { offGraphics.setColor(Color.black); offGraphics.fillPolygon(ship.sprite); } offGraphics.setColor(new Color(c, c, c)); offGraphics.drawPolygon(ship.sprite); offGraphics.drawLine(ship.sprite.xpoints[ship.sprite.npoints - 1], ship.sprite.ypoints[ship.sprite.npoints - 1], ship.sprite.xpoints[0], ship.sprite.ypoints[0]); } // Draw any explosion debris, counters are used to fade color to black. for (i = 0; i < MAX_SCRAP; i++) if (explosions[i].active) { c = (255 / SCRAP_COUNT) * explosionCounter [i]; offGraphics.setColor(new Color(c, c, c)); offGraphics.drawPolygon(explosions[i].sprite); } // Display status and messages. offGraphics.setFont(font); offGraphics.setColor(Color.white); offGraphics.drawString("Score: " + score, fontWidth, fontHeight); offGraphics.drawString("Ships: " + shipsLeft, fontWidth, d.height - fontHeight); s = "High: " + highScore; offGraphics.drawString(s, d.width - (fontWidth + fm.stringWidth(s)), fontHeight); if (!sound) { s = "Mute"; offGraphics.drawString(s, d.width - (fontWidth + fm.stringWidth(s)), d.height - fontHeight); } if (!playing) { s = "A S T E R O I D S"; offGraphics.drawString(s, (d.width - fm.stringWidth(s)) / 2, d.height / 2); s = "Copyright 1998 by Mike Hall"; offGraphics.drawString(s, (d.width - fm.stringWidth(s)) / 2, d.height / 2 + fontHeight); if (!loaded) { s = "Loading sounds..."; offGraphics.drawString(s, (d.width - fm.stringWidth(s)) / 2, d.height / 4); } else { s = "Game Over"; offGraphics.drawString(s, (d.width - fm.stringWidth(s)) / 2, d.height / 4); s = "'S' to Start"; offGraphics.drawString(s, (d.width - fm.stringWidth(s)) / 2, d.height / 4 + fontHeight); } } else if (paused) { s = "Game Paused"; offGraphics.drawString(s, (d.width - fm.stringWidth(s)) / 2, d.height / 4); } // Copy the off screen buffer to the screen. g.drawImage(offImage, 0, 0, this); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?