Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://stackoverflow.com/questions/5956194/why-isnt-my-if-else-block-ever-getting-hit-even-though-it-should-be-just-need/5956308#5956308
- //Help me out!
- //And you know...
- // don't steal my code, thanks!
- import java.applet.*;
- import java.awt.*;
- import java.util.Random;
- public class GameMain extends Applet implements Runnable {
- private int countLeft, countRight, countCenter;
- //Frame Settings
- private int width;
- private int height;
- private int left, top;
- private int playTop, playRight, playFloor, playLeft;
- private int worldWidth, worldHeight;
- //Game Constants
- private final int BASE_GENERATOR_WIDTH = 5;
- private final Color EMPTY = null;
- private final Color PARTICLE_SAND = new Color(255, 236, 139);
- private final Color PARTICLE_WATER = Color.BLUE;
- private final Color PARTICLE_SALT = new Color(252, 252, 252);
- private final Color PARTICLE_OIL = new Color(133, 99, 99);
- //User Variables
- private int GENERATOR_SAND = 5;
- private int GENERATOR_WATER = 5;
- private int GENERATOR_SALT = 5;
- private int GENERATOR_OIL = 5;
- private boolean HAS_FLOOR = true;
- //World
- private Color[][] world;
- private Random rand;
- public static Toolkit k = Toolkit.getDefaultToolkit();
- private Thread gameThread = null;
- private Graphics bufferGraphics;
- private Image offscreen;
- public void init() {
- width = 700;
- height = 600;
- //left = (k.getScreenSize().width - width) / 2;
- //top = 20;
- top=0;
- left=0;
- playTop = top + 3;
- playFloor= (top+height) - (height/7);
- playLeft = left + 3;
- playRight = width - 6;
- worldWidth = playRight - playLeft;
- worldHeight = playFloor - playTop;
- world = new Color[worldWidth][worldHeight+1];
- rand = new Random();
- for(int i = 0; i < world.length; i++) {
- for(int j = 0; j < world[i].length; j++) {
- world[i][j] = EMPTY;
- }
- }
- offscreen = createImage(width, height);
- bufferGraphics = offscreen.getGraphics();
- countCenter = countLeft = countRight = 0;
- gameThread = new Thread(this, "Game");
- gameThread.start();
- }
- public void run() {
- //System.out.println("run() reached");
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while(true) {
- //System.out.println("Reached repaint");
- repaint();
- try{
- Thread.sleep(1);
- }
- catch (InterruptedException ex) {
- System.out.println("Caught!");
- }
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void stop() {
- }
- public void paint(Graphics g) {
- bufferGraphics.clearRect(playLeft, playTop, worldWidth, worldHeight);
- bufferGraphics.setColor(Color.GRAY);
- bufferGraphics.fillRect(left, top, width, height);
- bufferGraphics.setColor(Color.BLACK);
- bufferGraphics.fillRect(left+3, top+3, width-6, height-6);
- bufferGraphics.setColor(Color.GRAY);
- bufferGraphics.fillRect(left, (top+height)-(height/7), width, (height/7));
- generateStreams();
- doGravity();
- drawWorld(bufferGraphics);
- g.drawImage(offscreen, 0, 0, this);
- //repaint();
- }
- public void update(Graphics g) {
- paint(g);
- }
- private void generateStreams() {
- generateSand();
- generateWater();
- generateSalt();
- generateOil();
- }
- private void generateSand() {
- int center = worldWidth / 5;
- int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_SAND) / 2);
- int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_SAND) / 2);
- int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_SAND);
- for(int i = 0; i < generatorWidth; i++) {
- if(rand.nextInt(2) == 0) {
- world[left + i][0] = PARTICLE_SAND;
- }
- }
- }
- private void generateWater() {
- int center = worldWidth / 5 * 2;
- int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_WATER) / 2);
- int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_WATER) / 2);
- int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_WATER);
- for(int i = 0; i < generatorWidth; i++) {
- if(rand.nextInt(2) == 0) {
- world[left + i][0] = PARTICLE_WATER;
- }
- }
- }
- private void generateSalt() {
- int center = worldWidth / 5 * 3;
- int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_SALT) / 2);
- int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_SALT) / 2);
- int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_SALT);
- for(int i = 0; i < generatorWidth; i++) {
- if(rand.nextInt(2) == 0) {
- world[left + i][0] = PARTICLE_SALT;
- }
- }
- }
- private void generateOil() {
- int center = worldWidth / 5 * 4;
- int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_OIL) / 2);
- int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_OIL) / 2);
- int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_OIL);
- for(int i = 0; i < generatorWidth; i++) {
- if(rand.nextInt(2) == 0) {
- world[left + i][0] = PARTICLE_OIL;
- }
- }
- }
- private void doGravity() {
- for (int i = worldHeight - 1; i >= 0; i--) {
- ArrayList<Integer> jS = new ArrayList<Integer>();
- jS.ensureCapacity(worldWidth);
- for (int j = 0; j < worldWidth; j++) {
- jS.add(j);
- }
- Collections.shuffle(jS);
- for (int j = 0; j < worldWidth; j++) {
- int newJ = jS.get(j);
- if (world[newJ][i] != EMPTY) {
- if (hasGravity(world[newJ][i])) {
- dropParticle(newJ, i);
- }
- }
- }
- }
- }
- private boolean hasGravity(Color c) {
- if(c.equals(PARTICLE_SAND))
- return true;
- if(c.equals(PARTICLE_WATER))
- return true;
- if(c.equals(PARTICLE_SALT))
- return true;
- if(c.equals(PARTICLE_OIL))
- return true;
- return false;
- }
- private void dropParticle(int x, int y) {
- if(y == worldHeight-1) {
- if(!HAS_FLOOR) {
- world[x][y] = EMPTY;
- }
- }
- else if(world[x][y+1]==EMPTY && (x+1 < world.length) && world[x+1][y+1]==EMPTY && (x-1 >= 0) && world[x-1][y+1] == EMPTY) {
- int r = rand.nextInt(50);
- if(r == 0) {
- world[x-1][y+1] = world[x][y];
- //System.out.println("GO: right");
- //countRight++;
- }
- else if(r == 1) {
- world[x+1][y+1] = world[x][y];
- //System.out.println("GO: left");
- //countLeft++;
- }
- else {
- world[x][y+1] = world[x][y];
- //countCenter++;
- }
- world[x][y] = EMPTY;
- }
- else if(world[x][y+1]==EMPTY) {
- world[x][y+1] = world[x][y];
- world[x][y] = EMPTY;
- //countCenter++;
- }
- else if((x+1 < world.length) && world[x+1][y+1]==EMPTY && (x-1 >= 0) && world[x-1][y+1] == EMPTY) {
- if(rand.nextBoolean()) {
- world[x-1][y+1] = world[x][y];
- //countLeft++;
- }
- else {
- world[x+1][y+1] = world[x][y];
- //countRight++;
- }
- world[x][y] = EMPTY;
- }
- else if((x-1 >= 0) && world[x-1][y+1] == EMPTY) {
- world[x-1][y+1] = world[x][y];
- world[x][y] = EMPTY;
- countLeft++;
- //System.out.println("Hit Left");
- }
- else if((x+1 < world.length) && world[x+1][y+1] == EMPTY) {
- world[x+1][y+1] = world[x][y];
- world[x][y] = EMPTY;
- countRight++;
- //System.out.println("Hit Right");
- }
- System.out.println("Left: " + countLeft + " Center: " + countCenter + " Right: " + countRight);
- }
- private void drawWorld(Graphics g) {
- for(int i = 0; i < world.length; i++) {
- for(int j = 0; j < world[i].length; j++) {
- if(world[i][j] != EMPTY) {
- g.setColor(world[i][j]);
- g.fillRect(playLeft + i, playTop + j, 1, 1);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement