Hello everyone,
I made an expandable digit display and I am wondering what you think of it. (As you can maybe see) I am new to coding and made this myself. I did not copy anything from the internet. Does anyone has any tips for me to improve this code?
If you press ‘q’ on your keyboard, the number will increase.
In advance, thank you!
Digit d1, d2, d3; //Each single digit
int n1, n2, n3; //The number that the digit needs to display
float sc = 2;
void setup() {
size(1500, 500);
float x = width/2;
float y = height/2;
d1 = new Digit(x+150*sc, y, sc);
d2 = new Digit(x, y, sc, #E227E5);
d3 = new Digit(x-150*sc, y, sc);
d1.sketch();
d2.sketch();
d3.sketch();
}
void draw() {
if ( n1 >= 10) {
n2 += 1;
n1 = 0;
}
if (n2 >= 10) {
n3 += 1;
n2 = 0;
}
d1.number = n1;
d2.number = n2;
d3.number = n3;
d1.execute();
d2.execute();
d3.execute();
}
void keyPressed() {
if (key == 'q') {
n1 += 1;
}
}
class Digit {
float posx, posy, size;
color c_off = #B9B9B9; //color of the number when off, is the begin/standard color
color c_on = #0BA2B4; //Standard color of the number when on
Digit(float x, float y, float scale) {
posx = x;
posy = y;
size = scale;
}
Digit(float x, float y, float scale, color on) { //extra value added to change the color of specific digits.
posx = x;
posy = y;
size = scale;
c_on = on; //color of the number when on.
}
PShape n, nm;
void sketch() { //Sketches a part of the digit
n = createShape(); //The edge shape of the digit
n.beginShape();
n.noStroke();
n.fill(c_off);
n.vertex(0, 0);
n.vertex(100, 0);
n.vertex(80, 20);
n.vertex(20, 20);
n.endShape(CLOSE);
nm = createShape(); //The middle shape of the digit
nm.beginShape();
nm.noStroke();
nm.fill(c_off);
nm.vertex(15, 0);
nm.vertex(95, 0);
nm.vertex(110, 12.5);
nm.vertex(95, 25);
nm.vertex(15, 25);
nm.vertex(0, 12.5);
nm.endShape(CLOSE);
}
//t = top, m = middle, rt = right top, rb = right bodum, b = bodum, lb = left bodum, lt = left top.
//false = the part of the digit is off. true = the part of the digit is on.
boolean t, m, rt, rb, b, lb, lt = false;
//The number that the digit has to display
int number;
void execute() {
switch(number) { //Determines which part of the digit are turned on.
case 0:
t = true;
m = false;
rt = true;
rb = true;
b = true;
lb = true;
lt = true;
break;
case 1:
t = false;
m = false;
rt = true;
rb = true;
b = false;
lb = false;
lt = false;
break;
case 2:
t = true;
m = true;
rt = true;
rb = false;
b = true;
lb = true;
lt = false;
break;
case 3:
t = true;
m = true;
rt = true;
rb = true;
b = true;
lb = false;
lt = false;
break;
case 4:
t = false;
m = true;
rt = true;
rb = true;
b = false;
lb = false;
lt = true;
break;
case 5:
t = true;
m = true;
rt = false;
rb = true;
b = true;
lb = false;
lt = true;
break;
case 6:
t = true;
m = true;
rt = false;
rb = true;
b = true;
lb = true;
lt = true;
break;
case 7:
t = true;
m = false;
rt = true;
rb = true;
b = false;
lb = false;
lt = false;
break;
case 8:
t = true;
m = true;
rt = true;
rb = true;
b = true;
lb = true;
lt = true;
break;
case 9:
t = true;
m = true;
rt = true;
rb = true;
b = true;
lb = false;
lt = true;
break;
default:
t = false;
m = true;
rt = false;
rb = false;
b = false;
lb = false;
lt = false;
break;
}
//The full digit gets drawn with the appropriate color (off or on).
pushMatrix();
translate(posx, posy);
scale(size);
shapeMode(CENTER);
if (t == true) {
n.setFill(c_on);
} else {
n.setFill(c_off);
}
shape(n, 0, -100); //Top
if (m == true) {
nm.setFill(c_on);
} else {
nm.setFill(c_off);
}
shape(nm, 0, 0); //Middle
pushMatrix();
rotate(PI/2);
if (rt == true) { //Right top
n.setFill(c_on);
} else {
n.setFill(c_off);
}
shape(n, -55, -50);
if (rb == true) {
n.setFill(c_on);
} else {
n.setFill(c_off);
}
shape(n, 55, -50); //Right bodum
popMatrix();
pushMatrix();
rotate(PI);
if (b == true) { //bodum side
n.setFill(c_on);
} else {
n.setFill(c_off);
}
shape(n, 0, -100);
popMatrix();
pushMatrix();
rotate(PI*1.5);
if (lt == true) { //left top
n.setFill(c_on);
} else {
n.setFill(c_off);
}
shape(n, 55, -50);
if (lb == true) { //Left bodum
n.setFill(c_on);
} else {
n.setFill(c_off);
}
shape(n, -55, -50);
popMatrix();
popMatrix();
}
}