While loop only executing once?

I'm almost completely new to programming in general so excuse my naivety.

I'm trying to draw a dashed line for my pong game yet it only draws the first two (the first one being in void setup and the second being inside the while loop). Surely the variables 'line1' and 'line2' shouldn't be reset as the 'void setup' block of code would only be executed again when the while evaluates to false (ie when line1 >= 600 and line2 >= 592) in which case the while would loop until the dashed line reached the bottom of the window. I don't know where I'm going wrong.

int ballX, ballY;
int ballRadius = 7;
int paddle1X, paddle1Y;
int paddle2X, paddle2Y;      
int paddle1Hor = 6, paddle1Vert = 30;    
int paddle2Hor = 6, paddle2Vert = 30;  
int i;
int line1;
int line2;


void setup()
{
  noSmooth();
  frameRate(30);
  size(800, 600);
  paddle1X = 50; 
  paddle1Y = 50; 
  paddle2X = 750;
  paddle2Y = 425;
  ballX = 398;
  ballY = 298;
  i = 0;
  line1 = 0;
  line2 = 8;
}

void draw()
{
  background(0);
  fill(255, 255, 255);
  rect(paddle1X, paddle1Y, paddle1Hor, paddle1Vert);
  rect(paddle2X, paddle2Y, paddle2Hor, paddle2Vert);
  rect(ballX, ballY, ballRadius, ballRadius);
  line(400, line1, 400, line2);
  while (line1 >= 600 && line2 >= 592);{
    line(400, line2+15, 400, line1+15);
  }
  stroke(255);

}
Tagged:

Answers

  • edited November 2017

  • You need a for loop, not a while loop.

    Also, you need to increment your values. You are returning a value line2+15 but line2 stays the same. You need to either line2=line2+15; on a separate line, or line2+=15 inline to assign a new value back to the variable.

    Are you trying to animate the drawing of the dashed line slowly over a few seconds, or just draw it all at the same time in one frame? I can't tell from your code which you were attempting.

    setup() only runs once when the sketch launches, no matter what ( unless you manually re-invoke it or set the frameCount to -1 ).

  • That ; immediately after the while is wrong too.

  • You need a for loop, not a while loop.

    So I would just do the whole "for (i > [], i ++)" etc etc for however many lines I need to reach the bottom of the screen?

    Are you trying to animate the drawing of the dashed line slowly over a few seconds, or just draw it all at the same time in one frame?

    The latter. I understand how it might appear otherwise, should I do something differently that would draw it instantly? (But still using a for/while loop).

    setup() only runs once when the sketch launches, no matter what ( unless you manually re-invoke it or set the frameCount to -1

    Yes I realize this now silly me. The point about how my variables shouldn't be reset because the void setup block wouldn't execute is still kinda true.

  • edited November 2017

    Also is there really no way to get this working with a while loop? I feel as though this should work fine:

    while (line1 >= 600 && line2 >= 592);{ line2=line2+15; line1=line1+15; line(400, line2, 400, line1); }

    (But it doesn't, two lines just run down the screen until they reach the bottom)

  • That ; immediately after the while is wrong too.

    I removed it and the while loop didn't execute? I presume you are talking about the semicolon after 592) and before the brace.

  • try this, as the entire sketch

    int i = 0;
    
    while (i < 10) {
      println(i);
      i++;
    }
    

    firstly without the ; after the while() and then insert one...

    (in the second case the semicolon acts as the statement being looped, and it'll execute that forever, the condition never becoming true because i isn't changing. you'll need to hit stop...)

  • edited November 2017

    just a different explanation, maybe its helpful

    void setup() {
    }
    
    int line1=0, line2=8;
    void draw() {
      myDraw();
    }
    // visibility check
    void myDraw() {
    
      do // the do statement sees the for loop
        for (int i =5;--i>=1; ) // the for loop sees the line func
        line(20*i, line2, 20*i, line1+++15);
      while (line1 >= 80); // while statement sees do statement
    
      // uncomment to exit when condition line1 is Bigger than 20px == true
      //myDraw2();
    }
    void myDraw2() {
    
      // second while statement sees the draw loop 
      while (line1 >= 20);
    }
    

    you see while (line1 >= 600 && line2 >= 592) ; with semicolon exits the draw loop
    and as you know already the brackets { // are used to scope the visibility }

    so in your code the variables { line2=line2+15; line1=line1+15; line(400, line2, 400, line1); } are scoped to the draw function not the while loop

Sign In or Register to comment.