> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.3AzYD9KE93O/rev.598
 * 
 * authors: 
 *   MarcoK

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/* built with Studio Sketchpad: 
 *   http://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   http://sketchpad.cc/sp/pad/view/ro.F1slU0DCRk-/rev.630
 * 
 * authors: MarcoK
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   http://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// Pressing Control-R will render this sketch.

/* @pjs preload="/static/uploaded_resources/p.21792/b1.png"; */
/* @pjs preload="/static/uploaded_resources/p.21792/b2.png"; */
/* @pjs preload="/static/uploaded_resources/p.21792/b3.png"; */
/* @pjs preload="/static/uploaded_resources/p.21792/b4.png"; */
/* @pjs preload="/static/uploaded_resources/p.21792/b1bis.png"; */
/* @pjs preload="/static/uploaded_resources/p.21792/b2bis.png"; */
/* @pjs preload="/static/uploaded_resources/p.21792/b3bis.png"; */
/* @pjs preload="/static/uploaded_resources/p.21792/b4bis.png"; */

final String path = "/static/uploaded_resources/p.21792/";
final String fontPath = "/static/uploaded_resources/p.21792/Ffscala.ttf";

class Sound{
    Audio audio;
    Sound(String src){
        this.audio = new Audio();
        this.audio.setAttribute("src", path + src);    
    }
    void playSound(){
        this.audio.play();
    }
}

class Tasto{
    int x, y;
    int w, h;
    String src;
    PImage img;
    int delay;
    Tasto(float x, float y, String src){
        this.img = loadImage(path + this.src);
        //this.w = (int) this.img.widht;
        //this.h = (int) this.img.height;
        this.w = 100;
        this.h = 100;
        this.x = (int) x;
        this.y = (int) y;
        this.src = src;
        this.delay = 0;
    }
    void show(){
        delay++;
        image(this.img, this.x, this.y);
        if(delay == 20) this.img = loadImage(path + this.src);
    }
    void changeImage(String nImg){
        this.img = loadImage(path + nImg);
        this.delay = 0;
    }
}

Tasto t1;
Sound s1;   
PFont font;
String info = "Simon!";
final int LUNGHEZZA_SEQUENZA = 20;
int counter = 6;
int rnd = 0;
var sequenza = [];
var sub_sequenza= [];
int livello = 0;
int clicks = 0;
boolean turno_computer = false;
int cnt = 0;
playerSeq = [];
String seq_str = "";
String seq_player = "";

void setup(){
    size(280,370);
    t1 = new Tasto(30,30,"b1.png");
    t2 = new Tasto(140,30,"b2.png");
    t3 = new Tasto(30,140,"b3.png");
    t4 = new Tasto(140,140,"b4.png");
    s1 = new Sound("suono1.wav");
    s2 = new Sound("suono2.wav");
    s3 = new Sound("suono3.wav");
    s4 = new Sound("suono4.wav");

    font = loadFont("FFScala.ttf"); 
    textFont(font, 24);
    textAlign(CENTER);
    
    for(int i=0; i<LUNGHEZZA_SEQUENZA;i++){
        rnd = int(random(1,5));
        sequenza.push(rnd);
        
    }
    
}

void draw(){
    background(20);
    if(frameCount == 60) info = "Are you Ready?";
    if(frameCount >= 100 && frameCount <= 200){
        info = counter;
        if(frameCount % 20 == 0) counter--;
    }
    if(frameCount == 230) mossaComputer();//inizio
    
   
    if (turno_computer == true && (frameCount % 20 == 0)){   
        if(frameCount % 50 == 0){
            if(cnt < sub_sequenza.length){
                if(sub_sequenza[cnt] == 1) {s1.playSound();t1.changeImage("b1bis.png");}
                if(sub_sequenza[cnt] == 2) {s2.playSound();t2.changeImage("b2bis.png");}
                if(sub_sequenza[cnt] == 3) {s3.playSound();t3.changeImage("b3bis.png");}
                if(sub_sequenza[cnt] == 4) {s4.playSound();t4.changeImage("b4bis.png");}
            } else{info = "Player...wait";turno_computer = false;}
            cnt++;
          }  
            
    }
        
    t1.show();
    t2.show();
    t3.show();
    t4.show();
    
    text(info, width/2, (height/2 + height/3));
    text("Level " + livello, width/2, (height/2 + height/4))
}

void mousePressed(){
    
    if(mouseX > t1.x && mouseX < (t1.x + t1.w)){
        if(mouseY > t1.y && mouseY < (t1.y + t1.h)){
             t1.changeImage("b1bis.png");
             s1.playSound();
             clicks++;
             playerSeq.push(1);
             seq_player+="1";
        }
    }
    if(mouseX > t2.x && mouseX < (t2.x + t2.w)){
        if(mouseY > t2.y && mouseY < (t2.y + t2.h)){
            t2.changeImage("b2bis.png");
             s2.playSound();
             clicks++;
             playerSeq.push(2);
             seq_player+="2";
        }
    }
    if(mouseX > t3.x && mouseX < (t3.x + t3.w)){
        if(mouseY > t3.y && mouseY < (t3.y + t3.h)){
            t3.changeImage("b3bis.png");
             s3.playSound();
             clicks++;
             playerSeq.push(3);
             seq_player+="3";
        }
    }
    if(mouseX > t4.x && mouseX < (t4.x + t4.w)){
        if(mouseY > t4.y && mouseY < (t4.y + t4.h)){
            t4.changeImage("b4bis.png");
             s4.playSound();
             clicks++;
             playerSeq.push(4);
             seq_player+="4";
        }
    }
    if(clicks == livello) {
        if(seq_player == seq_str){
            clicks = 0;
            //cnt = 0;
            seq_player = "";
            if(cnt == LUNGHEZZA_SEQUENZA){
                info = "You Win!!";
            }else{cnt = 0; mossaComputer();}
            
        }else{
            info = "Game Over!";
        }
    }
    
}

void mossaComputer(){
    info = "Computer...";
    sub_sequenza.push(sequenza[livello]);
    seq_str+=sequenza[livello];
    livello++;
    turno_computer = true;
}