3 digit code problem

here is version where you can type 3 digits

the cursor keys don’t work here anymore

You could click somewhere to move the text enter field to another position (but then mouseClicked is also executed) :

void mousePressed() {
  //
  C.leftMargin=mouseX;
  C.yMargin=mouseY;
}

Chrisir

secretcode C = new secretcode();

PImage Basementstickers;
PImage Tafel_omlaag;
PImage Tafel_omhoog;
PImage pizzaopen;
PImage pizzadicht;
PImage Pijl;
PImage Key1;
PImage SDG2;
PImage SDG2_blank;
PImage SDG3;
PImage SDG3_blank;
PImage SDG7;
PImage SDG7_blank;
PImage Stickers;
PImage kast;
PImage kader;

Boolean kelder=true;
Boolean Tafel=false;
PImage Homer;
Boolean pizza=false;
Boolean closet=false;

int[] vlijnen = { 250, 385, 400, 470, 490, 554, 614, 700, 800 };
int[] hlijnen = {36, 110, 165, 260, 300, 310, 350, 380, 400, 435, 480, 550};

String errorText = ""; 

void setup() {
  size(1280, 720);

  //display();

  Basementstickers= loadImage("Basementstickers.png");
  pizzadicht = loadImage("pizzadicht.png");
  pizzaopen = loadImage("pizzaopen.png");
  Pijl= loadImage("Pijl.jpg");
  Key1= loadImage("Key1.png");
  SDG2= loadImage("SDG2.jpeg");
  SDG3= loadImage("SDG3.jpg");
  SDG7= loadImage ("SDG7.png");
  Stickers= loadImage("Stickers.png");
  Tafel_omlaag= loadImage("Tafel_omlaag.png");
  Tafel_omhoog= loadImage("Tafel_omhoog.png");
  kast = loadImage("kast1.png");
  kader = loadImage("unnamed.png");

  // background(Basementstickers);

  Homer= loadImage("Homer.png");

  C.setupsecretcode();

  noStroke();
}

void draw() {

  if (kelder == true) {
    //
    // C.play();

    if (kader!=null) {
      image(kader, 1080, 10, 200, 200);
      image(kader, 1080, 200, 200, 200);
      image(kader, 1080, 390, 200, 200);
    }
  }

  for (int i = 0; i < vlijnen.length; i++) {
    line(vlijnen[i], 0, vlijnen[i], 720);
  }
  for (int i = 0; i < hlijnen.length; i++) {
    line(0, hlijnen[i], 1280, hlijnen[i]);
  }

  // error text
  fill(255, 0, 0); 
  text(errorText, 
    width-310, height-32);

  // enter 3 digits 
  C.drawsecretcode() ;
}

void mouseClicked() {
  //Sticker op tafel
  if (mouseX>750 && mouseX<850 && mouseY>200 && mouseY<300 && kelder==false && Tafel==true) {
    background(Tafel_omhoog);
    image(Pijl, 0, 620, 100, 100);
  }
  //blad omhoog doen op tafel
  if (mouseX>700 && mouseX<800 && mouseY>300 && mouseY<400 && kelder==true && Tafel==false) {
    background(Tafel_omlaag); 
    image(Pijl, 0, 620, 100, 100);
    kelder=false;
    Tafel=true;
  }
  //stickers in kader
  if (mouseX>385 &&mouseX<470 && mouseY>36 && mouseY<165 && kelder==true ) {
    background(Stickers);
    image(Pijl, 0, 620, 100, 100);
    kelder=false;
  }
  //terug naar kelder
  if (mouseX<100 &&mouseX>0 && mouseY<720 && mouseY>620 && kelder==false ) {
    background(Basementstickers);
    kelder=true;
    Tafel=false;
  }
  //pizzadoos laten zien
  if (mouseX<490 && mouseX>400 && mouseY<480 && mouseY>435 && kelder==true &&pizza==false) {
    background(pizzadicht);
    image(Pijl, 0, 620, 100, 100);
    kelder = false;
    pizza = true;
  }
  if (mouseX<700 && mouseX>470 && mouseY>350 && mouseY<550 && kelder==false && pizza==true) {
    background(pizzaopen);
    image(Pijl, 0, 620, 100, 100);
    kelder= false;
    pizza = false;
  }
  //kast
  if (mouseX<400 && mouseX>250 && mouseY>110 && mouseY<380 && kelder==true && closet==false) {
    background(kast);
    image(Pijl, 0, 620, 100, 100);
    kelder = false;
    closet = true;
  }
  if (mouseX<310 && mouseX>260 && mouseY>552 && mouseY<650 && kelder==false && closet==true) {
  }
}

void keyPressed() {
  C. keyPressedsecretcode();
}

// ================================================================================

class secretcode {

  PImage [] img = new PImage [10] ;

  PImage code;
  int a = 1;
  int currentImg=1; 
  int secondImg=1;
  int thirdImg=1;
  Boolean cijfer1=false;
  Boolean cijfer2=false;
  Boolean cijfer3=false;
  PImage binnenkant;

  String result="";

  int leftMargin = 100;
  float yMargin;
  // final int buffMax = 5;
  final color BG  = color(2), FG2 = color(255, 0, 0), FG = 0;

  String s = "Ga naar het eerste cijfer met rechter pijltje, naar het tweede cijfer met pijltje omhoog en naar het derde cijfer met linker pijltje";

  void setupsecretcode() {

    yMargin=height-18; // define y position 

    binnenkant = loadImage("binnenkant.jpg");
    if (binnenkant!=null)binnenkant.resize(width, height);
    code = loadImage("Code.png");
    if (code!=null)code.resize(width, height);

    for (int i=1; i<=9; i = i+1) {
      img[i] = loadImage(i+".png");
    }
    textSize(20);
  }

  void drawsecretcode() {

    // enter a 3 digit number 

    // show text
    textSize(14);  
    fill(255);
    text("Please enter 3 digit number. Submit with Enter. You can use Backspace. Try 123 and 124. ", 
      leftMargin, yMargin-22);

    fill(111); 
    stroke(255);
    rect(leftMargin-2, yMargin-14, 
      100, 19);
    fill(255);
    text(result, 
      leftMargin, yMargin);
    // show cursor
    makeRedCursorLinesecretcode();
  }//func

  void makeRedCursorLinesecretcode() {
    stroke( frameCount % 30 < 30 >>2 ? BG : FG2 );
    int rPos = (int) textWidth(result) 
      + leftMargin +1; 
    //stroke(255, 0, 0); 
    line(rPos, yMargin-9, 
      rPos, yMargin+1);
  }

  // ------------------------------------------------------------

  void keyPressedsecretcode() {
    //
    //

    // reset 
    errorText = ""; 

    // eval key 
    if (key >= '1' && key <= '9') {
      if (result.length()<3)
        result+=key;
    } 
    //---
    else if (key==RETURN || key==ENTER) {
      if (result.length()!=3) {
        println("ERROR. You must enter THREE letters.");
        errorText = "ERROR. You must enter THREE letters."; 
        return;
      }//if
      // do something here
      evaluateResult(result); 
      result="";
    }// else if ENTER 
    //---
    else if (key==BACKSPACE) {
      if (result.length()>0) {
        result=result.substring(0, result.length()-1) ;
      }
    } 
    //---
    else if (key==ESC) {
      key=0;
    }//ESC
    //
  }

  void evaluateResult(String result) {
    switch(result) {

    case "123":
      println("3 ############################################################################################"); 
      break;

    case "124":
      println("4 ############################################################################################"); 
      break;
    } //switch
    //
  }//func 

  //void keyPressed() {
  //  //first digit
  //  if (keyCode == LEFT) {
  //    a=1;
  //  }
  //  if (key >= '1' && key <= '9' && a==1) {
  //    currentImg=int(key-48);
  //  }
  //  if (keyCode == UP ) {
  //    a = 2;
  //  }
  //  //second digit
  //  if (key >= '1' && key <= '9' && a==2) {
  //    secondImg=int(key-48);
  //  }
  //  if (keyCode == RIGHT && a == 2) {
  //    a = 3;
  //  }
  //  //third digit
  //  if (key >= '1' && key <= '9'  && a==3) {
  //    thirdImg=int(key-48);
  //  }
  //}
}
//
1 Like

You helped me so much! with you code i have succesfully added my diggit code to the main code! The only problem now is that i can see the screen of the diggit code, but i can’t enter the input i want to the code…
With ‘secretcode’ i mean the code to enter 3 digits, and ‘kelder’ means basement in dutch.

It works in my code

You need this outside of the class so that you can enter digits. Do you mean this? (If not: There is also another function to act on the digits after you press enter)

When you still want to identify the cursor keys do this first and then call
C. keyPressedsecretcode();

When you still need help, please post your entire code