Trying to code some falling stars, wishing for some criticism and making the code shorter

Hello stranger,

I'm really new to Processing, I'm studying at a university and we started off with Processing, so this is me after some months working with processing and not even that deep into processing. Just the basics.

I'm watching regularly here in this forum and check some strips of codes to understand more and more.

SO HERE'S MY PROBLEM: I've coded some falling stars (pretty useless, but I had that idea and wanted to realize it.)

So there are 2 functions for now(or even 3) If you press '1' it will turn the stars upside down and fly into the air and turn yellow. If you press '2' it will turn the stars downwards again and turns them again into rainbow-stars. If you press any button on your mouse it'll change all the colors from the stars.

So, here's what I ask: Please guys, this code is maybe too long and I'd like to hear from other with more experience if the code is good. BTW: There is a slight problem: If I press 1 and then again press 1 it'll turn the value from v again to -v (because it's in the code like that) and if I press then 2, it'll make the rainbow-stars fly upwards and vanish from the screen. I'd love to fix that instead of making another 2 if-statement to extinguish that problem.

I've made another tab for the class, but that's up to you

Star[] lol = new Star[100];

void setup () {
  size (500, 500);
  for (int i = 0; i < lol.length; i++) {
  lol[i] = new Star(random(0, width+150), random(-40, height+40), random(2, 5), random(0, 255), random(0, 255), random(0, 255), 1);
 }
}

void draw () {
  smooth();
  background(0);
  for (int i = 0; i < lol.length; i++) {
    scale(0.7);
    lol[i].render();    // Drawing Stars
    scale(1.429);
    lol[i].move();      // Move and fade out
}

void keyPressed () {
  for (int i = 0; i < lol.length; i++) {
    lol[i].yellowstars();
  }
}

void mousePressed() {
  for (int i = 0; i < lol.length; i++) {
    lol[i].coloring();
  }
}

class Star {
  float x;
  float y;
  float v;
  float f1, f2, f3;
  int state;
  boolean col;
  boolean yellow;

  Star(float x, float y, float v, float f1, float f2, float f3, int state) {
    this.x = x;
    this.y = y;
    this.v = v;
    this.f1 = f1;
    this.f2 = f2;
    this.f3 = f3;
    this.state = state;
    col = true;
    yellow = false;
  }

  void render() {
    noStroke();
    fill(f1, f2, f3);
    beginShape();
    vertex(x, y+20);  // 1
    vertex(x+15, y+31); // 2
    vertex(x+10, y+50); // 3
    vertex(x+25, y+37); // 4
    vertex(x+40, y+50); // 5
    vertex(x+35, y+31); // 6
    vertex(x+50, y+20); // 7
    vertex(x+32, y+20); // 8
    vertex(x+25, y);  // 9
    vertex(x+18, y+20); // 10
    vertex(x+4, y+20);  // 11
    endShape(CLOSE);
  }

  void move() {
    y += v;
    if (state == 1) {
      col = true;
      if (y >= 500 && f1 != 0 && f2 != 0 && f3 !=0 && col == true) {
        f1-=3;
        f2-=3;
        f3-=3;             
        if (f1 == 0 && f2 == 0 && f3 == 0) {
          f1 = f2 = f3 = 0;
        }

        if (x >= width+150 || x <= 0 || y >= 700) {
          x= random(0, width+150);
          y= -30;
          f1 = random(255);
          f2 = random(255);
          f3 = random(255);
        }
      }
    }
    if (state == 2) {
      if (y <= 200 && f1 != 0 && f2!=0 && f3!=0 && yellow == true) {
        f1-=3;
        f2-=3;
        f3-=3;
        if (f1 == 0 && f2 == 0 && f3 == 0) {
          f1 = f2 = f3 = 0;
        }
      }
      if (x <= 0 || y <= -30 && yellow == true) {
        x = random(0, width+150);
        y = 730;
        f1 = 255;
        f2 = 253;
        f3 = 70;
      }
    }
  }

  void coloring() {
    if (mousePressed = true) {
      if (col == true) {
        yellow = false;
        f1 = random(255);
        f2 = random(255);
        f3 = random(255);
      }
    }
  }

  void yellowstars() {
    if (keyPressed == true) {
      if (key == '1') {
        yellow = true;
        col = false;
        state =2;
        f1 = 255;
        f2 = 246;
        f3 = 70;
        v = -v;
      }
    }
    if (key == '2') {
      yellow = false;
      col = true;
      state = 1;
      f1 = random(255);
      f2 = random(255);
      f3 = random(255);
      v = -v;
    }
  }
}

Thank you guys.. Sorry for the long post.

Answers

  • edited February 2016

    If I press 1 and then again press 1 it'll turn the value from v again to -v (because it's in the code like that) and if I press then 2, it'll make the rainbow-stars fly upwards and vanish from the screen. I'd love to fix that instead of making another 2 if-statement to extinguish that problem.

    just check to see if it's already negative before making it negative... two lines of code. or use abs() to force it.

    a = abs(-a);  // a will always be positive
    a = -abs(-a); // a will always be negative
    

    https://processing.org/reference/abs_.html

  • I didnt even know about abs, god damn it, thanks :D

  • Afaik you could store the shape with the vertex stuff that you have in render() now in a PShape (in the constructor) and

    then in render just say shape (shapeStar, x,y);

    could be a little faster or elegant?

  • Instead of f1,f2,f3

    Better use datatype color

  • Chrisir, before I can load a shape don't I first of all have to import a vector graphic in order to being able to PShape it?

    And for f1,f2,f3 I could use the datatype color or even abuse PVector.x,.y,.z, right? (Just wondering if that would work)

    But thanks! I checked PShape in YouTube and learned something new, thanks a lot :)

  • using a color would make it difficult to add and subtract bits from each of the channels individually. a pvector would be better.

  • okay, so that means PVector works, thanks a lot :)

  • edited February 2016

    No, I would use the type color here, not PVector (which even is float not int!!!) - but do as you like

    there is more: see that keyPressed and mousePressed call a function in the class which in turn ask if (keyPressed == true) { and if (mousePressed = true) { ? Makes no sense, they are true because of the way the functions are called.

    then look at this

    for (int i = 0; i < lol.length; i++) {
        lol[i].yellowstars();
    

    in yellowstars you ask if (key == '1') { and if (key == '2') { - if is always cheaper outside the for loop then inside (you have it inside)

    So

    void keyPressed () {
      if(key==1)
      for (int i = 0; i < lol.length; i++) {
        lol[i].yellowstars(1);
      }
      else if (key==2)
          for (int i = 0; i < lol.length; i++) {
            lol[i].yellowstars(2);
          }
    
    }
    

    or even

      void keyPressed () {
          if(key==1||key==2)
          for (int i = 0; i < lol.length; i++) {
            lol[i].yellowstars(key);
          }
     }
    

    Also

    did you notice that col is off and when yellow is on and vice versa?

    kill both and make a state 3 that makes up for them

    Also

    && col == true is just && col

    Also

    if (f1 == 0 && f2 == 0 && f3 == 0) {
              f1 = f2 = f3 = 0;
            }
    

    ???

  • just for fun I made the PShape thing

    Star[] lol = new Star[100];
    
    void setup () {
      size (500, 500);
      for (int i = 0; i < lol.length; i++) {
        lol[i] = new Star(random(0, width+150), random(-40, height+40), random(2, 5), random(0, 255), random(0, 255), random(0, 255), 1);
      }
    }
    
    void draw () {
      smooth();
      background(0);
      for (int i = 0; i < lol.length; i++) {
        scale(0.7);
        lol[i].render();    // Drawing Stars
        scale(1.429);
        lol[i].move();      // Move and fade out
      }
    }
    
    
    void keyPressed () {
      for (int i = 0; i < lol.length; i++) {
        lol[i].yellowstars();
      }
    }
    
    void mousePressed() {
      for (int i = 0; i < lol.length; i++) {
        lol[i].coloring();
      }
    }
    
    class Star {
      float x;
      float y;
      float v;
      float f1, f2, f3;
      int state;
      boolean col;
      boolean yellow;
    
      PShape s; 
    
      Star(float x, float y, float v, float f1, float f2, float f3, int state) {
        this.x = x;
        this.y = y;
        this.v = v;
        this.f1 = f1;
        this.f2 = f2;
        this.f3 = f3;
        this.state = state;
        col = true;
        yellow = false;
    
        s=createShape();
        s.beginShape(); 
        s.noStroke();
        s.fill(f1, f2, f3);
        s.vertex(x, y+20);  // 1
        s.vertex(x+15, y+31); // 2
        s.vertex(x+10, y+50); // 3
        s.vertex(x+25, y+37); // 4
        s.vertex(x+40, y+50); // 5
        s.vertex(x+35, y+31); // 6
        s.vertex(x+50, y+20); // 7
        s.vertex(x+32, y+20); // 8
        s.vertex(x+25, y);  // 9
        s.vertex(x+18, y+20); // 10
        s.vertex(x+4, y+20);  // 11
        s.endShape(CLOSE);
      }
    
      void render() {
        s.setFill(color(f1, f2, f3)); 
        shape(s, x, y);
      }
    
      void move() {
        y += v;
        if (state == 1) {
          col = true;
          if (y >= 500 && f1 != 0 && f2 != 0 && f3 !=0 && col == true) {
            f1-=3;
            f2-=3;
            f3-=3;             
            if (f1 == 0 && f2 == 0 && f3 == 0) {
              f1 = f2 = f3 = 0;
            }
    
            if (x >= width+150 || x <= 0 || y >= 700) {
              x= random(0, width+150);
              y= -30;
              f1 = random(255);
              f2 = random(255);
              f3 = random(255);
            }
          }
        }
        if (state == 2) {
          if (y <= 200 && f1 != 0 && f2!=0 && f3!=0 && yellow == true) {
            f1-=3;
            f2-=3;
            f3-=3;
            if (f1 == 0 && f2 == 0 && f3 == 0) {
              f1 = f2 = f3 = 0;
            }
          }
          if (x <= 0 || y <= -30 && yellow == true) {
            x = random(0, width+150);
            y = 730;
            f1 = 255;
            f2 = 253;
            f3 = 70;
          }
        }
      }
    
      void coloring() {
        if (mousePressed = true) {
          if (col == true) {
            yellow = false;
            f1 = random(255);
            f2 = random(255);
            f3 = random(255);
          }
        }
      }
    
      void yellowstars() {
        if (keyPressed == true) {
          if (key == '1') {
            yellow = true;
            col = false;
            state =2;
            f1 = 255;
            f2 = 246;
            f3 = 70;
            v = -v;
          }
        }
        if (key == '2') {
          yellow = false;
          col = true;
          state = 1;
          f1 = random(255);
          f2 = random(255);
          f3 = random(255);
          v = -v;
        }
      }
    }
    
  • Wow, thanks a lot for the advices, I'll go take a shower and start programming and correcting some things! I'll be back this evening and hopefully with a new function.

    I'm working on rotate and making a new button, so the stars will start to rotate :)

  • Also

    if (f1 == 0 && f2 == 0 && f3 == 0) {
              f1 = f2 = f3 = 0;
            }
    

    I made that one, because I was worried my numbers would go into the negative section, so meaning that it'll actually start again from 255 and count downwards again to 0 and again start the same process. I've wanted the fade-out-effect so the stars would vanish nicely without looking stupid. :) So the code was to let my f1, f2, f3 stay at 0 if they reach 0

  •     if (f1 == 0 && f2 == 0 && f3 == 0) {
              f1 = f2 = f3 = 0;
        }
    

    better use <= then

        if (f1 <= 0 && f2 <= 0 && f3 <= 0) {
              f1 = f2 = f3 = 0;
        }
    
  • Okay :) I didnt take <= because it was counting with a solid 3 and not 2.9 or so, but I see your point is valid. I'll change it and think about it in the future.

    I'll try myself adding a rotate function now. Thanks for the help :)

  • I sometimes use

    <= 0.01

  • But when it's equal 0

    you don't have to set it to 0

  • I'm kinda experiencing a lot of problems with my falling stars, it's difficult to make them rotate, because I'd need to translate every time I want to make my stars rotate. How can I rotate them? I mean, okay, its easier with a rectangle, because it's easier due to 1 call (rect - bla bla) but with beginShape and creating it with vertex it kinda bothers me and I dont know how to..

  • Okay forget what I said, solved the problem.. :D I found out about the resetMatrix().. :)

  • yeah

    or just use pushMatrix and popMatrix

  • I think pushMatrix and popMatrix will bring up the problem, that you cant call it more than 32 times :)

  • edited February 2016

    no, when you call it for each star, pushMatrix and popMatrix, you are closing the pair - you can do this as often as you like

    difficult is just when open a lot of pushMatrix without closing them by calling popMatrix

Sign In or Register to comment.