Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.Polygon;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- public class HelicopterGame extends Applet implements MouseListener {
- private static final long serialVersionUID = 1L;
- int screen;
- double direction;
- boolean goingUp;
- double heliHeight;
- int distance;
- Polygon playArea;
- int maxchange;
- BufferedImage canvas;
- Graphics2D g2;
- public static void main(String[] args) {
- JFrame frame = new JFrame("Helicopter Game");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Applet game = new HelicopterGame();
- game.setSize(600, 500);
- game.setPreferredSize(new Dimension(600, 500));
- frame.add(game);
- frame.pack();
- game.init();
- game.start();
- frame.setVisible(true);
- }
- public void init() {
- setSize(600, 500);
- setPreferredSize(new Dimension(600, 500));
- screen = 0;
- heliHeight = 250;
- distance = 0;
- goingUp = false;
- direction = 0;
- int[] areax = new int[62];
- int[] areay = new int[62];
- for (int i = 0; i <= 30; i++) {
- areax[i] = i * 20;
- areax[61 - i] = i * 20;
- areay[i] = 100;
- areay[61 - i] = 400;
- }
- playArea = new Polygon(areax, areay, 62);
- addMouseListener(this);
- }
- public void start() {
- canvas = new BufferedImage(600, 500, BufferedImage.TYPE_INT_ARGB);
- g2 = canvas.createGraphics();
- new Engine(this);
- }
- public void update(Graphics g) {
- paint(g);
- }
- public void paint(Graphics g) {
- if (g2 != null) {
- g2.setColor(Color.GRAY);
- g2.fillRect(0, 0, 600, 500);
- g2.setColor(Color.BLACK);
- g2.fillPolygon(playArea);
- g2.setColor(Color.WHITE);
- g2.setFont(new Font("Arial", Font.BOLD, 12));
- g2.drawString("Distance: " + (distance / 5), 10, 20);
- g2.drawString("By: XThexder (zackwiny)", 450, 20);
- g2.setColor(new Color(0, 100, 0));
- g2.fillRect(275, (int) heliHeight - 25, 50, 50);
- g2.setColor(new Color(100, 100, 100));
- if (!playArea.contains(new Point(275, (int) heliHeight - 25))) g2.fillRect(275, (int) heliHeight - 25, 25, 25);
- if (!playArea.contains(new Point(325, (int) heliHeight - 25))) g2.fillRect(300, (int) heliHeight - 25, 25, 25);
- if (!playArea.contains(new Point(275, (int) heliHeight + 25))) g2.fillRect(275, (int) heliHeight, 25, 25);
- if (!playArea.contains(new Point(325, (int) heliHeight + 25))) g2.fillRect(300, (int) heliHeight, 25, 25);
- g2.setFont(new Font("Arial Black", Font.BOLD, 48));
- if (screen == 0) {
- g2.setColor(Color.WHITE);
- g2.drawString("Click to start", 110, 150);
- } else if (screen == 2) {
- g2.setColor(Color.RED);
- g2.drawString("Game over", 140, 150);
- g2.setFont(new Font("Arial", Font.BOLD, 24));
- g2.drawString("Your score: " + (distance / 5), 160, 200);
- g2.drawString("Your time: " + (distance / 100.0) + " seconds", 160, 225);
- } else if (screen == 3) {
- g2.setColor(Color.RED);
- g2.drawString("Click to restart", 85, 150);
- g2.setFont(new Font("Arial", Font.BOLD, 24));
- g2.drawString("Your score: " + (distance / 5), 160, 200);
- g2.drawString("Your time: " + (distance / 100.0) + " seconds", 160, 225);
- }
- g.drawImage(canvas, 0, 0, null);
- }
- }
- public void mouseClicked(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- public void mousePressed(MouseEvent e) {
- if (screen == 0 || screen == 3) {
- heliHeight = 250;
- distance = 0;
- maxchange = 5;
- direction = 0;
- for (int i = 0; i <= 30; i++) {
- playArea.ypoints[i] = 100;
- playArea.ypoints[61 - i] = 400;
- }
- playArea.invalidate();
- screen = 1;
- }
- goingUp = true;
- }
- public void mouseReleased(MouseEvent e) {
- goingUp = false;
- }
- }
- class Engine implements Runnable {
- HelicopterGame theApplet;
- boolean direction;
- long timer;
- public Engine(HelicopterGame applet) {
- theApplet = applet;
- direction = false;
- timer = 0;
- new Thread(this).start();
- }
- public void run() {
- while (true) {
- theApplet.repaint();
- if (theApplet.screen == 1) {
- if (theApplet.goingUp) theApplet.direction -= 1.4;
- else theApplet.direction += 1;
- theApplet.direction = Math.max(Math.min(theApplet.direction, 40), -35);
- theApplet.heliHeight += theApplet.direction / 10.0;
- theApplet.distance++;
- if (!theApplet.playArea.contains(new Point(275, (int) theApplet.heliHeight - 25)) ||
- !theApplet.playArea.contains(new Point(325, (int) theApplet.heliHeight - 25)) ||
- !theApplet.playArea.contains(new Point(275, (int) theApplet.heliHeight + 25)) ||
- !theApplet.playArea.contains(new Point(325, (int) theApplet.heliHeight + 25))) {
- theApplet.screen = 2;
- timer = System.currentTimeMillis() + 2000;
- } else if (theApplet.distance % 3 == 0) {
- for (int i = 0; i < 30; i++) {
- theApplet.playArea.ypoints[i] = theApplet.playArea.ypoints[i + 1];
- theApplet.playArea.ypoints[61 - i] = theApplet.playArea.ypoints[60 - i];
- }
- int change = direction ? random(-theApplet.maxchange, 0) : random(0, theApplet.maxchange);
- if (theApplet.distance % 200 == 0) theApplet.maxchange++;
- theApplet.playArea.ypoints[30] = theApplet.playArea.ypoints[29] + change;
- theApplet.playArea.ypoints[31] = theApplet.playArea.ypoints[32] + change;
- if (theApplet.playArea.ypoints[30] <= 0) {
- direction = !direction;
- theApplet.playArea.ypoints[30] = 1;
- } else if (theApplet.playArea.ypoints[31] >= 500) {
- direction = !direction;
- theApplet.playArea.ypoints[31] = 499;
- } else if (random(0, 50) == 1) {
- direction = !direction;
- }
- theApplet.playArea.invalidate();
- //theApplet.heliHeight = (theApplet.playArea.ypoints[14] + theApplet.playArea.ypoints[46]) / 2;
- }
- } else if (theApplet.screen == 2 && timer - System.currentTimeMillis() <= 0) theApplet.screen = 3;
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- return;
- }
- }
- }
- private int random(int a, int b) {
- return (int) (Math.random() * (b - a + 1)) + a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment