Traduction of the code

edited January 2018 in Questions about Code

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;
Tagged:

Answers

  • Edit post, highlight code, press Ctrl-o to format

  • 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

    for(int A=0;A<4;A++){
        SquareX[A]=int(random(0,4));
        SquareY[A]=A;
    }
    

    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.

  • 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));
      }
    }
    

    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.

  •     Score++;
        SquareX[3]=SquareX[2];
        SquareX[2]=SquareX[1];
        SquareX[1]=SquareX[0];
        SquareX[0]=int(random(0,4));
        Start=true;
    

    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 !

    /* Use your Mouse to play*/
    
    int ScreenX=440;
    int ScreenY=800;
    int FrameRate=10;
    int[] SquareX=new int[4];
    int[] SquareY=new int[4];
    int MaxTime=20;
    boolean Mouse;
    boolean Start;
    boolean End;
    int Timer;
    int Score;
    
    void setup(){
      size(ScreenX,ScreenY);
      frameRate(FrameRate);
      Mouse=false;
      Start=false;
      End=false;
      Timer=0;
      Score=0;
      for(int A=0;A<4;A++){
        SquareX[A]=int(random(0,4));
        SquareY[A]=A;
      }
    }
    
    void draw(){
      background(255);
    
      if(Start==true&Timer<MaxTime*FrameRate){
        Timer=Timer+1;
      }
      else if(Timer>=MaxTime){
        Start=false;
        End=true;
      }
    
      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;
          }
          Mouse=false;
        }
      } 
    
      if(End==true){
        fill(0,255,0,100);
        rect(0,0,ScreenX,ScreenY);
    
        fill(0);
        textSize((ScreenX*ScreenY)/10000);
        textAlign(CENTER,CENTER);
        text("End",ScreenX/2,ScreenY/4*1);
        text("SCORE:"+round(Score),ScreenX/2,ScreenY/4*2);
        text("PLAY AGAIN PRESS SPACE",ScreenX/2,ScreenY/4*3);
        if(keyPressed){
        if(key==' '){
          setup(); 
        }
        }
      }
    }
    
    void mousePressed(){
      if(mouseButton==LEFT){
        Mouse=true;
      }
    }
    
Sign In or Register to comment.