Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cyentw.game.src;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.geom.AffineTransform;
- import java.util.Random;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class Main extends JPanel implements ActionListener {
- /**
- * The FPS. Its static at the moment; I wish to change that.
- */
- private int FPS = 100;
- private float DELAYFPS = 1000 / FPS;
- /**
- * Where on the screen we draw our ship
- */
- private int drawPointX = 240;
- private int drawPointY = 230;
- /**
- * The amount of stars to set/paint
- */
- private int starCount = 50;
- private boolean drawOrder = true;
- /**
- * Ints to make color setting easier
- */
- private int GREY_BLOCK = 10;
- private int WHITE_BLOCK = 11;
- private int DARK_GREEN_BLOCK = 12;
- private int DARK_BLUE_BLOCK = 13;
- private int RED_BLOCK = 4;
- /**
- * The locations of the stars in the background.
- */
- private float[][] stars = locateStars(starCount);
- /**
- * The number of times the program paints a object. Is updated every paint
- * cycle.
- */
- private int polygonsUsed = 0;
- /**
- * The positions of the ship. 20,-130 are the starting positions.
- */
- private float posX = 20;
- private float posY = -130;
- /**
- * If the stats are showing on the screen or not
- */
- private boolean statsShowing = false;
- /**
- * The angle and the change rate of that angle every cylce.
- */
- private float angle = 0;
- private float angleAcceleration = 0;
- /**
- * The speed of the ship. (Should be named speed)
- */
- private float acceleration = 0;
- /**
- * flags to make key down/up response time faster
- */
- private boolean aDown = false;
- private boolean dDown = false;
- private boolean wDown = false;
- private boolean sDown = false;
- private int[][] planetLocations = { { 0, 0 }, { 20, 20 } };
- /**
- * The components of the ship. components[the component number/id][the
- * color, x coord, y coord]
- */
- private int[][] components = { { GREY_BLOCK, 0, -1 }, { GREY_BLOCK, 0, 0 },
- { WHITE_BLOCK, 1, 1 }, { WHITE_BLOCK, -1, 1 },
- { WHITE_BLOCK, 1, 0 }, { WHITE_BLOCK, -1, 0 },
- };
- /**
- * If the planets should be painted or not.
- */
- private boolean[] planetVisible = { true, true };
- /**
- * The location of the centers of the planets. planetCenters[which
- * planet][the x coord, the y coord]
- */
- private int[][] planetCenters = { { 0, 3 }, { 20, 23 } };
- /**
- * All of the blocks in the 1rst planet. worldBlocks[this is which component
- * it is][the color,the x coord, the y coord]
- */
- private int[][] worldBlocks = { { WHITE_BLOCK, 0, 0 },
- { WHITE_BLOCK, 1, 0 }, { WHITE_BLOCK, -1, 0 },
- { DARK_BLUE_BLOCK, 0, 1 }, { DARK_BLUE_BLOCK, 1, 1 },
- { DARK_BLUE_BLOCK, 2, 1 }, { DARK_BLUE_BLOCK, -1, 1 },
- { DARK_BLUE_BLOCK, -2, 1 }, { DARK_GREEN_BLOCK, 0, 2 },
- { DARK_GREEN_BLOCK, -1, 2 }, { DARK_BLUE_BLOCK, 1, 2 },
- { DARK_BLUE_BLOCK, 2, 2 }, { DARK_BLUE_BLOCK, 3, 2 },
- { DARK_BLUE_BLOCK, -2, 2 }, { DARK_BLUE_BLOCK, -3, 2 },
- { DARK_BLUE_BLOCK, -3, 3 }, { DARK_BLUE_BLOCK, 3, 3 },
- { DARK_GREEN_BLOCK, -2, 3 }, { DARK_GREEN_BLOCK, -1, 3 },
- { DARK_GREEN_BLOCK, 0, 3 }, { DARK_BLUE_BLOCK, 1, 3 },
- { DARK_BLUE_BLOCK, 2, 3 }, { DARK_BLUE_BLOCK, -3, 4 },
- { DARK_GREEN_BLOCK, -1, 4 }, { DARK_BLUE_BLOCK, -2, 4 },
- { DARK_BLUE_BLOCK, 0, 4 }, { DARK_BLUE_BLOCK, 1, 4 },
- { DARK_BLUE_BLOCK, 2, 4 }, { DARK_BLUE_BLOCK, 3, 4 },
- { DARK_BLUE_BLOCK, -2, 5 }, { DARK_GREEN_BLOCK, -1, 5 },
- { DARK_GREEN_BLOCK, 0, 5 }, { DARK_BLUE_BLOCK, -2, 5 },
- { DARK_BLUE_BLOCK, 1, 5 }, { DARK_BLUE_BLOCK, 2, 5 },
- { WHITE_BLOCK, 0, 6 }, { WHITE_BLOCK, 1, 6 },
- { WHITE_BLOCK, -1, 6 }, };
- private int[][] world2Blocks = new int[38][3];
- private int[][][] universeBlocks = { worldBlocks, world2Blocks };
- private Timer timerFPS;
- /**
- * The main method
- */
- public Main() {
- makePlanets();
- addKeyListener(new TAdapter());
- setBackground(Color.black);
- setFocusable(true);
- initGame();
- }
- /**
- * Little short cut so that I dont have to write out every block for the 2nd
- * planet :-)
- */
- private void makePlanets() {
- int i = 1;
- world2Blocks[0] = new int[] { RED_BLOCK, 0, 0 };
- for (int l = -1; l < 2; l++) {
- world2Blocks[i][0] = RED_BLOCK;
- world2Blocks[i][1] = l;
- world2Blocks[i][2] = 0;
- i++;
- }
- for (int l = -2; l < 3; l++) {
- world2Blocks[i][0] = RED_BLOCK;
- world2Blocks[i][1] = l;
- world2Blocks[i][2] = 1;
- i++;
- }
- for (int l = -3; l < 4; l++) {
- world2Blocks[i][0] = RED_BLOCK;
- world2Blocks[i][1] = l;
- world2Blocks[i][2] = 2;
- i++;
- }
- for (int l = -3; l < 4; l++) {
- world2Blocks[i][0] = RED_BLOCK;
- world2Blocks[i][1] = l;
- world2Blocks[i][2] = 3;
- i++;
- }
- for (int l = -3; l < 4; l++) {
- world2Blocks[i][0] = RED_BLOCK;
- world2Blocks[i][1] = l;
- world2Blocks[i][2] = 4;
- i++;
- }
- for (int l = -2; l < 3; l++) {
- world2Blocks[i][0] = RED_BLOCK;
- world2Blocks[i][1] = l;
- world2Blocks[i][2] = 5;
- i++;
- }
- for (int l = -1; l < 2; l++) {
- world2Blocks[i][0] = RED_BLOCK;
- world2Blocks[i][1] = l;
- world2Blocks[i][2] = 6;
- i++;
- }
- }
- /**
- * pauses the game
- *
- * @param pause
- */
- public void pause(boolean pause) {
- if (pause == true) {
- timerFPS.stop();
- System.out.println("Player: Paused the game.");
- setBackground(Color.gray);
- } else {
- timerFPS.start();
- System.out.println("Player: Resumed the game.");
- setBackground(Color.black);
- }
- }
- /**
- * Checks if the game is paused
- *
- * @return if the game is paused
- */
- public boolean isPaused() {
- if (timerFPS.isRunning()) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * Starts the timer
- */
- public void initGame() {
- timerFPS = new Timer((int) DELAYFPS, this);
- timerFPS.start();
- }
- /**
- * The main paint method. Directs to others.
- */
- public void paint(Graphics g) {
- super.paint(g);
- polygonsUsed = components.length;
- if (!isPaused()) {
- for (int i = 0; i < stars.length; i++) {
- paintStars(g, i);
- }
- if (drawOrder == true)
- for (int o = 0; o < universeBlocks.length; o++)
- if (planetVisible[o] == true) {
- polygonsUsed += universeBlocks[o].length;
- for (int i = 0; i < universeBlocks[o].length; i++) {
- paintWorldComponents(g, i, o);
- }
- }
- for (int i = 0; i < components.length; i++) {
- paintComponent(g, i);
- }
- if (drawOrder == false)
- for (int o = 0; o < universeBlocks.length; o++)
- if (planetVisible[o] == true) {
- polygonsUsed += universeBlocks[o].length;
- for (int i = 0; i < universeBlocks[o].length; i++) {
- paintWorldComponents(g, i, o);
- }
- }
- }
- if (isPaused())
- paintPaused(g);
- else if (gameStatsShowing()) {
- paintStats(g);
- }
- }
- /**
- * Paints the ship's blocks
- *
- * @param g
- * graphics
- * @param component
- * which component of the ship we are painting (this method
- * paints 1 block)
- */
- public void paintComponent(Graphics g, int component) {
- Graphics2D g2d = (Graphics2D) g.create();
- AffineTransform at = new AffineTransform();
- at.setToRotation(Math.toRadians(angle), drawPointX + 10,
- drawPointY + 10);
- if (components[component][0] == 1) {
- g2d.setColor(Color.CYAN);
- }
- if (components[component][0] == 2) {
- g2d.setColor(Color.GREEN);
- }
- if (components[component][0] == 3) {
- g2d.setColor(Color.YELLOW);
- }
- if (components[component][0] == WHITE_BLOCK) {
- g2d.setColor(Color.white);
- }
- if (components[component][0] == GREY_BLOCK) {
- g2d.setColor(Color.gray);
- }
- if (components[component][0] != 0)
- g2d.fillRect(drawPointX + (components[component][1] * 10),
- drawPointY + (components[component][2] * 10), 10, 10);
- }
- /**
- * Paints the game stats for the player to see
- *
- * @param g
- * graphics
- */
- public void paintStats(Graphics g) {
- g.setColor(Color.WHITE);
- g.drawString(
- "FPS: "
- + String.valueOf((long) ((Math.pow(DELAYFPS, -1)) * 1000)),
- 10, 20);
- g.drawString("aDown = " + aDown, 10, 30);
- g.drawString("dDown = " + dDown, 10, 40);
- g.drawString("wDown = " + wDown, 10, 50);
- g.drawString("sDown = " + sDown, 10, 60);
- g.drawString("Angle: " + angle, 100, 20);
- g.drawString("Angle Acceleration = " + angleAcceleration, 100, 30);
- g.drawString("Speed = " + acceleration, 100, 40);
- g.drawString("posX = " + posX, 100, 50);
- g.drawString("posY = " + posY, 100, 60);
- g.drawString("drawOrder = " + drawOrder, 250, 20);
- g.drawString("polygonsUsed = " + polygonsUsed, 250, 30);
- }
- /**
- * Paints the planet's blocks.
- *
- * @param g
- * graphics
- * @param component
- * which component to paint (this method paints 1 component)
- * @param world
- * which world this block belongs to
- */
- public void paintWorldComponents(Graphics g, int component, int world) {
- Color color = Color.BLACK;
- if (universeBlocks[world][component][0] == GREY_BLOCK) {
- color = Color.gray;
- } else if (universeBlocks[world][component][0] == WHITE_BLOCK) {
- color = Color.white;
- } else if (universeBlocks[world][component][0] == DARK_GREEN_BLOCK) {
- color = (new Color(3381504));
- } else if (universeBlocks[world][component][0] == DARK_BLUE_BLOCK) {
- color = Color.blue;
- } else if (universeBlocks[world][component][0] == RED_BLOCK) {
- color = Color.red;
- }
- Graphics2D g2d = (Graphics2D) g.create();
- AffineTransform at = new AffineTransform();
- at.setToRotation(Math.toRadians(360 - angle), drawPointX + 10,
- drawPointY + 10);
- g2d.setTransform(at);
- g2d.setColor(color);
- g2d.fillRect(
- drawPointX
- + 0
- - (int) posX
- + (universeBlocks[world][component][1] + planetLocations[world][0])
- * 40,
- drawPointY
- + (int) posY
- + (universeBlocks[world][component][2] + planetLocations[world][0])
- * 40, 40, 40);
- }
- /**
- * Paints the paused screen
- *
- * @param g
- * graphics
- */
- public void paintPaused(Graphics g) {
- Font pausedFont = new Font("Lucida Console", Font.BOLD, 60);
- g.setFont(pausedFont);
- g.drawString("Game Paused", 30, 90);
- }
- /**
- * Paints the parallax effect stars in the background
- *
- * @param g
- * graphics
- * @param i
- * which star we are painting
- */
- public void paintStars(Graphics g, int i) {
- Graphics2D g2d = (Graphics2D) g.create();
- AffineTransform at = new AffineTransform();
- at.setToRotation(Math.toRadians(360 - angle), drawPointX + 10,
- drawPointY + 10);
- g2d.setTransform(at);
- g2d.setColor(Color.white);
- g2d.fillRect((int) stars[i][0], (int) stars[i][1], 2, 2);
- }
- /**
- * Locates the stars for the background of the game.
- *
- * @param stars
- * number of stars to locate
- * @return array with locations of stars
- */
- public float[][] locateStars(int stars) {
- float[][] array = new float[stars][2];
- Random r = new Random();
- for (int i = 0; i < array.length; i++) {
- int x = r.nextInt(Start.WIDTH * 2) - Start.WIDTH / 2 + 1;
- int y = r.nextInt(Start.HEIGHT * 2) - Start.HEIGHT / 2 + 1;
- array[i][0] = x;
- array[i][1] = y;
- System.out.println("Star " + i + " created at " + x + ", " + y);
- }
- return array;
- }
- public void gameOver(Graphics g) {
- }
- /**
- * Stuff for game stats
- */
- private void gameStats(boolean isShowing) {
- if (!isShowing) {
- statsShowing = true;
- System.out.println("Player: Enabled game stats");
- }
- if (isShowing) {
- statsShowing = false;
- System.out.println("Player: Disabled game stats");
- }
- }
- private boolean gameStatsShowing() {
- return statsShowing;
- }
- public void actionPerformed(ActionEvent e) {
- /**
- * The timer based game loop. This is what I want to change.
- */
- if (e.getSource() == timerFPS) {
- update();
- repaint();
- }
- }
- public void update() {
- double a = 200 / DELAYFPS;
- /**
- * Player position updated
- */
- posX += (acceleration * (Math.sin(Math.toRadians(angle))) / 10) / a
- * 10;
- posY += (acceleration * (Math.cos(Math.toRadians(angle))) / 10) / a
- * 10;
- /**
- * Tests if programs should draw each planet
- */
- for (int i = 0; i < planetCenters.length; i++) {
- if (posX > planetCenters[i][0] * 40 + Start.WIDTH / 2 + 300
- || posX < planetCenters[i][0] * 40 - Start.WIDTH / 2 - 300
- || posY > 0 - (planetCenters[i][1] * 40) + Start.HEIGHT / 2
- + 300
- || posY < 0 - (planetCenters[i][1] * 40) - Start.HEIGHT / 2
- - 300)
- planetVisible[i] = false;
- else
- planetVisible[i] = true;
- }
- /**
- * Redo-ing the location for the stars
- */
- for (int i = 0; i < stars.length; i++) {
- stars[i][0] -= (acceleration * (Math.sin(Math.toRadians(angle))) / 10) / 5;
- stars[i][1] += (acceleration * (Math.cos(Math.toRadians(angle))) / 10) / 5;
- }
- /**
- * Checking for keys down and adjusting variables accordingly
- */
- if (aDown)
- if (angleAcceleration > -11)
- angleAcceleration -= 0.25F;
- if (dDown)
- if (angleAcceleration < 11)
- angleAcceleration += 0.25F;
- if (wDown)
- if (acceleration < 11)
- acceleration += 0.25 / 2;
- if (sDown)
- if (acceleration > -11)
- acceleration -= 0.25 / 2;
- if (angleAcceleration != 0) {
- angle += angleAcceleration / a;
- }
- if (angle > 360) {
- angle -= 360;
- }
- if (angle < 0) {
- angle += 360;
- }
- }
- private class TAdapter extends KeyAdapter {
- /**
- * Checks for various keys and adjusts variables/performs methods
- */
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- if (key == KeyEvent.VK_P) {
- pause(!isPaused());
- }
- if (key == KeyEvent.VK_F1) {
- gameStats(gameStatsShowing());
- }
- if (key == KeyEvent.VK_A) {
- aDown = true;
- }
- if (key == KeyEvent.VK_D) {
- dDown = true;
- }
- if (key == KeyEvent.VK_W) {
- wDown = true;
- }
- if (key == KeyEvent.VK_S) {
- sDown = true;
- }
- if (key == KeyEvent.VK_B) {
- flipDrawOrder();
- }
- if (key == KeyEvent.VK_E) {
- acceleration = 0;
- }
- if (key == KeyEvent.VK_Q) {
- angleAcceleration = 0;
- }
- }
- public void keyReleased(KeyEvent e) {
- int key = e.getKeyCode();
- if (key == KeyEvent.VK_A) {
- aDown = false;
- }
- if (key == KeyEvent.VK_D) {
- dDown = false;
- }
- if (key == KeyEvent.VK_W) {
- wDown = false;
- }
- if (key == KeyEvent.VK_S) {
- sDown = false;
- }
- }
- }
- /**
- * Flips the order in which things are drawn. (Will render the ship in front
- * of or behind the planets)
- */
- public void flipDrawOrder() {
- if (drawOrder == true) {
- drawOrder = false;
- } else {
- drawOrder = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement