/* gremlin goes beserk, goblin goes after human */ import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; public class goblinapp2b extends JFrame implements KeyListener { private Graphics brush; private Graphics display; private Image canvas; // off-screen graphics buffer private human professor; // professor object private goblin stu; private goblin ent; private diamond treasure; private static final Color bkcolor = Color.black; private static final int XBOUND = 800; private static final int YBOUND = 600; private static final int STEP = 4; private static final int FDELAY = 50; public static void main(String[] args) // needed for application { goblinapp2b session = new goblinapp2b(); session.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); session.init(); } public void paint(Graphics g) {} // overrides auto-update public void init() { setBounds(0,0,XBOUND,YBOUND); show(); canvas = createImage(XBOUND,YBOUND); brush = canvas.getGraphics(); display = this.getGraphics(); brush.setColor(bkcolor); // clear brush.fillRect(0,0,XBOUND,YBOUND); // with black background brush.setColor(Color.blue); addKeyListener(this); requestFocus(); // keys go to applet animate(); } // init public void clearbuffer() { Color old; old = brush.getColor(); brush.setColor(bkcolor); // clear brush.fillRect(0,0,XBOUND,YBOUND); // with black background brush.setColor(old); // restores color } // clearbuffer public void nextframe(int delay) // delay in ms { try { Thread.sleep(delay); } catch (InterruptedException IE) {} display.drawImage(canvas,0,0,null); // draws to screen clearbuffer(); } // nextframe with ms delay // event handling public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); // key pressed if (key == KeyEvent.VK_UP) professor.setdirection(0,-1*STEP); // move up if (key == KeyEvent.VK_DOWN) professor.setdirection(0,STEP); if (key == KeyEvent.VK_LEFT) professor.setdirection(-1*STEP,0); if (key == KeyEvent.VK_RIGHT) professor.setdirection(STEP,0); if (key == KeyEvent.VK_SPACE) professor.setdirection(0,0); // stop // else do nothing - direction remains the same } /* ----------------******************------------------ */ public int randint(int min, int max) { return (int) (Math.random() * (max-min+1) + min); } public void animate() { int x1; int y1; // hold numbers int x2; int y2; int dx; int dy; // direction to set for goblin professor = new human(XBOUND-50,YBOUND-50,brush); stu = new goblin(50,50,brush,"gremlin1.gif"); goblin dent = new goblin(XBOUND-50,50,brush,"zerg1.gif"); treasure = new diamond(XBOUND/2,YBOUND/2,brush); stu.setdirection(12,10); try {Thread.sleep(1000);} catch (Exception ee) {} boolean stop = false; while (!stop) { // goblin moves quickly // set second goblin's course vector: moves faster! x1 = dent.getx(); y1 = dent.gety(); x2 = professor.getx(); y2 = professor.gety(); if (x2>x1) dx = STEP+1; else dx = -1*(STEP+1); if (y2>y1) dy = STEP+1; else dy = -1*(STEP+1); dent.setdirection(dx,dy); stu.move(); dent.move(); treasure.move(); professor.move(); nextframe(FDELAY); if (stu.overlap(professor) || dent.overlap(professor)) { display.setFont(new Font("Serif",Font.BOLD,24)); display.setColor(Color.red); display.drawString("The Goblins Win!",XBOUND/2-100,YBOUND/2); stop = true; } if (treasure.overlap(professor)) { display.setFont(new Font("Serif",Font.BOLD,24)); display.setColor(Color.red); display.drawString("The Professor Wins!",XBOUND/2-100,YBOUND/2); stop = true; } } } // animate } // class goblinapp2 /* ************************************************* */ class human { private Image img; // gif private int xcord, ycord; // coordinates of center private int dx, dy; // movement vector private Graphics brush; private int radius; private static final int XBOUND = 800; private static final int YBOUND = 600; // accessor methods public int getx() { return xcord; } public int gety() { return ycord; } public int getr() { return radius; } public int getdx() { return dx; } public int getdy() { return dy; } public human(int x0, int y0, Graphics b0) // constructor { xcord = x0; ycord = y0; // change following line to correct location of gif: img = Toolkit.getDefaultToolkit().getImage("/shared/csc/local/stuff/man15.gif"); radius = 15; brush = b0; } public void draw() { brush.drawImage(img,xcord-radius,ycord-radius,null); //brush.drawOval(xcord-radius,ycord-radius,2*radius,2*radius); } public void setdirection(int newdx, int newdy) { dx = newdx; dy = newdy; } public void move() { xcord = xcord + dx; if (xcord < radius/2) xcord = XBOUND-(radius/2); if (xcord > XBOUND-(radius/2)) xcord = radius/2; ycord = ycord + dy; if (ycord < radius/2) ycord = YBOUND-(radius/2); if (ycord > YBOUND-(radius/2)) ycord = radius/2; draw(); } } // end class human class goblin { private Image img; // gif private int xcord, ycord; // coordinates of center private int dx, dy; // movement vector private Graphics brush; private int radius; private static final int XBOUND = 800; private static final int YBOUND = 600; // accessor methods public int getx() { return xcord; } public int gety() { return ycord; } public int getr() { return radius; } public int getdx() { return dx; } public int getdy() { return dy; } public goblin(int x0, int y0, Graphics b0, String giffile) // constructor { xcord = x0; ycord = y0; img = Toolkit.getDefaultToolkit().getImage("/shared/csc/local/stuff/"+giffile); radius = 30; brush = b0; } public void draw() { brush.drawImage(img,xcord-radius,ycord-radius,null); //brush.drawOval(xcord-radius,ycord-radius,2*radius,2*radius); } public void setdirection(int newdx, int newdy) { dx = newdx; dy = newdy; } public void move() // bounces off { if (xcord < radius) { xcord = radius; dx = -1 * dx; } else if (xcord > (XBOUND-radius)) { xcord = XBOUND-radius; dx = -1 * dx; } else xcord = xcord + dx; if (ycord < radius) { ycord = radius; dy = -1 * dy; } else if (ycord > (YBOUND-radius)) { ycord = YBOUND-radius; dy = -1 * dy; } else ycord = ycord + dy; draw(); } public int randint(int min, int max) { return (int) (Math.random() * (max-min+1) + min); } public double dist(int x1, int y1, int x2, int y2) { int dx = (x1-x2) * (x1-x2); int dy = (y1-y2) * (y1-y2); return Math.sqrt(dx+dy); } // detects if goblin's circle overlaps that of human: public boolean overlap(human h) { int x2, y2, r2; x2 = h.getx(); y2 = h.gety(); r2 = h.getr(); return dist(xcord,ycord,x2,y2) <= (radius + r2 - 5); } } // end class goblin class diamond { private Image img; // gif private int xcord, ycord; // coordinates of center private Graphics brush; private int radius; private static final int XBOUND = 800; private static final int YBOUND = 600; // accessor methods public int getx() { return xcord; } public int gety() { return ycord; } public int getr() { return radius; } public diamond(int x0, int y0, Graphics b0) // constructor { xcord = x0; ycord = y0; img = Toolkit.getDefaultToolkit().getImage("/shared/csc/local/stuff/gem1.gif"); radius = 12; brush = b0; } public void draw() { brush.drawImage(img,xcord-radius,ycord-radius,null); //brush.drawOval(xcord-radius,ycord-radius,2*radius,2*radius); } public void move() // bounces off { int dx, dy; dx = randint(-4,4); dy = randint(-4,4); if ((xcord+dx > radius/2) && (xcord+dx < XBOUND-(radius/2))) xcord = xcord + dx; if ((ycord+dy > radius/2) && (ycord+dy < YBOUND-(radius/2))) ycord = ycord + dy; draw(); } public int randint(int min, int max) { return (int) (Math.random() * (max-min+1) + min); } public double dist(int x1, int y1, int x2, int y2) { int dx = (x1-x2) * (x1-x2); int dy = (y1-y2) * (y1-y2); return Math.sqrt(dx+dy); } // detects if diamond's circle overlaps that of human: public boolean overlap(human h) { int x2, y2, r2; x2 = h.getx(); y2 = h.gety(); r2 = h.getr(); return dist(xcord,ycord,x2,y2) <= (radius + r2 - 5); } } // end class diamond