//Kite8.java //Add end of game //Andy Harris, 06/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Kite8 extends Applet implements Runnable { public static final int NORTH = 0; public static final int NORTHEAST = 1; public static final int EAST = 2; public static final int SOUTHEAST = 3; public static final int SOUTH = 4; public static final int SOUTHWEST = 5; public static final int WEST = 6; public static final int NORTHWEST = 7; Image kite[] = new Image[8]; int direction = NORTH; int dx = 0; int dy = 0; int x = 0; int y = 0; int wd = 25; int ht = 25; //balloon values Bloon balloon[] = new Bloon[5]; int bCounter = 0; //sounds AudioClip sndPop; AudioClip sndHonk; EventHandler evt = new EventHandler(); Thread looper; boolean keepGoing = true; int score = 0; int lives = 3; public void init(){ //get the kite images kite[NORTH] = getImage (getDocumentBase(), "kiteN.gif"); kite[NORTHEAST] = getImage (getDocumentBase(), "kiteNE.gif"); kite[EAST] = getImage (getDocumentBase(), "kiteE.gif"); kite[SOUTHEAST] = getImage (getDocumentBase(), "kiteSE.gif"); kite[SOUTH] = getImage (getDocumentBase(), "kiteS.gif"); kite[SOUTHWEST] = getImage (getDocumentBase(), "kiteSW.gif"); kite[WEST] = getImage (getDocumentBase(), "kiteW.gif"); kite[NORTHWEST] = getImage (getDocumentBase(), "kiteNW.gif"); //Initialize the balloons for (bCounter = 0; bCounter < 5; bCounter++){ balloon[bCounter] = new Bloon(getImage( getDocumentBase(), "balloonRed.gif")); } // end for loop //get the audio sndPop = getAudioClip(getDocumentBase(), "pop.au"); sndHonk = getAudioClip(getDocumentBase(), "honk.au"); this.setBackground(new Color(232, 232, 255)); this.addKeyListener(evt); this.requestFocus(); } // end init public void start(){ if (looper == null){ looper = new Thread(this); keepGoing = true; looper.start(); } // end if } // end start public void stop(){ keepGoing = false; looper = null; } // end stop public void run(){ while (keepGoing){ try { looper.sleep(100); } catch (InterruptedException exc){ System.out.println(exc.getMessage()); } // end try checkDirection(); upDateKite(); checkCollision(); moveBalloon(); repaint(); } // end while } // end run public void checkDirection(){ switch (direction){ case NORTH: dx = 0; dy = -10; break; case NORTHEAST: dx = 7; dy = -7; break; case EAST: dx = 10; dy = 0; break; case SOUTHEAST: dx = 7; dy = 7; break; case SOUTH: dx = 0; dy = 10; break; case SOUTHWEST: dx = -7; dy = 7; break; case WEST: dx = -10; dy = 0; break; case NORTHWEST: dx = -7; dy = -7; break; } // end switch } // end checkDirection public void upDateKite(){ x += dx; if (x < 0){ x = 0; } // end if int appWidth = this.getSize().width; appWidth -= (kite[direction].getWidth(this)/4); if (x > appWidth){ x = appWidth; } // end if y += dy; if (y < 0){ y = 0; } // end if int appHeight = this.getSize().height; appHeight -= (kite[direction].getHeight(this)/4); if (y > appHeight){ y = appHeight; } //end if } // end upDateKite(); public void checkCollision(){ int kLeft = x; int kRight = x + wd; int kTop = y; int kBottom = y + ht; for (bCounter = 0; bCounter < 5; bCounter++){ Bloon b = balloon[bCounter]; //incorporate margin int bLeft = b.x - b.margin; int bRight = b.x + b.width + b.margin; int bTop = b.y - b.margin; int bBottom = b.y + b.height + b.margin; if(kLeft > bLeft){ if (kRight < bRight){ if (kTop > bTop){ if (kBottom < bBottom ){ sndPop.play(); score += kTop; //reset balloon b.x = (int)(Math.random() * getSize().width); b.y = getSize().height - b.height; } // end if } // end if } // end if } // end if } // end for loop } // end checkCollision public void moveBalloon(){ for (bCounter = 0; bCounter < 5; bCounter++){ Bloon b = balloon[bCounter]; b.y -= 2; if (b.y < 0){ //lose a life and reset kite sndHonk.play(); b.x = (int)(Math.random() * getSize().width); b.y = getSize().height - b.height; lives--; } else { //move it a little bit in X direction b.x = (int)(Math.random() * 6) - 3 + b.x; if (b.x < b.width){ b.x = 15; } else if (b.x >this.getSize().width){ b.x = this.getSize().width - b.width; } // end 'bound checking' if } // end 'off top of screen' if } // end for loop } // end moveBalloon public void paint(Graphics g){ for (bCounter = 0; bCounter < 5; bCounter++){ Bloon b = balloon[bCounter]; g.drawImage(b.image, b.x, b.y, b.width, b.height, this); } // end for loop g.drawImage(kite[direction], x, y, wd, ht, this); g.drawString(String.valueOf(score), 10, 10); if (lives == 3){ g.drawImage(kite[NORTH], 525, 10, 25, 25, this); } // end if if (lives >= 2){ g.drawImage(kite[NORTH], 550, 10, 25, 25, this); } // end if if (lives >= 1){ g.drawImage(kite[NORTH], 575, 10, 25, 25, this); } // end if if (lives < 0){ keepGoing = false; g.setColor(Color.red); g.setFont(new Font("SansSerif", Font.BOLD, 80)); g.drawString ("Game Over", 100, 100); g.setFont(new Font("SansSerif", Font.BOLD, 50)); g.drawString ("ESC to start over", 100, 200); g.setFont(new Font("SansSerif", Font.BOLD, 12)); g.setColor(Color.black); } // end if } // end paint public void restart(){ lives = 3; score = 0; keepGoing = true; looper = null; looper = new Thread(this); looper.start(); for (bCounter = 0; bCounter < 5; bCounter++){ balloon[bCounter].x = 300; balloon[bCounter].y = 400; } // end for } // end restart private class EventHandler extends KeyAdapter { public void keyPressed(KeyEvent e){ if (e.getKeyCode() == KeyEvent.VK_LEFT){ direction--; if (direction < NORTH) { direction = NORTHWEST; } // end if } else if (e.getKeyCode() == KeyEvent.VK_RIGHT){ direction++; if (direction > NORTHWEST){ direction = NORTH; } // end if } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){ restart(); } // end key checking if } // end key press event } // end eventHandler class def private class Bloon { int x = 300; int y = 400; int width = 15; int height = 25; int margin = 10; Image image; public Bloon(Image img){ image = img; } // end constructor } // end balloon class def } // end kite class def