matoに計算式(例えば、50+52)のような式をを表示させたい
画像に重ねるのか上だか下だか知りませんが、↓のようにするだけではありませんか?(多少の位置合わせは必要でしょうが)
Processing
1image(img, imgX, 150, 50, 50);
2text("50+52", imgX, 150);
ユーザーがパネルで計算結果を正確に打つ
「計算結果」をボタンで入力するんですよね?(電卓を作ってるわけじゃないんですよね?)
なんで演算キー(+とか)があるんでしょうか?(逆にクリアキーなんかはないんですかね?)
破壊されて、スコアが獲得できるようにしたいです.
「実現したいこと」の提示はありがたいですが、やってみたことや分からないことの情報がありません。これでは単に作業依頼です。
ヘルプ | 質問する時のヒント
ヘルプ | 推奨していない質問
例えばスコアの変数やスコアを表示しようとしている様子が、提示コードからは全くうかがえません(実際はあるのかもしれませんが、それが分からないのでは無いのと同じです)
要は脳トレのようなものを作ってるわけですね。
「問題・答え・入力」をすべて文字列(String
)で持ってしまうのが楽そうな気がします。
回答は面倒だったので足し算のみですが、四則演算に拡張するのも難しくはないでしょう。
無駄に「破壊」感を出してみましたw
Simple Particle System / Examples / Processing.org
Processing
1final float x = 20;
2final float y = 300;
3final float w = 800 - 40;
4final float h = 400;
5final String[] label = {
6 "7", "8", "9",
7 "4", "5", "6",
8 "1", "2", "3",
9 "0", "", "C" };
10final ParticleSystem ps = new ParticleSystem();
11
12float imgX = 1000;
13float imgVX = 2;
14String question;
15String answer;
16String input;
17int score;
18
19
20void setup() {
21 size(800, 800);
22 textSize(48);
23}
24
25void draw() {
26 background(127);
27 showPanel();
28 mato();
29
30 textAlign(LEFT, BASELINE);
31 text("SCORE: " + score, 10, 50);
32 //textAlign(LEFT, BOTTOM);
33 //text(answer, 10, 250);
34 textAlign(CENTER, BOTTOM);
35 text(input, 0, 0, width, 250);
36
37 ps.run();
38}
39
40void mousePressed() {
41 if (mouseX < x || mouseX > x + w) return;
42 if (mouseY < y || mouseY > y + h) return;
43
44 int i = int(3 * (mouseX - x) / w);
45 int j = int(4 * (mouseY - y) / h);
46 String s = label[getID(i, j)];
47
48 if (s.equals("C")) { // クリア
49 input = "";
50 return;
51 }
52
53 input += s;
54 if (input.equals(answer)) { // 正解
55 score++;
56 ps.addParticle(new PVector(imgX, 150), question);
57 imgX = 1000; // 範囲外に飛ばすことで、matoでの再設定を誘発させる
58 }
59}
60
61void showPanel() {
62 rect(x, y, w, h);
63
64 for (int i = 0; i < label.length; i++) {
65 showLabel(i);
66 }
67}
68
69void showLabel(int i) {
70 float bw = w / 3;
71 float bh = h / 4;
72 float bx = x + (i % 3) * bw;
73 float by = y + (i / 3) * bh;
74
75 fill(200);
76 rect(bx, by, bw, bh);
77 fill(0);
78 textAlign(CENTER, CENTER);
79 text(label[i], bx, by, bw, bh);
80}
81
82void mato() {
83 if (width < imgX) {
84 imgX = 0;
85 int a = int(random(1, 100));
86 int b = int(random(1, 100));
87 question = a + "+" + b; // 問題を文字列化
88 answer = str(a + b); // 答えも文字列化
89 input = "";
90 }
91
92 textAlign(LEFT, BASELINE);
93 text(question, imgX, 150);
94 imgX += imgVX;
95}
96
97int getID(int i, int j) {
98 return j * 3 + i;
99}
100
101
102// [Simple Particle System / Examples / Processing.org](https://processing.org/examples/simpleparticlesystem.html)
103// をベースに小改造(文字列を一文字ずつばらばらに四散させる)
104class ParticleSystem {
105 ArrayList<Particle> particles = new ArrayList<Particle>();
106 void addParticle(PVector position, String str) {
107 for (char c : str.toCharArray()) {
108 particles.add(new Particle(position, c));
109 }
110 }
111 void run() {
112 for (int i = particles.size() - 1; i >= 0; i--) {
113 Particle p = particles.get(i);
114 p.run();
115 if (p.isDead()) {
116 particles.remove(i);
117 }
118 }
119 }
120}
121class Particle {
122 PVector position;
123 PVector velocity = PVector.random3D().mult(10);
124 float lifespan = 255;
125 char c;
126 Particle(PVector l, char c) {
127 position = l.copy();
128 this.c = c;
129 }
130 void run() {
131 update();
132 display();
133 }
134 void update() {
135 position.add(velocity);
136 lifespan -= 2;
137 }
138 void display() {
139 push();
140 translate(position.x, position.y);
141 rotate(radians(position.z));
142 fill(0, lifespan);
143 textAlign(CENTER, CENTER);
144 text(c, 0, 0);
145 pop();
146 }
147 boolean isDead() {
148 return lifespan < 0.0;
149 }
150}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/25 08:42
2022/06/25 08:42