pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

epitaph64 private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Epitaph64 on Wed 3 Jun 07:25
report abuse | download | new post

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package spheres;
  7.  
  8. import java.util.ArrayList;
  9. import org.newdawn.slick.AppGameContainer;
  10. import org.newdawn.slick.BasicGame;
  11. import org.newdawn.slick.Color;
  12. import org.newdawn.slick.GameContainer;
  13. import org.newdawn.slick.Graphics;
  14. import org.newdawn.slick.Image;
  15. import org.newdawn.slick.Input;
  16. import org.newdawn.slick.SlickException;
  17.  
  18. public class Main extends BasicGame {
  19.  
  20.     private MersenneTwisterFast mt;
  21.  
  22.     private Input input;
  23.  
  24.     //Player related variables
  25.     private float playerX, playerY, playerDX, playerDY;
  26.     private boolean alive = true;
  27.     private boolean positive = true;
  28.  
  29.     private int lastX, lastY;
  30.  
  31.     private int score = 0;
  32.  
  33.     private Image bg;
  34.     private Image bg2;
  35.     private Image asteroid;
  36.     private Image asteroidInverse;
  37.  
  38.     private int percentagePositive;
  39.  
  40.     private int collisionsThisLoop = 0;
  41.  
  42.     private ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
  43.     private boolean win = false;
  44.  
  45.     public Main()
  46.     {
  47.         super("Spheres!");
  48.     }
  49.  
  50.     public static void main(String[] args) {
  51.         try
  52.         {
  53.             AppGameContainer app = new AppGameContainer(new Main());
  54.             app.setDisplayMode(1024, 768, false);
  55.             app.setShowFPS(false);
  56.             app.setFullscreen(false);
  57.             app.setMaximumLogicUpdateInterval(10);
  58.             app.setMinimumLogicUpdateInterval(4);
  59.             app.setVSync(true);
  60.             app.start();
  61.         }
  62.         catch (SlickException e)
  63.         {
  64.             e.printStackTrace();
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     public void init(GameContainer container) throws SlickException
  70.     {
  71.         asteroids.clear();
  72.         input = container.getInput();
  73.         playerX = input.getMouseX();
  74.         playerY = input.getMouseY();
  75.         lastX = (int) playerX;
  76.         lastY = (int) playerY;
  77.         mt = new MersenneTwisterFast();
  78.         for (int i = 0; i < 8; i++)
  79.         {
  80.             asteroids.add(new Asteroid(mt.nextInt(900)+100, mt.nextInt(600)+100, mt.nextInt(100)+50, mt.nextInt(5), mt.nextInt(5), mt.nextBoolean()));
  81.         }
  82.     }
  83.  
  84.     @Override
  85.     public void update(GameContainer container, int delta) throws SlickException
  86.     {
  87.         collisionsThisLoop = 0;
  88.         if (input.isKeyDown(Input.KEY_ESCAPE)) container.exit();
  89.         if (input.isKeyDown(Input.KEY_F1)) container.reinit();
  90.         if (input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) positive = !positive;
  91.         if (alive)
  92.         {
  93.             if ((percentagePositive > 512 && !positive) || (percentagePositive <= 512 && positive))
  94.             {
  95.                 score ++;
  96.             }
  97.             updatePlayer();
  98.             updateAsteroids();
  99.             if (score % 200 == 0)
  100.             {
  101.                 asteroids.add(new Asteroid(mt.nextInt(900)+100, mt.nextInt(600)+100, mt.nextInt((score / 50))+50, mt.nextInt(5), mt.nextInt(5), mt.nextBoolean()));
  102.             }
  103.         }
  104.         if (collisionsThisLoop > 10)
  105.         {
  106.             win = true;
  107.         }
  108.         if (win)
  109.         {
  110.             alive = false;
  111.         }
  112.     }
  113.  
  114.     public void render(GameContainer container, Graphics g) throws SlickException
  115.     {
  116.         g.setColor(Color.gray);
  117.         g.fillRect(0, 0, 1024, 768);
  118.         g.setColor(Color.black);
  119.         g.fillRect(0, 0, 1024, 15);
  120.         g.setColor(Color.white);
  121.         g.fillRect(percentagePositive, 0, 1024 - percentagePositive, 15);
  122.         g.drawLine(512, 15, 512, 30);
  123.         for (Asteroid a: asteroids)
  124.         {
  125.             if (a.positive)
  126.             {
  127.                 g.setColor(Color.black);
  128.             }
  129.             else
  130.             {
  131.                 g.setColor(Color.white);
  132.             }
  133.             if (a.spawner == 0)
  134.             {
  135.                 if (!a.positive)
  136.                 {
  137.                     g.fillOval(a.x - (a.size / 2) - 2, a.y - (a.size / 2) - 2, a.size, a.size);
  138.                 }
  139.                 else
  140.                 {
  141.                     g.fillOval(a.x - (a.size / 2) - 2, a.y - (a.size / 2) - 2, a.size, a.size);
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 g.drawOval(a.x - (a.size / 2) - 2, a.y - (a.size / 2) - 2, a.size, a.size);
  147.             }
  148.         }
  149.  
  150.         if (!positive)
  151.         {
  152.             g.setColor(Color.white);
  153.         }
  154.         else
  155.         {
  156.             g.setColor(Color.black);
  157.         }
  158.         g.fillOval(playerX - 8, playerY - 8, 16, 16);
  159.  
  160.         if (!positive)
  161.         {
  162.             g.setColor(Color.white);
  163.         }
  164.         else
  165.         {
  166.             g.setColor(Color.black);
  167.         }
  168.         g.drawString("Score: " + score, 15, 15);
  169.         if (win)
  170.         {
  171.             g.drawString("YOU WIN!", 400, 300);
  172.         }
  173.     }
  174.  
  175.     public void updateAsteroids()
  176.     {
  177.         float positiveMass = 0;
  178.         float overallMass = 0;
  179.         for (Asteroid a: asteroids)
  180.         {
  181.             if (a.positive)
  182.             {
  183.                 positiveMass += a.size;
  184.                 overallMass += a.size;
  185.             }
  186.             else
  187.             {
  188.                 overallMass += a.size;
  189.             }
  190.             float changeX = 0;
  191.             float changeY = 0;
  192.             a.colliding = false;
  193.             if (a.spawner > 0)
  194.             {
  195.                 a.spawner --;
  196.             }
  197.             for (Asteroid b: asteroids)
  198.             {
  199.                 if (a != b)
  200.                 {
  201.                     float x1 = (a.x - b.x);
  202.                     float y1 = (a.y - b.y);
  203.  
  204.                     if ( Math.sqrt((x1 * x1) + (y1 * y1)) < (a.size / 2) + (b.size / 2))
  205.                     {
  206.                         a.colliding = true;
  207.                         //Calculate Seperation
  208.                         float angle = (float) Math.atan2(b.y - a.y, b.x - a.x);
  209.                         changeX =  -5 * (float) Math.cos(angle);
  210.                         changeY =  -5 * (float) Math.sin(angle);
  211.                     }
  212.                 }
  213.             }
  214.             if (a.colliding)
  215.             {
  216.                 //Perform seperation
  217.                 a.dx += changeX * 0.90;
  218.                 a.dy += changeY * 0.90;
  219.                 collisionsThisLoop ++;
  220.             }
  221.             if (a.dx > 5)
  222.             {
  223.                 a.dx = 5;
  224.             }
  225.             if (a.dx < -5)
  226.             {
  227.                 a.dx = -5;
  228.             }
  229.             if (a.dy > 5)
  230.             {
  231.                 a.dy = 5;
  232.             }
  233.             if (a.dy < -5)
  234.             {
  235.                 a.dy = -5;
  236.             }
  237.             if (a.positive != positive)
  238.             {
  239.                 if (a.x > playerX)
  240.                 {
  241.                     a.dx -= 0.10f;
  242.                 }
  243.                 if (a.x < playerX)
  244.                 {
  245.                     a.dx += 0.10f;
  246.                 }
  247.                 if (a.y < playerY)
  248.                 {
  249.                     a.dy += 0.10f;
  250.                 }
  251.                 if (a.y > playerY)
  252.                 {
  253.                     a.dy -= 0.10f;
  254.                 }
  255.             }
  256.             else
  257.             {
  258.                 if (a.x > playerX)
  259.                 {
  260.                     a.dx += 0.10f;
  261.                 }
  262.                 if (a.x < playerX)
  263.                 {
  264.                     a.dx -= 0.10f;
  265.                 }
  266.                 if (a.y < playerY)
  267.                 {
  268.                     a.dy -= 0.10f;
  269.                 }
  270.                 if (a.y > playerY)
  271.                 {
  272.                     a.dy += 0.10f;
  273.                 }
  274.             }
  275.             if (a.x + a.dx > 1024 - (a.size / 2))
  276.             {
  277.                 a.dx = -a.dx;
  278.             }
  279.             if (a.x + a.dx < 0 + (a.size / 2))
  280.             {
  281.                 a.dx = -a.dx;
  282.             }
  283.             if (a.y + a.dy > 768 - (a.size / 2))
  284.             {
  285.                 a.dy = -a.dy;
  286.             }
  287.             if (a.y + a.dy < 15 + (a.size / 2))
  288.             {
  289.                 a.dy = -a.dy;
  290.             }
  291.             a.x += a.dx;
  292.             a.y += a.dy;
  293.         }
  294.         if (overallMass != 0)
  295.         {
  296.             percentagePositive = (int) ((positiveMass / overallMass) * 1024);
  297.         }
  298.         else
  299.         {
  300.             percentagePositive = 1024;
  301.         }
  302.     }
  303.  
  304.     private void updatePlayer()
  305.     {
  306.         float changeX = 0;
  307.         float changeY = 0;
  308.         boolean asteroidCharge = false;
  309.         boolean colliding = false;
  310.         for (Asteroid a: asteroids)
  311.         {
  312.             if (a.spawner == 0 && (a.positive != positive))
  313.             {
  314.                 float x1 = (a.x - playerX);
  315.                 float y1 = (a.y - playerY);
  316.                 if ( Math.sqrt((x1 * x1) + (y1 * y1)) < (a.size / 2) + 8)
  317.                 {
  318.                     //Calculate Seperation
  319.                     float angle = (float) Math.atan2(playerY - a.y, playerX - a.x);
  320.                     changeX +=  8 * (float) Math.cos(angle);
  321.                     changeY +=  8 * (float) Math.sin(angle);
  322.                     asteroidCharge = a.positive;
  323.                     colliding = true;
  324.                 }
  325.             }
  326.         }
  327.         if (colliding)
  328.         {
  329.             alive = false;
  330.         }
  331.         int newX = input.getMouseX();
  332.         int newY = input.getMouseY();
  333.         if (newX >= 0 && newX < 1024 && newY > 15 && newY < 768)
  334.         {
  335.             if (Math.abs(lastX - newX) < 50 && Math.abs(lastY - newY) < 50)
  336.             {
  337.                 playerX = newX;
  338.                 playerY = newY;
  339.                 lastX = newX;
  340.                 lastY = newY;
  341.             }
  342.         }
  343.     }
  344. }
  345.  
  346. /*
  347.  * To change this template, choose Tools | Templates
  348.  * and open the template in the editor.
  349.  */
  350.  
  351. package spheres;
  352.  
  353. /**
  354.  *
  355.  * @author Walter
  356.  */
  357. public class Asteroid {
  358.  
  359.     public float x, y, dx, dy;
  360.     public int size;
  361.     public boolean colliding;
  362.     public boolean positive;
  363.     public int spawner;
  364.  
  365.     Asteroid(int x, int y, int size, int dx, int dy, boolean positive)
  366.     {
  367.         this.x = x;
  368.         this.y = y;
  369.         this.size = size;
  370.         this.dx = dx;
  371.         this.dy = dy;
  372.         this.spawner = 255;
  373.         this.positive = positive;
  374.     }
  375. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post