//Kite7.java //Make array of balloons //Andy Harris, 06/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Kite7 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; EventHandler evt = new EventHandler(); Thread looper; boolean keepGoing = true; int score = 0; 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"); 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){ b.x = (int)(Math.random() * getSize().width); b.y = getSize().height - b.height; } else { //move it a little bit in X direction b.x = (int)(Math.random() * 5) - 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(kite[direction], x, y, wd, ht, this); g.drawImage(b.image, b.x, b.y, b.width, b.height, this); g.drawString(String.valueOf(score), 10, 10); } // end for loop } // end paint 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 } // end key checking if } // end key press event } // end eventHandler class def private class Bloon { int x = 100; int y = 100; 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