// Java code for a modified Java applet which is a simple arcade-type game // with audio. // Written by: Doug Shiels, 5/1/96 // Modified from: 'Kill 'em' by Benny Pollak import java.awt.*; import java.lang.*; import java.net.*; import java.util.StringTokenizer; import java.util.Vector; import java.applet.AudioClip; class Critter { public Point p_; public Critter() { p_ = new Point(0,0); } public Critter(int x, int y) { if (x < 1) x = 1; if (y < 1) y = 1; p_ = new Point(x, y); } } public class NN extends java.applet.Applet implements Runnable { Thread thread; int speed; public Image nnImage,nnImage2; public Vector critters; boolean bJustStarted, running; int startCount; public AudioClip snd_start, snd_hit, snd_miss, snd_won; NNCanvas canvas; NNControls controls; public void init() { String att = getParameter("speed"); speed = (att == null) ? 2000 : (1000 * Integer.valueOf(att).intValue()); att = getParameter("start_count"); startCount = (att == null) ? 1 : Integer.valueOf(att).intValue(); nnImage = getImage(getDocumentBase(), "images/hammer_sphere_small.gif"); nnImage2 = getImage(getDocumentBase(), "images/reagan_cowboy.gif"); critters = new Vector(); bJustStarted = true; running=false; try { snd_hit = getAudioClip(new URL(getDocumentBase(), "audio/homer-wohoo.au")); snd_miss=getAudioClip(new URL(getDocumentBase(),"audio/homer-doh.au")); snd_won =getAudioClip(new URL(getDocumentBase(),"audio/goal.au")); snd_start =getAudioClip(new URL(getDocumentBase(),"audio/outlaw.au")); } catch (MalformedURLException e){} setLayout(new BorderLayout()); canvas = new NNCanvas(this); canvas.setBackground(Color.black); controls = new NNControls(this); add("Center", canvas); add("South", controls); } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { System.exit(0); } return false; } public void clearDots() { synchronized(critters) { int i; for (i = critters.size()-1; i >= 0; i--) { critters.removeElementAt(i); } } canvas.repaint(); } public void start() { thread = new Thread(this); thread.start(); } public void stop() { thread.stop(); } public void addCritter() { synchronized(critters) { Dimension dim = canvas.size(); if (dim.width != 0 && dim.height != 0) { int count = bJustStarted ? startCount : 1; while (count-- > 0) { int x = 250; int y = 250; while((x>185) && (x<290) && (y>180) && (y<300) && (x<475) && (y<475)) { x = (int)(Math.random()*(dim.width-1-nnImage.getWidth(this))); y = (int)(Math.random()*(dim.height-1-nnImage.getHeight(this))); } critters.addElement(new Critter(x, y)); } bJustStarted = false; } canvas.repaint(); } } public void run() { while (true) { while (running) { addCritter(); try { Thread.currentThread().sleep(speed); } catch (InterruptedException e){} } try { Thread.currentThread().sleep(speed); } catch (InterruptedException e){} } } } class NNControls extends Panel { NN app_; public Checkbox sound; public Button button; public NNControls(NN app) { app_ = app; add(button = new Button("Start")); add(sound = new Checkbox("Sound")); add(button = new Button("Pause")); sound.setState(true); } public boolean action(Event ev, Object arg) { if (ev.target instanceof Button) { String label = (String)arg; if (arg == "Start") { app_.clearDots(); if (app_.controls.sound.getState()) app_.snd_start.play(); app_.running=true; } if (arg == "Pause") { if (app_.running==true) app_.running=false; else app_.running=true; } return true; } return false; } } class NNCanvas extends Canvas { NN app_; int prevw, prevh; Image offimage; public NNCanvas(NN app) { app_ = app; } public void paint(Graphics g) { update(g); } public synchronized void update(Graphics g) { Dimension d = size(); try { if (offimage == null || d.width != prevw || d.height != prevh) { prevw = d.width; prevh = d.height; offimage = createImage(d.width, d.height); } Graphics offg = offimage.getGraphics(); offPaint(offg); g.drawImage(offimage, 0, 0, Color.black, this); } catch (java.lang.OutOfMemoryError e) { System.out.println("Applet Banner ran out of memory"); System.exit(1); } //Image offimage = createImage(d.width, d.height); //Graphics offg = offimage.getGraphics(); //offPaint(offg); //g.drawImage(offimage, 0, 0, this); } void offPaint(Graphics g) { Dimension d = size(); g.setColor(Color.black); g.fillRect(0, 0, d.width, d.height); g.setColor(Color.red); g.drawLine(0, 0, d.width-1, 0); g.drawLine(d.width-1, 0, d.width-1, d.height-1); g.drawLine(d.width-1, d.height-1, 0, d.height-1); g.drawLine(0, d.height-1, 0, 0); int i; g.drawImage(app_.nnImage2, 219, 209, Color.black, this); synchronized(app_.critters) { for (i = 0; i < app_.critters.size(); i++) { Critter critter = (Critter)app_.critters.elementAt(i); int x = critter.p_.x; int y = critter.p_.y; g.drawImage(app_.nnImage, critter.p_.x, critter.p_.y, Color.black, this); } } //app_.controls.repaint(); } public boolean keyDown(Event evt, int key) { if (key == ' ') { app_.addCritter(); repaint(); } return true; } public boolean mouseUp(Event evt, int x, int y) { int w = app_.nnImage.getWidth(this); int h = app_.nnImage.getHeight(this); boolean found = false; int i; Graphics g = getGraphics(); g.setColor(Color.yellow); g.drawLine(240,246,x,y); g.drawLine(258,248,x,y); synchronized(app_.critters) { for (i = app_.critters.size()-1; !found && i >= 0; i--) { Critter critter = (Critter)app_.critters.elementAt(i); int px = critter.p_.x; int py = critter.p_.y; if (x > px && x < px+w && y > py && y < py+h) { app_.critters.removeElementAt(i); found = true; } } } if (found) { if (app_.critters.size() == 0) { if (app_.controls.sound.getState()) app_.snd_won.play(); app_.running=false; } else { if (app_.controls.sound.getState()) app_.snd_hit.play(); } } else { if (app_.controls.sound.getState()) app_.snd_miss.play(); } repaint(); return true; } }