xthexder

Untitled

May 27th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Point;
  8. import java.awt.Polygon;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.image.BufferedImage;
  12.  
  13. import javax.swing.JFrame;
  14.  
  15. public class HelicopterGame extends Applet implements MouseListener {
  16.  
  17.     private static final long serialVersionUID = 1L;
  18.  
  19.     int screen;
  20.     double direction;
  21.     boolean goingUp;
  22.     double heliHeight;
  23.     int distance;
  24.     Polygon playArea;
  25.     int maxchange;
  26.    
  27.     BufferedImage canvas;
  28.     Graphics2D g2;
  29.    
  30.     public static void main(String[] args) {
  31.         JFrame frame = new JFrame("Helicopter Game");
  32.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.         Applet game = new HelicopterGame();
  34.         game.setSize(600, 500);
  35.         game.setPreferredSize(new Dimension(600, 500));
  36.         frame.add(game);
  37.         frame.pack();
  38.         game.init();
  39.         game.start();
  40.         frame.setVisible(true);
  41.     }
  42.    
  43.     public void init() {
  44.         setSize(600, 500);
  45.         setPreferredSize(new Dimension(600, 500));
  46.         screen = 0;
  47.         heliHeight = 250;
  48.         distance = 0;
  49.         goingUp = false;
  50.         direction = 0;
  51.         int[] areax = new int[62];
  52.         int[] areay = new int[62];
  53.         for (int i = 0; i <= 30; i++) {
  54.             areax[i] = i * 20;
  55.             areax[61 - i] = i * 20;
  56.             areay[i] = 100;
  57.             areay[61 - i] = 400;
  58.         }
  59.         playArea = new Polygon(areax, areay, 62);
  60.         addMouseListener(this);
  61.     }
  62.    
  63.     public void start() {
  64.         canvas = new BufferedImage(600, 500, BufferedImage.TYPE_INT_ARGB);
  65.         g2 = canvas.createGraphics();
  66.         new Engine(this);
  67.     }
  68.    
  69.     public void update(Graphics g) {
  70.         paint(g);
  71.     }
  72.    
  73.     public void paint(Graphics g) {
  74.         if (g2 != null) {
  75.             g2.setColor(Color.GRAY);
  76.             g2.fillRect(0, 0, 600, 500);
  77.             g2.setColor(Color.BLACK);
  78.             g2.fillPolygon(playArea);
  79.             g2.setColor(Color.WHITE);
  80.             g2.setFont(new Font("Arial", Font.BOLD, 12));
  81.             g2.drawString("Distance: " + (distance / 5), 10, 20);
  82.             g2.drawString("By: XThexder (zackwiny)", 450, 20);
  83.             g2.setColor(new Color(0, 100, 0));
  84.             g2.fillRect(275, (int) heliHeight - 25, 50, 50);
  85.             g2.setColor(new Color(100, 100, 100));
  86.             if (!playArea.contains(new Point(275, (int) heliHeight - 25))) g2.fillRect(275, (int) heliHeight - 25, 25, 25);
  87.             if (!playArea.contains(new Point(325, (int) heliHeight - 25))) g2.fillRect(300, (int) heliHeight - 25, 25, 25);
  88.             if (!playArea.contains(new Point(275, (int) heliHeight + 25))) g2.fillRect(275, (int) heliHeight, 25, 25);
  89.             if (!playArea.contains(new Point(325, (int) heliHeight + 25))) g2.fillRect(300, (int) heliHeight, 25, 25);
  90.             g2.setFont(new Font("Arial Black", Font.BOLD, 48));
  91.             if (screen == 0) {
  92.                 g2.setColor(Color.WHITE);
  93.                 g2.drawString("Click to start", 110, 150);
  94.             } else if (screen == 2) {
  95.                 g2.setColor(Color.RED);
  96.                 g2.drawString("Game over", 140, 150);
  97.                 g2.setFont(new Font("Arial", Font.BOLD, 24));
  98.                 g2.drawString("Your score: " + (distance / 5), 160, 200);
  99.                 g2.drawString("Your time: " + (distance / 100.0) + " seconds", 160, 225);
  100.             } else if (screen == 3) {
  101.                 g2.setColor(Color.RED);
  102.                 g2.drawString("Click to restart", 85, 150);
  103.                 g2.setFont(new Font("Arial", Font.BOLD, 24));
  104.                 g2.drawString("Your score: " + (distance / 5), 160, 200);
  105.                 g2.drawString("Your time: " + (distance / 100.0) + " seconds", 160, 225);
  106.             }
  107.             g.drawImage(canvas, 0, 0, null);
  108.         }
  109.     }
  110.  
  111.     public void mouseClicked(MouseEvent e) {}
  112.     public void mouseEntered(MouseEvent e) {}
  113.     public void mouseExited(MouseEvent e) {}
  114.  
  115.     public void mousePressed(MouseEvent e) {
  116.         if (screen == 0 || screen == 3) {
  117.             heliHeight = 250;
  118.             distance = 0;
  119.             maxchange = 5;
  120.             direction = 0;
  121.             for (int i = 0; i <= 30; i++) {
  122.                 playArea.ypoints[i] = 100;
  123.                 playArea.ypoints[61 - i] = 400;
  124.             }
  125.             playArea.invalidate();
  126.             screen = 1;
  127.         }
  128.         goingUp = true;
  129.     }
  130.  
  131.     public void mouseReleased(MouseEvent e) {
  132.         goingUp = false;
  133.     }
  134.  
  135. }
  136.  
  137. class Engine implements Runnable {
  138.  
  139.     HelicopterGame theApplet;
  140.     boolean direction;
  141.     long timer;
  142.    
  143.     public Engine(HelicopterGame applet) {
  144.         theApplet = applet;
  145.         direction = false;
  146.         timer = 0;
  147.         new Thread(this).start();
  148.     }
  149.    
  150.     public void run() {
  151.         while (true) {
  152.             theApplet.repaint();
  153.             if (theApplet.screen == 1) {
  154.                 if (theApplet.goingUp) theApplet.direction -= 1.4;
  155.                 else theApplet.direction += 1;
  156.                 theApplet.direction = Math.max(Math.min(theApplet.direction, 40), -35);
  157.                 theApplet.heliHeight += theApplet.direction / 10.0;
  158.                 theApplet.distance++;
  159.                 if (!theApplet.playArea.contains(new Point(275, (int) theApplet.heliHeight - 25)) ||
  160.                         !theApplet.playArea.contains(new Point(325, (int) theApplet.heliHeight - 25)) ||
  161.                         !theApplet.playArea.contains(new Point(275, (int) theApplet.heliHeight + 25)) ||
  162.                         !theApplet.playArea.contains(new Point(325, (int) theApplet.heliHeight + 25))) {
  163.                     theApplet.screen = 2;
  164.                     timer = System.currentTimeMillis() + 2000;
  165.                 } else if (theApplet.distance % 3 == 0) {
  166.                     for (int i = 0; i < 30; i++) {
  167.                         theApplet.playArea.ypoints[i] = theApplet.playArea.ypoints[i + 1];
  168.                         theApplet.playArea.ypoints[61 - i] = theApplet.playArea.ypoints[60 - i];
  169.                     }
  170.                     int change = direction ? random(-theApplet.maxchange, 0) : random(0, theApplet.maxchange);
  171.                     if (theApplet.distance % 200 == 0) theApplet.maxchange++;
  172.                     theApplet.playArea.ypoints[30] = theApplet.playArea.ypoints[29] + change;
  173.                     theApplet.playArea.ypoints[31] = theApplet.playArea.ypoints[32] + change;
  174.                     if (theApplet.playArea.ypoints[30] <= 0) {
  175.                         direction = !direction;
  176.                         theApplet.playArea.ypoints[30] = 1;
  177.                     } else if (theApplet.playArea.ypoints[31] >= 500) {
  178.                         direction = !direction;
  179.                         theApplet.playArea.ypoints[31] = 499;
  180.                     } else if (random(0, 50) == 1) {
  181.                         direction = !direction;
  182.                     }
  183.                     theApplet.playArea.invalidate();
  184.                     //theApplet.heliHeight = (theApplet.playArea.ypoints[14] + theApplet.playArea.ypoints[46]) / 2;
  185.                 }
  186.             } else if (theApplet.screen == 2 && timer - System.currentTimeMillis() <= 0) theApplet.screen = 3;
  187.             try {
  188.                 Thread.sleep(10);
  189.             } catch (InterruptedException e) {
  190.                 return;
  191.             }
  192.         }
  193.     }
  194.    
  195.     private int random(int a, int b) {
  196.         return (int) (Math.random() * (b - a + 1)) + a;
  197.     }
  198.    
  199. }
Advertisement
Add Comment
Please, Sign In to add comment