Posted by Epitaph64 on Wed 3 Jun 07:25
report abuse | download | new post
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package spheres;
- import java.util.ArrayList;
- import org.newdawn.slick.AppGameContainer;
- import org.newdawn.slick.BasicGame;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Image;
- import org.newdawn.slick.Input;
- import org.newdawn.slick.SlickException;
- public class Main extends BasicGame {
- private MersenneTwisterFast mt;
- private Input input;
- //Player related variables
- private float playerX, playerY, playerDX, playerDY;
- private boolean alive = true;
- private boolean positive = true;
- private int lastX, lastY;
- private int score = 0;
- private Image bg;
- private Image bg2;
- private Image asteroid;
- private Image asteroidInverse;
- private int percentagePositive;
- private int collisionsThisLoop = 0;
- private ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
- private boolean win = false;
- public Main()
- {
- super("Spheres!");
- }
- try
- {
- AppGameContainer app = new AppGameContainer(new Main());
- app.setDisplayMode(1024, 768, false);
- app.setShowFPS(false);
- app.setFullscreen(false);
- app.setMaximumLogicUpdateInterval(10);
- app.setMinimumLogicUpdateInterval(4);
- app.setVSync(true);
- app.start();
- }
- catch (SlickException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public void init(GameContainer container) throws SlickException
- {
- asteroids.clear();
- input = container.getInput();
- playerX = input.getMouseX();
- playerY = input.getMouseY();
- lastX = (int) playerX;
- lastY = (int) playerY;
- mt = new MersenneTwisterFast();
- for (int i = 0; i < 8; i++)
- {
- asteroids.add(new Asteroid(mt.nextInt(900)+100, mt.nextInt(600)+100, mt.nextInt(100)+50, mt.nextInt(5), mt.nextInt(5), mt.nextBoolean()));
- }
- }
- @Override
- public void update(GameContainer container, int delta) throws SlickException
- {
- collisionsThisLoop = 0;
- if (input.isKeyDown(Input.KEY_ESCAPE)) container.exit();
- if (input.isKeyDown(Input.KEY_F1)) container.reinit();
- if (input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) positive = !positive;
- if (alive)
- {
- if ((percentagePositive > 512 && !positive) || (percentagePositive <= 512 && positive))
- {
- score ++;
- }
- updatePlayer();
- updateAsteroids();
- if (score % 200 == 0)
- {
- 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()));
- }
- }
- if (collisionsThisLoop > 10)
- {
- win = true;
- }
- if (win)
- {
- alive = false;
- }
- }
- {
- g.fillRect(0, 0, 1024, 768);
- g.fillRect(0, 0, 1024, 15);
- g.fillRect(percentagePositive, 0, 1024 - percentagePositive, 15);
- g.drawLine(512, 15, 512, 30);
- for (Asteroid a: asteroids)
- {
- if (a.positive)
- {
- }
- else
- {
- }
- if (a.spawner == 0)
- {
- if (!a.positive)
- {
- g.fillOval(a.x - (a.size / 2) - 2, a.y - (a.size / 2) - 2, a.size, a.size);
- }
- else
- {
- g.fillOval(a.x - (a.size / 2) - 2, a.y - (a.size / 2) - 2, a.size, a.size);
- }
- }
- else
- {
- g.drawOval(a.x - (a.size / 2) - 2, a.y - (a.size / 2) - 2, a.size, a.size);
- }
- }
- if (!positive)
- {
- }
- else
- {
- }
- g.fillOval(playerX - 8, playerY - 8, 16, 16);
- if (!positive)
- {
- }
- else
- {
- }
- g.drawString("Score: " + score, 15, 15);
- if (win)
- {
- g.drawString("YOU WIN!", 400, 300);
- }
- }
- public void updateAsteroids()
- {
- float positiveMass = 0;
- float overallMass = 0;
- for (Asteroid a: asteroids)
- {
- if (a.positive)
- {
- positiveMass += a.size;
- overallMass += a.size;
- }
- else
- {
- overallMass += a.size;
- }
- float changeX = 0;
- float changeY = 0;
- a.colliding = false;
- if (a.spawner > 0)
- {
- a.spawner --;
- }
- for (Asteroid b: asteroids)
- {
- if (a != b)
- {
- float x1 = (a.x - b.x);
- float y1 = (a.y - b.y);
- {
- a.colliding = true;
- //Calculate Seperation
- }
- }
- }
- if (a.colliding)
- {
- //Perform seperation
- a.dx += changeX * 0.90;
- a.dy += changeY * 0.90;
- collisionsThisLoop ++;
- }
- if (a.dx > 5)
- {
- a.dx = 5;
- }
- if (a.dx < -5)
- {
- a.dx = -5;
- }
- if (a.dy > 5)
- {
- a.dy = 5;
- }
- if (a.dy < -5)
- {
- a.dy = -5;
- }
- if (a.positive != positive)
- {
- if (a.x > playerX)
- {
- a.dx -= 0.10f;
- }
- if (a.x < playerX)
- {
- a.dx += 0.10f;
- }
- if (a.y < playerY)
- {
- a.dy += 0.10f;
- }
- if (a.y > playerY)
- {
- a.dy -= 0.10f;
- }
- }
- else
- {
- if (a.x > playerX)
- {
- a.dx += 0.10f;
- }
- if (a.x < playerX)
- {
- a.dx -= 0.10f;
- }
- if (a.y < playerY)
- {
- a.dy -= 0.10f;
- }
- if (a.y > playerY)
- {
- a.dy += 0.10f;
- }
- }
- if (a.x + a.dx > 1024 - (a.size / 2))
- {
- a.dx = -a.dx;
- }
- if (a.x + a.dx < 0 + (a.size / 2))
- {
- a.dx = -a.dx;
- }
- if (a.y + a.dy > 768 - (a.size / 2))
- {
- a.dy = -a.dy;
- }
- if (a.y + a.dy < 15 + (a.size / 2))
- {
- a.dy = -a.dy;
- }
- a.x += a.dx;
- a.y += a.dy;
- }
- if (overallMass != 0)
- {
- percentagePositive = (int) ((positiveMass / overallMass) * 1024);
- }
- else
- {
- percentagePositive = 1024;
- }
- }
- private void updatePlayer()
- {
- float changeX = 0;
- float changeY = 0;
- boolean asteroidCharge = false;
- boolean colliding = false;
- for (Asteroid a: asteroids)
- {
- if (a.spawner == 0 && (a.positive != positive))
- {
- float x1 = (a.x - playerX);
- float y1 = (a.y - playerY);
- {
- //Calculate Seperation
- asteroidCharge = a.positive;
- colliding = true;
- }
- }
- }
- if (colliding)
- {
- alive = false;
- }
- int newX = input.getMouseX();
- int newY = input.getMouseY();
- if (newX >= 0 && newX < 1024 && newY > 15 && newY < 768)
- {
- {
- playerX = newX;
- playerY = newY;
- lastX = newX;
- lastY = newY;
- }
- }
- }
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package spheres;
- /**
- *
- * @author Walter
- */
- public class Asteroid {
- public float x, y, dx, dy;
- public int size;
- public boolean colliding;
- public boolean positive;
- public int spawner;
- Asteroid(int x, int y, int size, int dx, int dy, boolean positive)
- {
- this.x = x;
- this.y = y;
- this.size = size;
- this.dx = dx;
- this.dy = dy;
- this.spawner = 255;
- this.positive = positive;
- }
- }
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.