import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.net.MalformedURLException; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JPanel; // contains the drawing of the various elements of the game, such as the /* * The Arena is where the main action happens. It is responsible for all graphics * and sounds, and runs the secondary thread to move the aliens, control the shots, * etc. */ public class Arena extends JPanel implements Runnable { // The Thread that will act as secondary thread to control aliens and shots Thread thread = null; // (x,y) coordinates of the single 'alien' int xUFO = 10; int yUFO = 10; // height, width of the single 'alien' int wUFO = 40; int hUFO = 40; // direction of the alien ufo (1 moves left to right, -1 move right to left) int dirUFO = 1; // speed of the alien ufo, in ‘clock ticks’ int speedUFO = 30; int xBullet; int yBullet; int speedBullet = 1; // speed of the thread, i.e. the alien movement by providing ‘clock ticks’ int sleepTime = 5; long tick = 0L; AudioClip ufo = null; AudioClip fire = null; AudioClip die = null; ImageIcon icon1 = null; ImageIcon icon2 = null; ImageIcon icon = null; // Constructor. Is called automatically whenever a new Arena is created. Includes // setup instructions and other initializations (we’ll add those later). public Arena() { try { ufo = Applet.newAudioClip(new URL("file:" + System.getProperty("user.dir") + "/invader.wav")); fire = Applet.newAudioClip(new URL("file:" + System.getProperty("user.dir") + "/shoot.wav")); die = Applet.newAudioClip(new URL("file:" + System.getProperty("user.dir") + "/invaderkilled.wav")); icon1 = new ImageIcon(new URL("file:" + System.getProperty("user.dir") + "/invader1.jpg")); icon2 = new ImageIcon(new URL("file:" + System.getProperty("user.dir") + "/invader2.jpg")); icon = icon1; yBullet = getHeight(); xBullet = getWidth()/2; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Starts a new thread if no thread is currently active. The thread will execute // the 'run' method automatically when started because of the this use. public void start() { if (thread == null) { thread = new Thread(this); thread.start(); xBullet = getWidth()/2; yBullet = getHeight(); } } // Stops the current thread from running. Can be restarted by calling 'start' public void stop() { thread = null; } // Run method controls the game by moving the alien space ships, bullets, etc. // Also needs to contain code checking for any hits, update the score, etc. public void run() { while (thread != null) { if ((tick % speedBullet) == 0) { yBullet = yBullet - 2; } if ((tick % speedUFO) == 0) { if (xUFO >= getWidth()-wUFO) { dirUFO = -1; yUFO = yUFO + hUFO; xUFO = getWidth() - wUFO - 1; } else if (xUFO < 0) { dirUFO = 1; yUFO = yUFO + hUFO; xUFO = 1; } xUFO = xUFO + dirUFO*wUFO/2; if (icon.getImage() == icon1.getImage()) icon = icon2; else icon = icon1; ufo.play(); } detectHit(); if (yUFO > getHeight()) stop(); try { tick = tick + 1; thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } repaint(); } } public void detectHit() { if ((xBullet > xUFO) && (xBullet < xUFO + wUFO) && (yBullet > yUFO) && (yBullet < yUFO + hUFO)) { speedUFO = speedUFO - 5; die.play(); stop(); } } // Draws everything, using variables for their locations. If the variables // change, the corresponding object is drawn at its new location. Standard method // for all Java program containing some custom drawing. public void paintComponent(Graphics g) { // the following line must be the first line in this method super.paintComponent(g); g.drawImage(icon.getImage(), xUFO, yUFO, wUFO, wUFO, null); xBullet = getWidth()/2; g.setColor(Color.red); g.fillOval(xBullet, yBullet, 6, 6); } public void fire() { yBullet = getHeight(); fire.play(); } }