public void reset() { // init the location of the rocket to the center. rocketXPos = getWidth2() / 2; rocketYPos = getHeight2() / 2; // rocket direction rocketRight = true; // rocket speed. rocketXSpeed = 0; rocketYSpeed = 0; rocketMaxSpeed = 10; // rocket lives rocketLife = 3; // star position. starXPos = new int[numStars]; starYPos = new int[numStars]; starActive = new boolean[numStars]; for (int index = 0; index < numStars; index++) { starXPos[index] = (int) (Math.random() * getWidth2()); starYPos[index] = (int) (Math.random() * getHeight2()); starActive[index] = true; } // score stuff score = 0; // missile stuff missile = new Missile[Missile.numMissile]; for (int index = 0; index < missile.length; index++) { missile[index] = new Missile(); } Missile.currentIndex = 0; starHit = -1; gameOver = false; }
private static void playres(Resource res) { Collection<Resource.Audio> clips = res.layers(Resource.audio); int s = (int) (Math.random() * clips.size()); Resource.Audio clip = null; for (Resource.Audio cp : clips) { clip = cp; if (--s < 0) break; } if (clip != null) { play(clip.clip); } }
public void reset() { // init the location of the rocket to the center. rocketXPos = getWidth2() / 2; rocketYPos = getHeight2() / 2; star = new Star[starNum]; missle = new Missle[missleNum]; firsttouch = new boolean[starNum]; for (int i = 0; i < starNum; i++) { star[i] = new Star(20, 20); star[i].setXPos((int) (Math.random() * getWidth2())); star[i].setYPos((int) (Math.random() * getHeight2())); while (star[i].getLeftSide() < rocketXPos + Rocketwidth / 2 && star[i].getRightSide() > rocketXPos - Rocketwidth / 2 && star[i].getTop() > rocketYPos - Rocketheight / 2 && star[i].getBottom() < rocketYPos + Rocketheight / 2) { star[i].setXPos((int) (Math.random() * getWidth2())); star[i].setYPos((int) (Math.random() * getHeight2())); } firsttouch[i] = false; } speed = 0; for (int i = 0; i < missle.length; i++) { missle[i] = new Missle(10, 10); } missleIndex = 0; Rocketwidth = rocketImage.getWidth(this); Rocketheight = rocketImage.getHeight(this); score = 0; lives = 3; GameOver = false; ff = false; ffwidth = 25; }
// Transitions from Overworld to Battle scene public void battleBegin() { scene = BATTLE; moveUp = false; moveDown = false; moveLeft = false; moveRight = false; // Background changer int bgSelector = (int) (10 * Math.random() + 1); if (bgSelector > 5) { ImageIcon bg = new ImageIcon("BattleBG1.jpg"); background = bg.getImage(); battleBG = 1; player.battleBegin(235, 380); int placed = 0; for (int enemyNum = 0; enemyNum < enemies.size(); enemyNum++) { Enemy enemy = enemies.get(enemyNum); if (enemy.getActivity()) { enemy.battleBegin(440 - 20 * placed, 125); placed++; totalExperience += enemy.getEXP(); } } } else { ImageIcon bg = new ImageIcon("BattleBG2.png"); background = bg.getImage(); battleBG = 2; player.battleBegin(650, 365); int placed = 0; for (int loop = 0; loop < enemies.size(); loop++) { if (enemies.get(loop).getActivity()) { enemies.get(loop).battleBegin(340 - 20 * placed, 145); placed++; totalExperience += enemies.get(loop).getEXP(); } } } menuScreen.setVisible(false); staminaTimer.start(); }
public void animate() { if (animateFirstTime) { animateFirstTime = false; if (xsize != getSize().width || ysize != getSize().height) { xsize = getSize().width; ysize = getSize().height; } readFile(); outerSpaceImage = Toolkit.getDefaultToolkit().getImage("./Images/outerSpace.jpg"); rocketImage = Toolkit.getDefaultToolkit().getImage("./Images/rocket.GIF"); bgSound = new sound("Sound/starwars.wav"); highscore = 0; reset(); } if (GameOver) { rocketImage = Toolkit.getDefaultToolkit().getImage("./Images/rocket.GIF"); return; } else if (speed > 0) { rocketRight = true; rocketImage = Toolkit.getDefaultToolkit().getImage("./Images/animrocket.GIF"); } else if (speed < 0) { rocketRight = false; rocketImage = Toolkit.getDefaultToolkit().getImage("./Images/animRocket.GIF"); } else rocketImage = Toolkit.getDefaultToolkit().getImage("./Images/rocket.GIF"); if (ff) ffsize += .5; if (upspeed + rocketYPos > getHeight2()) { upspeed = 0; rocketYPos = getHeight2(); } else if (upspeed + rocketYPos < 0) { upspeed = 0; rocketYPos = 0; } else rocketYPos += upspeed; for (int i = 0; i < starNum; i++) { star[i].setXPos(star[i].getXPos() + speed); if (star[i].getXPos() < 0) { star[i].setXPos(getWidth2()); star[i].setYPos((int) (Math.random() * getHeight2())); } else if (star[i].getXPos() > getWidth2()) { star[i].setXPos(0); star[i].setYPos((int) (Math.random() * getHeight2())); } if (star[i].getLeftSide() < rocketXPos + Rocketwidth / 2 && star[i].getRightSide() > rocketXPos - Rocketwidth / 2 && star[i].getTop() > rocketYPos - Rocketheight / 2 && star[i].getBottom() < rocketYPos + Rocketheight / 2) { if (firsttouch[i] == false) { zsound = new sound("Sound/ouch.wav"); firsttouch[i] = true; lives--; } } else { firsttouch[i] = false; } if (ffsize == 2) { int a = rocketYPos - star[i].getYPos(); int b = rocketXPos - star[i].getXPos(); if (a < 0) a *= -1; if (b < 0) b *= -1; if (Math.sqrt((a * a) + (b * b)) <= ffwidth * 5 && ff) { if (!rocketRight) star[i].setXPos(getWidth2()); else star[i].setXPos(0); star[i].setYPos((int) (Math.random() * getHeight2())); score++; } } } for (int i = 0; i < missle.length; i++) { if (missle[i].getActive()) { if (missle[i].getRight()) missle[i].setXPos(missle[i].getXPos() - 2); else missle[i].setXPos(missle[i].getXPos() + 2); for (int i2 = 0; i2 < starNum; i2++) { if (star[i2].getLeftSide() < missle[i].getRightSide() && star[i2].getRightSide() > missle[i].getLeftSide() && star[i2].getTop() > missle[i].getBottom() && star[i2].getBottom() < missle[i].getTop()) { if (!rocketRight) star[i2].setXPos(getWidth2()); else star[i2].setXPos(0); star[i2].setYPos((int) (Math.random() * getHeight2())); missle[i].setActive(false); score++; } } } } if (lives == 0) { GameOver = true; if (score > highscore) highscore = score; } if (bgSound.donePlaying) { bgSound = new sound("Sound/starwars.wav"); } if (ffsize >= 2) { ff = false; ffsize = 1.0; } TimeCount++; }
public void animate() { if (animateFirstTime) { animateFirstTime = false; if (xsize != getSize().width || ysize != getSize().height) { xsize = getSize().width; ysize = getSize().height; } readFile(); outerSpaceImage = Toolkit.getDefaultToolkit().getImage("./outerSpace.jpg"); rocketImage = Toolkit.getDefaultToolkit().getImage("./rocket.GIF"); reset(); } if (gameOver) return; if (rocketLife <= 0) gameOver = true; // missile stuff for (int index = 0; index < missile.length; index++) { if (missile[index].active) { if (missile[index].missileRight) missile[index].xPos += 5; else missile[index].xPos -= 5; if (missile[index].xPos >= getWidth2() || missile[index].xPos <= 0) missile[index].active = false; } } // star speeds for (int index = 0; index < numStars; index++) { starXPos[index] -= rocketXSpeed; } rocketYPos -= rocketYSpeed; // stop rocket at edges of screen. if (rocketYPos >= getHeight2()) { rocketYSpeed = 0; rocketYPos = getHeight2(); } else if (rocketYPos <= 0) { rocketYSpeed = 0; rocketYPos = 0; } for (int index = 0; index < numStars; index++) { if (starXPos[index] > getWidth2()) { starYPos[index] = (int) (Math.random() * getHeight2()); starXPos[index] = 0; } else if (starXPos[index] < 0) { starYPos[index] = (int) (Math.random() * getHeight2()); starXPos[index] = getWidth2(); } } boolean hit = false; for (int index = 0; index < numStars; index++) { if (starActive[index]) { if (starXPos[index] - 10 < rocketXPos && starXPos[index] + 10 > rocketXPos && starYPos[index] - 10 < rocketYPos && starYPos[index] + 10 > rocketYPos) { hit = true; if (starHit != index) { starHit = index; zsound = new sound("ouch.wav"); rocketLife--; } } } } for (int count = 0; count < numStars; count++) { for (int index = 0; index < missile.length; index++) { if (missile[index].active && starActive[count]) { if (starXPos[count] - 10 < missile[index].xPos && starXPos[count] + 10 > missile[index].xPos && starYPos[count] - 10 < missile[index].yPos && starYPos[count] + 10 > missile[index].yPos) { missile[index].active = false; starActive[count] = false; score++; if (score > highScore) highScore = score; } } } } if (!hit) starHit = -1; if (rocketXSpeed > 0) rocketRight = true; else if (rocketXSpeed < 0) rocketRight = false; }
public void writeNoise(byte[] b, int velocity) { for (int i = 0; i < b.length; i++) { b[i] = (byte) Math.floor(Math.random() * velocity); } }