Advertisement
Guest User

Untitled

a guest
Jan 16th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.93 KB | None | 0 0
  1. package cyentw.game.src;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.geom.AffineTransform;
  12. import java.util.Random;
  13.  
  14. import javax.swing.JPanel;
  15. import javax.swing.Timer;
  16.  
  17. public class Main extends JPanel implements ActionListener {
  18.  
  19.     /**
  20.      * The FPS. Its static at the moment; I wish to change that.
  21.      */
  22.     private int FPS = 100;
  23.     private float DELAYFPS = 1000 / FPS;
  24.  
  25.     /**
  26.      * Where on the screen we draw our ship
  27.      */
  28.     private int drawPointX = 240;
  29.     private int drawPointY = 230;
  30.  
  31.     /**
  32.      * The amount of stars to set/paint
  33.      */
  34.     private int starCount = 50;
  35.  
  36.     private boolean drawOrder = true;
  37.  
  38.     /**
  39.      * Ints to make color setting easier
  40.      */
  41.     private int GREY_BLOCK = 10;
  42.     private int WHITE_BLOCK = 11;
  43.     private int DARK_GREEN_BLOCK = 12;
  44.     private int DARK_BLUE_BLOCK = 13;
  45.     private int RED_BLOCK = 4;
  46.  
  47.     /**
  48.      * The locations of the stars in the background.
  49.      */
  50.     private float[][] stars = locateStars(starCount);
  51.  
  52.     /**
  53.      * The number of times the program paints a object. Is updated every paint
  54.      * cycle.
  55.      */
  56.     private int polygonsUsed = 0;
  57.  
  58.     /**
  59.      * The positions of the ship. 20,-130 are the starting positions.
  60.      */
  61.     private float posX = 20;
  62.     private float posY = -130;
  63.  
  64.     /**
  65.      * If the stats are showing on the screen or not
  66.      */
  67.     private boolean statsShowing = false;
  68.  
  69.     /**
  70.      * The angle and the change rate of that angle every cylce.
  71.      */
  72.     private float angle = 0;
  73.     private float angleAcceleration = 0;
  74.  
  75.     /**
  76.      * The speed of the ship. (Should be named speed)
  77.      */
  78.     private float acceleration = 0;
  79.  
  80.     /**
  81.      * flags to make key down/up response time faster
  82.      */
  83.     private boolean aDown = false;
  84.     private boolean dDown = false;
  85.     private boolean wDown = false;
  86.     private boolean sDown = false;
  87.  
  88.     private int[][] planetLocations = { { 0, 0 }, { 20, 20 } };
  89.  
  90.     /**
  91.      * The components of the ship. components[the component number/id][the
  92.      * color, x coord, y coord]
  93.      */
  94.     private int[][] components = { { GREY_BLOCK, 0, -1 }, { GREY_BLOCK, 0, 0 },
  95.             { WHITE_BLOCK, 1, 1 }, { WHITE_BLOCK, -1, 1 },
  96.             { WHITE_BLOCK, 1, 0 }, { WHITE_BLOCK, -1, 0 },
  97.  
  98.     };
  99.  
  100.     /**
  101.      * If the planets should be painted or not.
  102.      */
  103.     private boolean[] planetVisible = { true, true };
  104.  
  105.     /**
  106.      * The location of the centers of the planets. planetCenters[which
  107.      * planet][the x coord, the y coord]
  108.      */
  109.     private int[][] planetCenters = { { 0, 3 }, { 20, 23 } };
  110.  
  111.     /**
  112.      * All of the blocks in the 1rst planet. worldBlocks[this is which component
  113.      * it is][the color,the x coord, the y coord]
  114.      */
  115.     private int[][] worldBlocks = { { WHITE_BLOCK, 0, 0 },
  116.             { WHITE_BLOCK, 1, 0 }, { WHITE_BLOCK, -1, 0 },
  117.             { DARK_BLUE_BLOCK, 0, 1 }, { DARK_BLUE_BLOCK, 1, 1 },
  118.             { DARK_BLUE_BLOCK, 2, 1 }, { DARK_BLUE_BLOCK, -1, 1 },
  119.             { DARK_BLUE_BLOCK, -2, 1 }, { DARK_GREEN_BLOCK, 0, 2 },
  120.             { DARK_GREEN_BLOCK, -1, 2 }, { DARK_BLUE_BLOCK, 1, 2 },
  121.             { DARK_BLUE_BLOCK, 2, 2 }, { DARK_BLUE_BLOCK, 3, 2 },
  122.             { DARK_BLUE_BLOCK, -2, 2 }, { DARK_BLUE_BLOCK, -3, 2 },
  123.             { DARK_BLUE_BLOCK, -3, 3 }, { DARK_BLUE_BLOCK, 3, 3 },
  124.             { DARK_GREEN_BLOCK, -2, 3 }, { DARK_GREEN_BLOCK, -1, 3 },
  125.             { DARK_GREEN_BLOCK, 0, 3 }, { DARK_BLUE_BLOCK, 1, 3 },
  126.             { DARK_BLUE_BLOCK, 2, 3 }, { DARK_BLUE_BLOCK, -3, 4 },
  127.             { DARK_GREEN_BLOCK, -1, 4 }, { DARK_BLUE_BLOCK, -2, 4 },
  128.             { DARK_BLUE_BLOCK, 0, 4 }, { DARK_BLUE_BLOCK, 1, 4 },
  129.             { DARK_BLUE_BLOCK, 2, 4 }, { DARK_BLUE_BLOCK, 3, 4 },
  130.             { DARK_BLUE_BLOCK, -2, 5 }, { DARK_GREEN_BLOCK, -1, 5 },
  131.             { DARK_GREEN_BLOCK, 0, 5 }, { DARK_BLUE_BLOCK, -2, 5 },
  132.             { DARK_BLUE_BLOCK, 1, 5 }, { DARK_BLUE_BLOCK, 2, 5 },
  133.             { WHITE_BLOCK, 0, 6 }, { WHITE_BLOCK, 1, 6 },
  134.             { WHITE_BLOCK, -1, 6 }, };
  135.  
  136.     private int[][] world2Blocks = new int[38][3];
  137.     private int[][][] universeBlocks = { worldBlocks, world2Blocks };
  138.     private Timer timerFPS;
  139.  
  140.     /**
  141.      * The main method
  142.      */
  143.     public Main() {
  144.         makePlanets();
  145.         addKeyListener(new TAdapter());
  146.  
  147.         setBackground(Color.black);
  148.         setFocusable(true);
  149.         initGame();
  150.     }
  151.  
  152.     /**
  153.      * Little short cut so that I dont have to write out every block for the 2nd
  154.      * planet :-)
  155.      */
  156.     private void makePlanets() {
  157.         int i = 1;
  158.         world2Blocks[0] = new int[] { RED_BLOCK, 0, 0 };
  159.         for (int l = -1; l < 2; l++) {
  160.             world2Blocks[i][0] = RED_BLOCK;
  161.             world2Blocks[i][1] = l;
  162.             world2Blocks[i][2] = 0;
  163.             i++;
  164.  
  165.         }
  166.         for (int l = -2; l < 3; l++) {
  167.             world2Blocks[i][0] = RED_BLOCK;
  168.             world2Blocks[i][1] = l;
  169.             world2Blocks[i][2] = 1;
  170.             i++;
  171.         }
  172.         for (int l = -3; l < 4; l++) {
  173.             world2Blocks[i][0] = RED_BLOCK;
  174.             world2Blocks[i][1] = l;
  175.             world2Blocks[i][2] = 2;
  176.             i++;
  177.         }
  178.         for (int l = -3; l < 4; l++) {
  179.             world2Blocks[i][0] = RED_BLOCK;
  180.             world2Blocks[i][1] = l;
  181.             world2Blocks[i][2] = 3;
  182.             i++;
  183.         }
  184.         for (int l = -3; l < 4; l++) {
  185.             world2Blocks[i][0] = RED_BLOCK;
  186.             world2Blocks[i][1] = l;
  187.             world2Blocks[i][2] = 4;
  188.             i++;
  189.         }
  190.         for (int l = -2; l < 3; l++) {
  191.             world2Blocks[i][0] = RED_BLOCK;
  192.             world2Blocks[i][1] = l;
  193.             world2Blocks[i][2] = 5;
  194.             i++;
  195.         }
  196.         for (int l = -1; l < 2; l++) {
  197.             world2Blocks[i][0] = RED_BLOCK;
  198.             world2Blocks[i][1] = l;
  199.             world2Blocks[i][2] = 6;
  200.             i++;
  201.         }
  202.     }
  203.  
  204.     /**
  205.      * pauses the game
  206.      *
  207.      * @param pause
  208.      */
  209.     public void pause(boolean pause) {
  210.         if (pause == true) {
  211.             timerFPS.stop();
  212.             System.out.println("Player: Paused the game.");
  213.             setBackground(Color.gray);
  214.         } else {
  215.             timerFPS.start();
  216.             System.out.println("Player: Resumed the game.");
  217.             setBackground(Color.black);
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * Checks if the game is paused
  223.      *
  224.      * @return if the game is paused
  225.      */
  226.     public boolean isPaused() {
  227.         if (timerFPS.isRunning()) {
  228.             return false;
  229.         } else {
  230.             return true;
  231.         }
  232.     }
  233.  
  234.     /**
  235.      * Starts the timer
  236.      */
  237.     public void initGame() {
  238.         timerFPS = new Timer((int) DELAYFPS, this);
  239.         timerFPS.start();
  240.     }
  241.  
  242.     /**
  243.      * The main paint method. Directs to others.
  244.      */
  245.     public void paint(Graphics g) {
  246.         super.paint(g);
  247.         polygonsUsed = components.length;
  248.  
  249.         if (!isPaused()) {
  250.             for (int i = 0; i < stars.length; i++) {
  251.                 paintStars(g, i);
  252.             }
  253.  
  254.             if (drawOrder == true)
  255.                 for (int o = 0; o < universeBlocks.length; o++)
  256.                     if (planetVisible[o] == true) {
  257.                         polygonsUsed += universeBlocks[o].length;
  258.                         for (int i = 0; i < universeBlocks[o].length; i++) {
  259.                             paintWorldComponents(g, i, o);
  260.                         }
  261.                     }
  262.  
  263.             for (int i = 0; i < components.length; i++) {
  264.                 paintComponent(g, i);
  265.             }
  266.             if (drawOrder == false)
  267.                 for (int o = 0; o < universeBlocks.length; o++)
  268.                     if (planetVisible[o] == true) {
  269.                         polygonsUsed += universeBlocks[o].length;
  270.                         for (int i = 0; i < universeBlocks[o].length; i++) {
  271.                             paintWorldComponents(g, i, o);
  272.                         }
  273.                     }
  274.         }
  275.         if (isPaused())
  276.             paintPaused(g);
  277.         else if (gameStatsShowing()) {
  278.             paintStats(g);
  279.         }
  280.  
  281.     }
  282.  
  283.     /**
  284.      * Paints the ship's blocks
  285.      *
  286.      * @param g
  287.      *            graphics
  288.      * @param component
  289.      *            which component of the ship we are painting (this method
  290.      *            paints 1 block)
  291.      */
  292.     public void paintComponent(Graphics g, int component) {
  293.         Graphics2D g2d = (Graphics2D) g.create();
  294.         AffineTransform at = new AffineTransform();
  295.         at.setToRotation(Math.toRadians(angle), drawPointX + 10,
  296.                 drawPointY + 10);
  297.         if (components[component][0] == 1) {
  298.             g2d.setColor(Color.CYAN);
  299.         }
  300.         if (components[component][0] == 2) {
  301.             g2d.setColor(Color.GREEN);
  302.         }
  303.         if (components[component][0] == 3) {
  304.             g2d.setColor(Color.YELLOW);
  305.         }
  306.         if (components[component][0] == WHITE_BLOCK) {
  307.             g2d.setColor(Color.white);
  308.         }
  309.         if (components[component][0] == GREY_BLOCK) {
  310.             g2d.setColor(Color.gray);
  311.         }
  312.         if (components[component][0] != 0)
  313.             g2d.fillRect(drawPointX + (components[component][1] * 10),
  314.                     drawPointY + (components[component][2] * 10), 10, 10);
  315.     }
  316.  
  317.     /**
  318.      * Paints the game stats for the player to see
  319.      *
  320.      * @param g
  321.      *            graphics
  322.      */
  323.     public void paintStats(Graphics g) {
  324.         g.setColor(Color.WHITE);
  325.         g.drawString(
  326.                 "FPS: "
  327.                         + String.valueOf((long) ((Math.pow(DELAYFPS, -1)) * 1000)),
  328.                 10, 20);
  329.         g.drawString("aDown = " + aDown, 10, 30);
  330.         g.drawString("dDown = " + dDown, 10, 40);
  331.         g.drawString("wDown = " + wDown, 10, 50);
  332.         g.drawString("sDown = " + sDown, 10, 60);
  333.         g.drawString("Angle: " + angle, 100, 20);
  334.         g.drawString("Angle Acceleration = " + angleAcceleration, 100, 30);
  335.         g.drawString("Speed = " + acceleration, 100, 40);
  336.         g.drawString("posX = " + posX, 100, 50);
  337.         g.drawString("posY = " + posY, 100, 60);
  338.         g.drawString("drawOrder = " + drawOrder, 250, 20);
  339.         g.drawString("polygonsUsed = " + polygonsUsed, 250, 30);
  340.     }
  341.  
  342.     /**
  343.      * Paints the planet's blocks.
  344.      *
  345.      * @param g
  346.      *            graphics
  347.      * @param component
  348.      *            which component to paint (this method paints 1 component)
  349.      * @param world
  350.      *            which world this block belongs to
  351.      */
  352.     public void paintWorldComponents(Graphics g, int component, int world) {
  353.         Color color = Color.BLACK;
  354.         if (universeBlocks[world][component][0] == GREY_BLOCK) {
  355.             color = Color.gray;
  356.         } else if (universeBlocks[world][component][0] == WHITE_BLOCK) {
  357.             color = Color.white;
  358.         } else if (universeBlocks[world][component][0] == DARK_GREEN_BLOCK) {
  359.             color = (new Color(3381504));
  360.         } else if (universeBlocks[world][component][0] == DARK_BLUE_BLOCK) {
  361.             color = Color.blue;
  362.         } else if (universeBlocks[world][component][0] == RED_BLOCK) {
  363.             color = Color.red;
  364.         }
  365.         Graphics2D g2d = (Graphics2D) g.create();
  366.         AffineTransform at = new AffineTransform();
  367.         at.setToRotation(Math.toRadians(360 - angle), drawPointX + 10,
  368.                 drawPointY + 10);
  369.         g2d.setTransform(at);
  370.         g2d.setColor(color);
  371.         g2d.fillRect(
  372.                 drawPointX
  373.                         + 0
  374.                         - (int) posX
  375.                         + (universeBlocks[world][component][1] + planetLocations[world][0])
  376.                         * 40,
  377.                 drawPointY
  378.                         + (int) posY
  379.                         + (universeBlocks[world][component][2] + planetLocations[world][0])
  380.                         * 40, 40, 40);
  381.     }
  382.  
  383.     /**
  384.      * Paints the paused screen
  385.      *
  386.      * @param g
  387.      *            graphics
  388.      */
  389.     public void paintPaused(Graphics g) {
  390.         Font pausedFont = new Font("Lucida Console", Font.BOLD, 60);
  391.         g.setFont(pausedFont);
  392.         g.drawString("Game Paused", 30, 90);
  393.     }
  394.  
  395.     /**
  396.      * Paints the parallax effect stars in the background
  397.      *
  398.      * @param g
  399.      *            graphics
  400.      * @param i
  401.      *            which star we are painting
  402.      */
  403.     public void paintStars(Graphics g, int i) {
  404.         Graphics2D g2d = (Graphics2D) g.create();
  405.         AffineTransform at = new AffineTransform();
  406.         at.setToRotation(Math.toRadians(360 - angle), drawPointX + 10,
  407.                 drawPointY + 10);
  408.         g2d.setTransform(at);
  409.         g2d.setColor(Color.white);
  410.         g2d.fillRect((int) stars[i][0], (int) stars[i][1], 2, 2);
  411.     }
  412.  
  413.     /**
  414.      * Locates the stars for the background of the game.
  415.      *
  416.      * @param stars
  417.      *            number of stars to locate
  418.      * @return array with locations of stars
  419.      */
  420.     public float[][] locateStars(int stars) {
  421.         float[][] array = new float[stars][2];
  422.         Random r = new Random();
  423.         for (int i = 0; i < array.length; i++) {
  424.             int x = r.nextInt(Start.WIDTH * 2) - Start.WIDTH / 2 + 1;
  425.             int y = r.nextInt(Start.HEIGHT * 2) - Start.HEIGHT / 2 + 1;
  426.             array[i][0] = x;
  427.             array[i][1] = y;
  428.             System.out.println("Star " + i + " created at " + x + ", " + y);
  429.         }
  430.         return array;
  431.     }
  432.  
  433.     public void gameOver(Graphics g) {
  434.  
  435.     }
  436.  
  437.     /**
  438.      * Stuff for game stats
  439.      */
  440.     private void gameStats(boolean isShowing) {
  441.        
  442.         if (!isShowing) {
  443.             statsShowing = true;
  444.             System.out.println("Player: Enabled game stats");
  445.         }
  446.         if (isShowing) {
  447.             statsShowing = false;
  448.             System.out.println("Player: Disabled game stats");
  449.         }
  450.     }
  451.  
  452.     private boolean gameStatsShowing() {
  453.         return statsShowing;
  454.     }
  455.  
  456.     public void actionPerformed(ActionEvent e) {
  457.         /**
  458.          * The timer based game loop. This is what I want to change.
  459.          */
  460.         if (e.getSource() == timerFPS) {
  461.             update();
  462.             repaint();
  463.         }
  464.     }
  465.  
  466.     public void update() {
  467.         double a = 200 / DELAYFPS;
  468.         /**
  469.          * Player position updated
  470.          */
  471.         posX += (acceleration * (Math.sin(Math.toRadians(angle))) / 10) / a
  472.                 * 10;
  473.         posY += (acceleration * (Math.cos(Math.toRadians(angle))) / 10) / a
  474.                 * 10;
  475.  
  476.         /**
  477.          * Tests if programs should draw each planet
  478.          */
  479.         for (int i = 0; i < planetCenters.length; i++) {
  480.  
  481.             if (posX > planetCenters[i][0] * 40 + Start.WIDTH / 2 + 300
  482.                     || posX < planetCenters[i][0] * 40 - Start.WIDTH / 2 - 300
  483.                     || posY > 0 - (planetCenters[i][1] * 40) + Start.HEIGHT / 2
  484.                             + 300
  485.                     || posY < 0 - (planetCenters[i][1] * 40) - Start.HEIGHT / 2
  486.                             - 300)
  487.                 planetVisible[i] = false;
  488.             else
  489.                 planetVisible[i] = true;
  490.         }
  491.  
  492.         /**
  493.          * Redo-ing the location for the stars
  494.          */
  495.         for (int i = 0; i < stars.length; i++) {
  496.             stars[i][0] -= (acceleration * (Math.sin(Math.toRadians(angle))) / 10) / 5;
  497.             stars[i][1] += (acceleration * (Math.cos(Math.toRadians(angle))) / 10) / 5;
  498.         }
  499.  
  500.         /**
  501.          * Checking for keys down and adjusting variables accordingly
  502.          */
  503.         if (aDown)
  504.             if (angleAcceleration > -11)
  505.                 angleAcceleration -= 0.25F;
  506.  
  507.         if (dDown)
  508.             if (angleAcceleration < 11)
  509.                 angleAcceleration += 0.25F;
  510.  
  511.         if (wDown)
  512.             if (acceleration < 11)
  513.                 acceleration += 0.25 / 2;
  514.  
  515.         if (sDown)
  516.             if (acceleration > -11)
  517.                 acceleration -= 0.25 / 2;
  518.  
  519.         if (angleAcceleration != 0) {
  520.             angle += angleAcceleration / a;
  521.         }
  522.         if (angle > 360) {
  523.             angle -= 360;
  524.         }
  525.         if (angle < 0) {
  526.             angle += 360;
  527.         }
  528.     }
  529.  
  530.     private class TAdapter extends KeyAdapter {
  531.         /**
  532.          * Checks for various keys and adjusts variables/performs methods
  533.          */
  534.         public void keyPressed(KeyEvent e) {
  535.  
  536.             int key = e.getKeyCode();
  537.  
  538.             if (key == KeyEvent.VK_P) {
  539.                 pause(!isPaused());
  540.             }
  541.             if (key == KeyEvent.VK_F1) {
  542.                 gameStats(gameStatsShowing());
  543.             }
  544.  
  545.             if (key == KeyEvent.VK_A) {
  546.                 aDown = true;
  547.             }
  548.  
  549.             if (key == KeyEvent.VK_D) {
  550.                 dDown = true;
  551.             }
  552.  
  553.             if (key == KeyEvent.VK_W) {
  554.                 wDown = true;
  555.             }
  556.  
  557.             if (key == KeyEvent.VK_S) {
  558.                 sDown = true;
  559.             }
  560.             if (key == KeyEvent.VK_B) {
  561.                 flipDrawOrder();
  562.             }
  563.  
  564.             if (key == KeyEvent.VK_E) {
  565.                 acceleration = 0;
  566.             }
  567.             if (key == KeyEvent.VK_Q) {
  568.                 angleAcceleration = 0;
  569.             }
  570.         }
  571.  
  572.         public void keyReleased(KeyEvent e) {
  573.  
  574.             int key = e.getKeyCode();
  575.  
  576.             if (key == KeyEvent.VK_A) {
  577.                 aDown = false;
  578.             }
  579.             if (key == KeyEvent.VK_D) {
  580.                 dDown = false;
  581.             }
  582.             if (key == KeyEvent.VK_W) {
  583.                 wDown = false;
  584.             }
  585.             if (key == KeyEvent.VK_S) {
  586.                 sDown = false;
  587.             }
  588.         }
  589.  
  590.     }
  591.  
  592.     /**
  593.      * Flips the order in which things are drawn. (Will render the ship in front
  594.      * of or behind the planets)
  595.      */
  596.     public void flipDrawOrder() {
  597.  
  598.         if (drawOrder == true) {
  599.             drawOrder = false;
  600.         } else {
  601.             drawOrder = true;
  602.         }
  603.     }
  604.  
  605. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement