Erasing function & Loop questions

    int begin;               // Time the event/game begins
    boolean active;          // Flag for event/game status
    boolean done;            // Flag for "game over"
    PImage beginImage;       // Start with this image
    PImage endImage;         // End with this image

    float hue = random(360);
    float saturation = random(30, 50); // color's code from openprocessing's user Reona~
    float Hue = random(360);
    float Saturation = random(50, 100);
    float[] xPos = new float[20];
    float[] yPos = new float[20];// ellipses
    float[] xxPos = new float[5];
    float[] yyPos = new float[5];// squares

    int A;
    int i;
    int B;
    int C;
    int D;
    int E;
    int F;

    void setup() 
    {
      size(1000, 610); 
      active = false;       
      done = false;          // The event/game has not finished
      beginImage = loadImage("begin_image.png");
      endImage = loadImage("gameover.png");
      frameRate(60);
      smooth();
      colorMode(HSB, 360, 100, 100);
      background(360);
      ellipseMode(RADIUS);
      rectMode(RADIUS);
      A = 20;
      B = 570;
      C = 530;
      D = 690;
      E = 650;
      F = 610;

      for (int i = 0; i < 20; i++) {
        xPos[i] = random(-1000, 1000);
        yPos[i] = random(-1000, 1000);// ellipses
      }

      for (int z = 0; z < 5; z++) {
        xxPos[z] = random(0, 1000);
        yyPos[z] = random(0, 1000);// squares
      }
    }

    void draw() 
    {
      background(360);

      if (active == true) {
        eventGame();         // Run the event/game
        timer();             // Time the event/game
      } 
      else {
        if (done == true) {
          endScreen();      // Show the "end" screen
        } 
        else {
          beginScreen();     // Show the "first" screen
        }
      }
    }

    void eventGame() 
    {
      background(360);


      strokeWeight(3);
      stroke(260, 80, 100, 120);
      noFill();
      beginShape();
      vertex(mouseX-40, 450);
      vertex(mouseX-40, 500);
      vertex(mouseX-80, 500);
      vertex(mouseX-80, 600);
      vertex(mouseX+80, 600);
      vertex(mouseX+80, 500);
      vertex(mouseX+40, 500);
      vertex(mouseX+40, 450);
      endShape();// beaker

      noStroke();
      fill(186, 25, 98);
      rect(mouseX, 580, 80, A);// bule retangle


      for (int i = 0; i < 20; i++) { 
        fill(hue, saturation, 100, 120);
        ellipse(xPos[i], yPos[i], 15, 15);

        yPos[i]+=2;
        if (xPos[i] > width) {
          xPos[i] = random(-1000, -500);
        }
        if (yPos[i] > height) {
          yPos[i] = random(-1000, -500);
        }
        if (mouseX > 920) {
          mouseX = 920;
        }
        if (mouseX < 80) {
          mouseX = 80;
        }
      }       //  keep falling ellipses


      fill(Hue, Saturation, 100, 120);
      for (int z = 0; z < 5; z++) {
        rect(xxPos[z], yyPos[z], 20, 20);
        yyPos[z]+=2;
        if (xxPos[z] > width) {
          xxPos[z] = random(-1000, -500);
        }
        if (yyPos[z] > height) {
          yyPos[z] = random(-1000, -500);
        }
      }         // keep falling squares

      for (int i = 0; i < 20; i++) { 
        if (mouseX-40 < xPos[i] && mouseX+40 > xPos[i] && yPos[i] > 450 )
        { 
          xPos[i] = -999999;
          yPos[i] = -999999;
          println("good");
          A += 5;
        }
      }
      for (int z = 0; z < 5; z++) {
        if (xxPos[i] > mouseX-40 && xxPos[i] < mouseX+40 && yyPos[i] > 450 ) {
          xxPos[i] = -999999;
          yyPos[i] = -999999;
          active = false;
          done = true;     // if the mouse controlled bottle meets any square then end of the game(lose)
        }
      }
    }

    void mousePressed() 
    {
      if (active == false) {
        active = true;
        begin = millis();
      } 
      else {
        // Your mouse code goes here...
        print("mouse pressed\n");
      }
    }

    void mouseMoved() 
    {
      // Your mouse events go here...
      print("bottle moved\n");
    }

    void timer() 
    {
      int curTime = millis();
      if (curTime > begin + 20000) {
        active = false;
        done = true;
      }

      noStroke();
      fill(255);
      rect(0, height-5, width, 5);
      fill(0);
      rect(0, height-5, (curTime-begin)/20, 5);
    }

    // Displays when the game/event begins
    void beginScreen() {
      image(beginImage, 0, 0);
    }

    // Displays when the 10 seconds are over
    void endScreen() {
      image(endImage, 0, 0);
    }

I'm new to Processing and basically what I'm trying to do is to create a game to catch falling ellipses and avoid falling squares.However, I've encountered several questions.

Question 1: Why my loop for "if the mouse controlled bottle meets any square then end of the game(lose)" does not work. How can I make it work?

Question 2: How can I make the game also end (win) when "A"(the height of the water rectangle) equals to "80" ?

Question 3: How can I restart the whole game (erasing) as long as the game is ended?

Thank you so much!

Tagged:

Answers

  • _vk_vk
    edited October 2013

    Hi, would be nice if you could use liked images, the way it is, we can't really run your code. I got just a white window commenting out the images... you can do:

    endImage = loadImage("http://someplace.com/image.jpg");

    that might help you getting help (:

  • Answer ✓

    1) You loop on z but you index on i. You should have seen the error if you hadn't a useless int i; global declaration that hides the problem...

    2) Do a test?

    3) Restart a game is a very frequent question. General solution is to move the init of variables to an start() method (or other name), to call it in setup(), and to call it again when you need to restart the game. This would put the sketch back in its initial state.

Sign In or Register to comment.