import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class FLY1 extends Applet implements Runnable{
  Dimension d;  
  Thread tm;
  Random r = new Random();
  Image offI;
  int crashes=0;
  int speed=80;
  int altitude=25;
  int x_plane_body[] = { 10, 40, 7, 0, 10, 17, 10 };
  int y_plane_body[] = {  70, 55, 55, 65, 60, 60, 70 };
  int x_reset_body[] = { 10, 40, 7, 0, 10, 17, 10 };
  int y_reset_body[] = {  70, 55, 55, 65, 60, 60, 70 };
  int x_cockpit[] = { 20, 28, 40, 20 };
  int y_cockpit[] = { 55, 50, 55, 55 };
  int x_reset_cockpit[] = { 20, 28, 40, 20 };
  int y_reset_cockpit[] = { 55, 50, 55, 55 };
  int x_mtn[]={ 0,0,25,80,85,90,150,175,200,225,265,290,305,
                 340,360,415,425,435,500,500,0};
  int y_mtn[]={ 300,260,240,165,160,165,250,282,282,250,125,
                 120,125,250,250,170,178,170,280,300,300};
  Point2D mountain[] = new Point2D[21];

  public void init(){
    d = getSize();
    for(int i = 0; i<x_mtn.length; i++){
      mountain[i] = new Point2D(x_mtn[i],y_mtn[i]);
    }
    offI=createImage(d.width,d.height);
    tm = new Thread(this);
    tm.start();
    requestFocus(); 
    this.addKeyListener(new KeyAdapter(){ 
      public void keyPressed(KeyEvent k) { 
        if(k.getKeyCode() == KeyEvent.VK_UP){
          for(int i = 0; i < y_plane_body.length; i++){
            y_plane_body[i]--;
          }
          for(int i = 0; i < y_cockpit.length; i++){
            y_cockpit[i]--;
          }
        } 
        else if(k.getKeyCode() == KeyEvent.VK_DOWN){
          for(int i = 0; i < y_plane_body.length; i++){
            y_plane_body[i]++;
          }
          for(int i = 0; i < y_cockpit.length; i++){
            y_cockpit[i]++;
          }
        }
        else if(k.getKeyCode() == KeyEvent.VK_LEFT){
          if(speed<150) speed+=10;
        }
        else if(k.getKeyCode() == KeyEvent.VK_RIGHT){
          if(speed>20) speed-=10;
        }
        repaint();
      }
    });
  }

  public boolean collision(){
     if(Tools2D.insidePolygon(new Point2D(x_plane_body[0],y_plane_body[0]), mountain))
       return true;
     else if(Tools2D.insidePolygon(new Point2D(x_plane_body[1],y_plane_body[1]), mountain))
       return true;
     else return false;
  }

  public void reset(){
    for(int i = 0; i<x_reset_body.length; i++){
      x_plane_body[i] = x_reset_body[i];
      y_plane_body[i] = y_reset_body[i];
    }
    for(int i = 0; i<x_reset_cockpit.length; i++){
      x_cockpit[i] = x_reset_cockpit[i];
      y_cockpit[i] = y_reset_cockpit[i];
    }
    crashes++;
  }

  public void run(){
    while(true){
      for(int i = 0; i < x_plane_body.length; i++){
        x_plane_body[i] +=2;
      }
      for(int i = 0; i < x_cockpit.length; i++){
        x_cockpit[i] +=2;
      }
      if(x_plane_body[4]>d.width){
        for(int i = 0; i < x_plane_body.length; i++){
          x_plane_body[i] -= d.width+40;
        }
        for(int i = 0; i < x_cockpit.length; i++){
          x_cockpit[i] -= d.width+40;
        }
      }
      if(collision()) reset();
      repaint();
      try{
        Thread.sleep(speed);
      }catch(InterruptedException e){};
    }
  }

  public void update(Graphics g){
    paint(g);
  }

  public void paint(Graphics g){
    Graphics offG = offI.getGraphics();
    offG.setColor(Color.cyan);
    offG.fillRect(0,0,d.width,d.height);
    Polygon planebody = new Polygon(x_plane_body, y_plane_body, x_plane_body.length);
    Polygon cockpit = new Polygon(x_cockpit, y_cockpit, x_cockpit.length);
    Polygon mtn = new Polygon(x_mtn,y_mtn,x_mtn.length);
    offG.setColor(Color.red);
    offG.fillPolygon(planebody);
    offG.setColor(Color.yellow);
    offG.fillPolygon(cockpit);    
    offG.setColor(new Color(100,150,50));
    offG.fillPolygon(mtn);
    offG.setColor(Color.red);
    offG.setFont(new Font("Courier", Font.BOLD, 24));
    offG.drawString("Crashes: " + crashes, d.width-180, d.height-10);
    g.drawImage(offI, 0, 0, this);
  }
}
