import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class tanks extends Applet { private Button sbutton = new Button("Start"); private Image tankimg; // image of tank public Image skullimg; public int twidth = 30; // height and width of tank public int theight = 30; private Image buffer; // the off-screen buffer public int ah, aw; // width and height of applet public int yoff = 40; // y offset private tank[] H; // array of tanks public target[] Targ; // only some info are accessable! public int tanknum = 2; // number of tanks in race public boolean gameInProgress = false; AnimationMonitor monitor; // all drawing through here int ioff = 10; // image offset (for gun) public void init() { int i; ioff = twidth / 3; skullimg = getImage(getDocumentBase(), "skull9.gif"); // get image gif prepareImage(skullimg,this); tankimg = createImage(twidth,theight); Graphics tgc; tgc = tankimg.getGraphics(); tgc.setColor(Color.red); tgc.fillRect(0,0,twidth,theight); tgc.setColor(Color.black); // black turet tgc.fillOval(6,6,18,18); tgc.setColor(Color.red); H = new tank[tanknum]; // create tankarray Targ = new target[tanknum]; aw = 500; // applet width and height ah = 400; // make two tanks!! int ix = (int) (Math.random() * aw); // random initial position int iy = (int) (Math.random() * ah); H[0] = new tank1(0,tankimg,ix,iy,this); // create threads Targ[0] = new target(ix,iy); // target access Targ[0].gunposition = H[0].gunposition; ix = (int) (Math.random() * aw); // random initial position iy = (int) (Math.random() * ah); H[1] = new tank2(1,tankimg,ix,iy,this); // create threads Targ[1] = new target(ix,iy); // target access Targ[1].gunposition = H[1].gunposition; setBackground(Color.white); Image track = createImage(aw,yoff+ah); // static buffer buffer = createImage(aw,yoff+ah); // double buffer Graphics gc; gc = track.getGraphics(); // gc prepares static image gc.setColor(Color.green); gc.fillRect(0,0,aw,ah); // green background // creates animation monitior monitor = new AnimationMonitor(tanknum,track,buffer,this); sbutton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { showStatus("And off they go..."); gameInProgress = true; monitor.prepare(); // prepare background image for(int j=0;j ( Math.sqrt( ((gx-T.xpos)*(gx-T.xpos)) + ((gy-T.ypos)*(gy-T.ypos))))); boolean temp = (Math.abs(ty-T.ypos) < 14); if (slope != 0.0) // do same for x cord proximity { int tx = (int) ((T.ypos-ypos)/slope) + xpos; temp = temp || (Math.abs(tx-T.xpos) < 14); } return (infront && temp); } // end insight public boolean insight(target T) // !!!! { return (inrange(T) && aligned(T)); } protected void fire() { if (target_insight()) // firing at no target has no effect, sorry. { double gundr = gunposition * 0.01745; // radiant conversion int gx = xpos + ((int) (25.0 * Math.sin(gundr))); int gy = ypos + ((int) (25.0 * Math.cos(gundr))); double slope = (1.0 * (gy-ypos)) / (1.0* (gx - xpos)); int tx = ((int) ( gunrange * Math.cos(gundr) )) + gx; // int ty = (int) (slope * (tx - gx)) + gy; A.monitor.fire(this,gx,gy,tx,ty,slope); // firing must be synched try { Thread.sleep(40); } catch (Exception e) {} // delay } } protected void rotateTurret(int newdir) // left or right? { if (newdir == left) gunposition = (gunposition + rotangle) % 360; else if (newdir == right) { gunposition = gunposition - rotangle; if (gunposition < 0) gunposition = gunposition + 360; } A.monitor.drawto(ID,img,xpos,ypos,gunposition); // request draw try { Thread.sleep(40); } catch (Exception e) {} // delay } // end rotateTurret protected void rotateTowards(target T) // doesn't work yet { double tdr = Math.atan(T.xpos/T.ypos); // degree of target int td = (int) (tdr * 57.2958); // radian to degree if (gunposition > td) { int a = gunposition - td; int b = 360 - gunposition + td; if (a > b) rotateTurret(left); else rotateTurret(right); } else { int a = td - gunposition; int b = 360 - td + gunposition; if (a > b) rotateTurret(right); else rotateTurret(left); } } // end rotateTowards private void awayfrom(int x2, int y2) // prevents deadlock { if (xpos > x2) xpos = xpos + 2; else xpos = xpos - 2; if (ypos > y2) ypos = ypos + 2; else ypos = ypos - 2; } protected void move(int dir) // move in a certain direction { int newx = xpos; int newy = ypos; boolean change = true; if (dir == north) { newy = newy - 4; if (newyah-15) newy = ah-15; } else if (dir == east) { newx = newx + 4; if (newx>aw-15) newx = aw-15; } else if (dir == west) { newx = newx - 4; if (newx < 15) newx = 15; } xpos = newx; ypos = newy; // detect collision for(int i=0; i