/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.4apJdkJzxi9/rev.2358
*
* authors:
* Marco Torchiano
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// Pressing Control-R will render this sketch.
int h = random(11);
int m = random(59);
int WIDTH = 600;
int HEIGHT = 400;
int MARGIN = 30;
int GIRA = 0;
int QUIZ = 1;
int QUIZ_OK = 2;
int QUIZ_NO = 3;
int status = GIRA;
boolean showNumbers = true;
int correct;
int interline;
int CENTER = HEIGHT/2;
int RADIUS = CENTER - MARGIN;
String [] answers = new String[3];
void setup() { // this is run once.
// set the background color
background(255);
// canvas size (Variable aren't evaluated. Integers only, please.)
//size(WIDTH, HEIGHT);
size(600, 400);
// smooth edges
smooth();
// limit the number of frames per second
frameRate(10);
// set the width of the line.
strokeWeight(4);
font = loadFont("Arial");
textFont(font, 32);
}
void draw() { // this is run repeatedly.
background(255,255,255);
// set the color
stroke(0,0,0);
if(status==GIRA){
fill(200,200,200);
}else{
fill(0,0,0);
}
textSize(32);
text("Gira",HEIGHT+MARGIN,MARGIN);
if(status==QUIZ){
fill(200,200,200);
}else{
fill(0,0,0);
}
textSize(32);
text("Quiz",HEIGHT+MARGIN,3*MARGIN);
textSize(16);
text("Click sull'orologio mostra/nasconde i numeri",HEIGHT*2/3,HEIGHT-10);
// draw clock contour
fill(255,255,255) ;
ellipse(CENTER,CENTER,2*RADIUS,2*RADIUS);
// draw the hour marks
for(int i=0; i<12; ++i){
double angle = PI/2 - i*PI/6;
line(CENTER+RADIUS*cos(angle),CENTER-RADIUS*sin(angle),
CENTER+0.9*RADIUS*cos(angle),CENTER-0.9*RADIUS*sin(angle));
if(showNumbers){
fill(0,0,0) ;
textSize(24);
if(i==0) s = "12";
else s = ""+i;
text(s,CENTER+1.1*RADIUS*cos(angle)-textWidth(s)/2,CENTER-1.1*RADIUS*sin(angle)+textAscent()/2);
fill(128,128,128) ;
textSize(18);
s = ""+(i*5);
text(s,CENTER+0.8*RADIUS*cos(angle)-textWidth(s)/2,CENTER-0.8*RADIUS*sin(angle)+textAscent()/2);
fill(255,255,255) ;
}
}
// hours
strokeWeight(10);
stroke(200,100,100);
double h_angle = PI/2-h*PI/6-m*PI/360;
line(200,200,200+100*cos(h_angle),200-100*sin(h_angle));
// minutes
strokeWeight(6);
stroke(100,200,100);
double m_angle = PI/2-m*PI/30;
line(200,200,200+150*cos(m_angle),200-150*sin(m_angle));
strokeWeight(4);
if(status==GIRA){
update();
}else{
//stroke(255,255,255);
//fill(255,255,255);
//rect(HEIGHT+MARGIN,HEIGHT/2,WIDTH-HEIGHT,4 * interline);
stroke(0,0,0);
textSize(18);
interline = (textAscent()+textDescent())*2;
for(int i=0; i<3; ++i){
if(i==correct && status==QUIZ_OK){
fill(100,200,100);
text(answers[i] + " SI!",HEIGHT+MARGIN,HEIGHT/2+(i+1)*interline);
}else
if(i==correct && status==QUIZ_NO){
fill(200,100,100);
text(answers[i] + " No!",HEIGHT+MARGIN,HEIGHT/2+(i+1)*interline);
}else{
fill(0,0,0);
text(answers[i],HEIGHT+MARGIN,HEIGHT/2+(i+1)*interline);
}
}
}
}
void update(){
if(status==GIRA){
m++;
if(m>=60){
m=0;
h = (h+1) % 12;
}
}
}
void performQuiz(){
int secret_H = random(11.99);
int secret_M = ((int)random(11.99))*5; // easier to identify
h = (int)secret_H;
m = (int)secret_M;
correct = (int)random(2.99);
for(int i=0; i<3; ++i){
if(i==correct){
answers[i] = h + " : " + m;
}else{
answers[i] = (int)random(11.99) + " : " + ((int)random(11.99))*5;
}
}
}
void mouseClicked(){
if(mouseX > HEIGHT+MARGIN){
if(mouseY<MARGIN){ // Gira
status = GIRA;
}else
if(mouseY<3*MARGIN){ // Quiz
status = QUIZ;
performQuiz();
}else
if(mouseY>HEIGHT/2){ // quiz answers
//println("MouseY=" + mouseY);
//println("HEIGHT/2=" + HEIGHT/2);
//println("interline=" + interline);
i = (int)((mouseY-HEIGHT/2)/interline);
//println("i=" + i);
if(i==correct){
status = QUIZ_OK;
}else{
status = QUIZ_NO;
}
}
}else{
showNumbers = ! showNumbers;
}
}