We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
}
Answers
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 eitherline2=line2+15;
on a separate line, orline2+=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.
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?
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).
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.
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)
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
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...)
just a different explanation, maybe its helpful
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