Quiz quiz; void quizSetup() { norma = 10; quiz = new Quiz(); menu++; } class Quiz { //問題文 String[][] questionText = { { "○", "×", "△", "□" } , { "赤", "緑", "青", "黄" } , { "き", "た", "は", "ら" } }; color[] questionColor = { color(255, 0, 0), #01DF01, color(0, 0, 255), #D7DF01 }; //問題の種類 0:同じ色 1:同じ形 2:違う色 3:違う形 int type; //選択肢用のテキスト変数配列と文字色変数配列 String[] choiceText = new String[4]; color[] choiceColor = new color[4]; //問題文用のテキスト変数と文字色変数 String answerText; color answerColor; //正否エフェクトの表示非表示切替 boolean isEffect = false; boolean isAnswer = false; int effectTime = 0; Quiz() { float preType = random(10); //問題の種類をランダムで決定 int a = int(random(2.99)); //選択肢のパターンをランダムで決定 if (preType < 3) type = 0; else if (preType < 6) type = 1; else if (preType < 8) type = 2; else if (preType <=10) type = 3; //選択肢の順番をランダムで決定 for (int x =0; x<4;x++) { while (true) { int y = int(random(3.99)); if (choiceText[y] == null) { choiceText[y] = questionText[a][x]; break; } } //選択肢の文字色をランダムで決定 while (true) { int z = int(random(3.99)); if (choiceColor[z] == 0) { choiceColor[z] = questionColor[x]; break; } } } //問題文のテキストと文字色をランダムで決定(どちらかが問題の答えになる) answerText = choiceText[int(random(3.99))]; answerColor = choiceColor[int(random(3.99))]; } void display0() { strokeWeight(3); display1(); //問題文エリア(上半分) display2(); //選択肢エリア(下半分) if (isEffect) effect(); //エフェクト表示の変数がtrueの時、エフェクト表示 } //問題文エリア void display1() { noFill(); rect(width/2, height/4, width, height/2); //問題文 textSize(king); fill(answerColor); text(answerText, width*0.5, height*0.4); textSize(normal); fill(0); if (type <= 1) text("同じ", width*0.25, height*0.15); else text("違う", width*0.25, height*0.15); if (type == 0 ||type == 2 ) text("色はどれ?", width*0.5, height*0.25); else text("形はどれ?", width*0.5, height*0.25); } //回答選択肢エリア void display2() { //範囲 fill(0, 20); rect(width/4, height*5/8, width/2, height/4); rect(width*3/4, height*5/8, width/2, height/4); rect(width/4, height*7/8, width/2, height/4); rect(width*3/4, height*7/8, width/2, height/4); //選択肢の文章 textAlign(CENTER, CENTER); textSize(king); fill(choiceColor[0]); text(choiceText[0], width*0.25, height*0.625); fill(choiceColor[1]); text(choiceText[1], width*0.75, height*0.625); fill(choiceColor[2]); text(choiceText[2], width*0.25, height*0.875); fill(choiceColor[3]); text(choiceText[3], width*0.75, height*0.875); } boolean judgement(int a) { //問題の種類によって分岐。正解の場合trueを返す。 //同じ色を答える問題 if (type == 0 && answerColor == choiceColor[a]) return true; //同じ形を答える問題 if (type == 1 && answerText == choiceText[a]) return true; //違う色を答える問題 if (type == 2 && answerColor != choiceColor[a]) return true; //違う形を答える問題 if (type == 3 && answerText != choiceText[a]) return true; return false; //不正解の場合falseを返す。 } //エフェクト表示用関数 void effect() { textSize(big); if (isAnswer) { //正解の場合 noFill(); stroke(255, 0, 0); strokeWeight(20); ellipse(width/2, height/2, width*0.8, width*0.8); } else {//不正解の場合 stroke(0, 0, 255); strokeWeight(20); line(width*0.2, height*0.2, width*0.8, height*0.8); line(width*0.8, height*0.2, width*0.2, height*0.8); } //エフェクトの表示時間調整 effectTime++; if (effectTime >= timeSpeed) { if (norma == 0) menu++; else quiz = new Quiz(); } } void mouseReleased() { //問題に回答すると選んだ選択肢の番号を記録し、エフェクトを表示させる。 if (mouseY >= height/2 && isEffect == false) { if (mouseX < width/2 && mouseY < height*0.75) isAnswer = judgement(0); else if (mouseX > width/2 && mouseY < height*0.75) isAnswer = judgement(1); else if (mouseX < width/2 && mouseY > height*0.75) isAnswer = judgement(2); else if (mouseX > width/2 && mouseY > height*0.75) isAnswer = judgement(3); if (isAnswer) norma --; isEffect = true; } if (mouseY <= 40 && mouseX <= 40) menu = 4; } }