We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone ! My friends and I have just gotten into coding and have a few wonders about the role of some lines in a code. We are creating a « Piano Tiles » bootleg and used the majority of an already existing code but we can't figure out what these lines mean and what is their purpose. We hope you can help us understand the things that are still beyond comprehension for us !
for(int A=0;A<4;A++){
SquareX[A]=int(random(0,4));
SquareY[A]=A;
for(int B=0;B<4;B++){
fill(0,100);
rect(ScreenX/4*B,ScreenY-ScreenY/5,ScreenX/4,ScreenY/5);
fill(#FFF04B,100);
rect(ScreenX/4*round(SquareX[B]),ScreenY/5*SquareY[B],ScreenX/4,ScreenY/5);
if(B<3){
line(ScreenX/4*(B+1),0,ScreenX/4*(B+1),ScreenY-ScreenY/5);
line(0,ScreenY/5*(B+1),ScreenX,ScreenY/5*(B+1));
if(Mouse==true&End==false){
if(mouseX>ScreenX/4*round(SquareX[3])&mouseX<ScreenX/4*(round(SquareX[3])+1)&mouseY>ScreenY-ScreenY/5&mouseY<ScreenY){
Score++;
SquareX[3]=SquareX[2];
SquareX[2]=SquareX[1];
SquareX[1]=SquareX[0];
SquareX[0]=int(random(0,4));
Start=true;
Answers
Edit post, highlight code, press Ctrl-o to format
Done
Well, the first bit of code is setting up the values in two different arrays. There are four values in each array, at indexes 0, 1, 2, and 3, which is why there is a loop that starts with A==0, keeps going if A<4, and increases A at the end of every loop. For loops are a common thing, and if you're learning to program, you should probably get a handle on them ASAP.
The values assigned to the arrays are a random number picked from [0,1,2,3] for SquareX, and then [0,1,2,3] in order for SquareY. This code is probably what places the bars to click on in their random positions.
it's hard to comment on snippets of code without the context. but here goes
really? that's a problem. it's a for loop, executes 4 times. it's setting a pair of variables to the same random value, an integer value that's either 0, 1, 2 or 3 (not 4).
btw, variables should start with a small letter, so A should be a, SquareX should be squareX.
i've indented the second bit of code and added two last }s . this is important.
that's drawing 4 pairs of rectangles. it's adding an extra pair of lines to the first 3.
there's not really enough information to know what rectangles it's drawing (what's ScreenX? what's ScreenY?)
(if you want to know what does what, change the fill or stroke colour before each call to rect and line)
(and, again, B should be b.)
The next block of code is drawing a bunch of rectangles and lines. I would suggest you comment lines out one at a time to get a better understanding of what elements in the sketch each line is drawing.
this is shuffling the rectangles down, and adding a new first rect at a new random position.
all this is conditional on some mouse properties, mouse button pressed, mouse position? can't tell.
The third bit of code looks like it is moving the squares. That is, it copies each value in the SquareX array to the next position in the array, then puts a new random value in the first position.
Not much more can be said about this code without seeing the full, complete sketch. Post the whole thing!
The full code is here if it can help you, thanks for all your answers !