回答編集履歴

1

見直しキャンペーン中

2023/07/22 08:51

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,627 +1,313 @@
1
1
  どちらも残したいので回答を分けさせていただきます。
2
2
 
3
-
4
-
5
3
  # とにかく動けばいい場合
6
-
7
4
  ```Processing
8
-
9
5
  import ddf.minim.*;
10
6
 
11
-
12
-
13
7
  Minim minim;
14
-
15
8
  AudioPlayer player;
16
-
17
9
  PFont font;
18
10
 
19
-
20
-
21
11
  Opening opening;
22
-
23
12
  Question question;
24
-
25
13
  Correct correct;
26
-
27
14
  Incorrect incorrect;
28
15
 
29
-
30
-
31
16
  int x1 = 30;
32
-
33
17
  int y1 = 500;
34
-
35
18
  int x2 = 250;
36
-
37
19
  int y2 = 500;
38
-
39
20
  int w = 170;
40
-
41
21
  int h = 170;
42
22
 
43
-
44
-
45
23
  int mode; // 0:タイトル 1:問題 2:判定
46
24
 
47
-
48
-
49
25
  void setup() {
50
-
51
26
  size(460, 800);
52
-
53
27
  //minim = new Minim(this);
54
-
55
28
  //player = minim.loadFile("QuizKnockのメインテーマ.mp3");
56
-
57
29
  //player.play();
58
-
59
30
  font = createFont("游明朝 Demibold", 24);
60
-
61
31
  textFont(font);
62
32
 
63
-
64
-
65
33
  opening = new Opening();
66
-
67
34
  correct = new Correct();
68
-
69
35
  incorrect = new Incorrect();
70
-
71
36
  question = new Question();
72
-
73
37
  opening.display();
74
-
75
- }
38
+ }
76
-
77
-
78
39
 
79
40
  void draw() {
80
-
81
- }
41
+ }
82
-
83
-
84
42
 
85
43
  void mouseClicked() {
86
-
87
44
  switch (mode) {
88
-
89
45
  case 0:
90
-
91
46
  mode = 1;
92
-
93
47
  question.display();
94
-
95
48
  break;
96
-
97
49
  case 1:
98
-
99
50
  if (x1 < mouseX && mouseX < x1 + w && y1 < mouseY && mouseY < y1 + h) {
100
-
101
51
  mode = 2;
102
-
103
52
  correct.display();
104
-
105
53
  }
106
-
107
54
  if (x2 < mouseX && mouseX < x2 + w && y2 < mouseY && mouseY < y2 + h) {
108
-
109
55
  mode = 2;
110
-
111
56
  incorrect.display();
112
-
113
57
  }
114
-
115
58
  break;
116
-
117
59
  case 2:
118
-
119
60
  mode = 0;
120
-
121
61
  opening.display();
122
-
123
62
  break;
124
-
125
- }
63
+ }
126
-
127
- }
64
+ }
128
-
129
-
130
-
131
65
 
132
66
 
133
67
  class Opening {
134
-
135
- void display() {
68
+ void display() {
136
-
137
69
  background(0, 255, 255);
138
-
139
- textAlign(CENTER);
70
+ textAlign(CENTER);
140
-
141
- fill(0);
71
+ fill(0);
142
-
143
- textSize(25);
72
+ textSize(25);
144
-
145
73
  text("ルイザ・グロス・ホロウィッツ賞", width / 2, 250);
146
-
147
74
  text("日本人受賞者を覚えよう!", width / 2, 285);
148
-
149
75
  textSize(18);
150
-
151
76
  text("ルイザ・グロス・ホロウィッツ賞", width / 2, 500);
152
-
153
77
  text("日本人受賞者を全員答えられたらクリアだよ", width / 2, 530);
154
-
155
- }
78
+ }
156
-
157
- }
79
+ }
158
-
159
-
160
80
 
161
81
  class Question {
162
-
163
- void display() {
82
+ void display() {
164
-
165
83
  background(245, 100, 170);
166
-
167
- textAlign(CENTER);
84
+ textAlign(CENTER);
168
-
169
- fill(0);
85
+ fill(0);
170
-
171
- textSize(25);
86
+ textSize(25);
172
-
173
87
  text("生物学や生化学の研究者に贈られる", width / 2, 250);
174
-
175
88
  text("ルイザ・グロス・ホロウィッツ賞を", width / 2, 285);
176
-
177
89
  text("現在、日本人で唯一受賞したのは誰?", width / 2, 320);
178
-
179
90
  fill(255);
180
-
181
91
  noStroke();
182
-
183
92
  rect(x1, y1, w, h);
184
-
185
- fill(0);
93
+ fill(0);
186
-
187
- textAlign(CENTER);
94
+ textAlign(CENTER);
188
-
189
95
  textSize(30);
190
-
191
96
  text("利根川進", 115, 600);
192
-
193
97
  fill(255);
194
-
195
98
  rect(x2, y2, w, h);
196
-
197
- fill(0);
99
+ fill(0);
198
-
199
100
  text("木村資生", 335, 600);
200
-
201
- }
101
+ }
202
-
203
- }
102
+ }
204
-
205
-
206
103
 
207
104
  class Correct {
208
-
209
- void display() {
105
+ void display() {
210
-
211
106
  background(255, 255, 0);
212
-
213
- textAlign(CENTER);
107
+ textAlign(CENTER);
214
-
215
108
  textSize(80);
216
-
217
109
  text("クリア", width / 2, 300);
218
-
219
- textSize(25);
110
+ textSize(25);
220
-
221
111
  text("1不可説不可説転点", width / 2, 500);
222
-
223
112
  text("獲得", width / 2, 535);
224
-
225
- }
113
+ }
226
-
227
- }
114
+ }
228
-
229
-
230
115
 
231
116
  class Incorrect {
232
-
233
- void display() {
117
+ void display() {
234
-
235
118
  background(255, 255, 0);
236
-
237
- textAlign(CENTER);
119
+ textAlign(CENTER);
238
-
239
120
  textSize(80);
240
-
241
121
  text("残念", width / 2, 300);
242
-
243
122
  textSize(20);
244
-
245
123
  text("あなたはルイザ・グロス・ホロウィッツ賞の", width / 2, 500);
246
-
247
124
  text("日本人受賞者を1人も覚えていません", width / 2, 535);
248
-
249
- }
125
+ }
250
-
251
- }
126
+ }
252
-
253
127
  ```
254
128
 
255
-
256
-
257
129
  しかしこれでは「クラスを定義し,オブジェクトを生成」はしましたが、とても「オブジェクトを活用」しているとは言えません。
258
-
259
130
  クラスの`display()`を`openingDisplay()`等で関数にしたのと大差ありません。
260
131
 
261
132
 
262
-
263
-
264
-
265
133
  ###### オブジェクトを活用した場合
266
-
267
134
  ```Processing
268
-
269
135
  import ddf.minim.*;
270
136
 
271
-
272
-
273
137
  Minim minim;
274
-
275
138
  AudioPlayer player;
276
-
277
139
  PFont font;
278
140
 
279
-
280
-
281
141
  Scene current; // 今の画像?を保持する変数
282
142
 
283
-
284
-
285
143
  void setup() {
286
-
287
144
  size(460, 800);
288
-
289
145
  //minim = new Minim(this);
290
-
291
146
  //player = minim.loadFile("QuizKnockのメインテーマ.mp3");
292
-
293
147
  //player.play();
294
-
295
148
  font = createFont("游明朝 Demibold", 24);
296
-
297
149
  textFont(font);
298
150
 
299
-
300
-
301
151
  Opening opening = new Opening(); // *1でsetNextしたいのでOpening型変数に入れる
302
-
303
152
  //Scene opening = new Opening(); // としたいところだがScene型にはsetNextがない
304
-
305
153
  Scene correct = new Correct(opening); // CorrectはScene型に入れられる
306
-
307
154
  Scene incorrect = new Incorrect(opening);
308
-
309
155
  Scene question = new Question(correct, incorrect);
310
156
 
311
-
312
-
313
157
  opening.setNext(question); // *1
314
-
315
158
  current = opening; // 初めの画像?の設定
316
-
317
- }
159
+ }
318
-
319
-
320
160
 
321
161
  void draw() {
322
-
323
162
  current.display(); // 今の画像?を表示
324
-
325
- }
163
+ }
326
-
327
-
328
164
 
329
165
  void mouseClicked() {
330
-
331
166
  Scene next = current.getNext(mouseX, mouseY); // 今の画像?に次の画像?を要求する
332
-
333
167
  current = next; // 今の画像?変数の中身を次の画像?に入れ替える
334
-
335
- }
168
+ }
336
-
337
-
338
-
339
169
 
340
170
 
341
171
  interface Scene { // それぞれの画像?が共通して持っているべき機能
342
-
343
172
  void display(); // きっと何か表示するのかな?
344
-
345
173
  Scene getNext(int x, int y); // 座標を引数にSceneをかえす ふーむ?
346
-
347
- }
174
+ }
348
-
349
175
  // インターフェースからは実際の動作はわかりません クラスで「実装」して初めて意味を持ちます
350
-
351
176
  // 長くなるので各自で調べてください^^;
352
177
 
353
178
 
354
-
355
-
356
-
357
179
  class Opening implements Scene { // Openingは「Sceneのメソッドを必ず持っています」という約束(実装)
358
-
359
180
  Scene next; // 次の画像?
360
181
 
361
-
362
-
363
182
  void setNext(Scene next) { // 次の画像?設定(他と同じようにコンストラクタで渡したいが、循環してるのでどれかはこうするよりない)
364
-
365
183
  this.next = next;
366
-
367
- }
184
+ }
368
-
369
-
370
-
185
+
371
- void display() {
186
+ void display() {
372
-
373
187
  background(0, 255, 255);
374
-
375
- textAlign(CENTER);
188
+ textAlign(CENTER);
376
-
377
- fill(0);
189
+ fill(0);
378
-
379
- textSize(25);
190
+ textSize(25);
380
-
381
191
  text("ルイザ・グロス・ホロウィッツ賞", width / 2, 250);
382
-
383
192
  text("日本人受賞者を覚えよう!", width / 2, 285);
384
-
385
193
  textSize(18);
386
-
387
194
  text("ルイザ・グロス・ホロウィッツ賞", width / 2, 500);
388
-
389
195
  text("日本人受賞者を全員答えられたらクリアだよ", width / 2, 530);
390
-
391
- }
196
+ }
392
-
393
-
394
197
 
395
198
  Scene getNext(int x, int y) {
396
-
397
199
  return next; // 次の画像?(question)を無条件でかえす
398
-
399
- }
200
+ }
400
-
401
- }
201
+ }
402
-
403
-
404
202
 
405
203
  class Question implements Scene {
406
-
407
204
  final Scene next1; // finalをつけると変更不可になる(初期化も強制される)
408
-
409
205
  final Scene next2;
410
-
411
206
  final int x1 = 30; // この辺の変数はほかで使わないのでクラスのメンバーに
412
-
413
207
  final int y1 = 500;
414
-
415
208
  final int x2 = 250;
416
-
417
209
  final int y2 = 500;
418
-
419
210
  final int w = 170;
420
-
421
211
  final int h = 170;
422
212
 
423
-
424
-
425
213
  Question(Scene next1, Scene next2) { // 分岐があるので2つもらう
426
-
427
214
  this.next1 = next1;
428
-
429
215
  this.next2 = next2;
430
-
431
- }
216
+ }
432
-
433
-
434
-
217
+
435
- void display() {
218
+ void display() {
436
-
437
219
  background(245, 100, 170);
438
-
439
- textAlign(CENTER);
220
+ textAlign(CENTER);
440
-
441
- fill(0);
221
+ fill(0);
442
-
443
- textSize(25);
222
+ textSize(25);
444
-
445
223
  text("生物学や生化学の研究者に贈られる", width / 2, 250);
446
-
447
224
  text("ルイザ・グロス・ホロウィッツ賞を", width / 2, 285);
448
-
449
225
  text("現在、日本人で唯一受賞したのは誰?", width / 2, 320);
450
-
451
226
  fill(255);
452
-
453
227
  noStroke();
454
-
455
228
  rect(x1, y1, w, h);
456
-
457
- fill(0);
229
+ fill(0);
458
-
459
- textAlign(CENTER);
230
+ textAlign(CENTER);
460
-
461
231
  textSize(30);
462
-
463
232
  text("利根川進", 115, 600);
464
-
465
233
  fill(255);
466
-
467
234
  rect(x2, y2, w, h);
468
-
469
- fill(0);
235
+ fill(0);
470
-
471
236
  text("木村資生", 335, 600);
472
-
473
- }
237
+ }
474
-
475
-
476
238
 
477
239
  Scene getNext(int x, int y) {
478
-
479
240
  if (x1 < x && x < x1 + w && y1 < y && y < y1 + h) {
480
-
481
241
  return next1; // もし左の四角内ならcorrectをかえす
482
-
483
242
  }
484
-
485
243
  if (x2 < x && x < x2 + w && y2 < y && y < y2 + h) {
486
-
487
244
  return next2; // もし右の四角内ならincorrectをかえす
488
-
489
245
  }
490
246
 
491
-
492
-
493
247
  return this; // どちらでもなければ自身(question)をかえす(無駄というか意味のない動作だが、そういう設計にしたので仕方がない^^;
494
-
495
- }
248
+ }
496
-
497
- }
249
+ }
498
-
499
-
500
250
 
501
251
  class Correct implements Scene {
502
-
503
252
  final Scene next;
504
253
 
505
-
506
-
507
254
  Correct(Scene next) {
508
-
509
255
  this.next = next;
510
-
511
- }
256
+ }
512
-
513
-
514
-
257
+
515
- void display() {
258
+ void display() {
516
-
517
259
  background(255, 255, 0);
518
-
519
- textAlign(CENTER);
260
+ textAlign(CENTER);
520
-
521
261
  textSize(80);
522
-
523
262
  text("クリア", width / 2, 300);
524
-
525
- textSize(25);
263
+ textSize(25);
526
-
527
264
  text("1不可説不可説転点", width / 2, 500);
528
-
529
265
  text("獲得", width / 2, 535);
530
-
531
- }
266
+ }
532
-
533
-
534
267
 
535
268
  Scene getNext(int x, int y) {
536
-
537
269
  return next;
538
-
539
- }
270
+ }
540
-
541
- }
271
+ }
542
-
543
-
544
272
 
545
273
  class Incorrect implements Scene {
546
-
547
274
  final Scene next;
548
275
 
549
-
550
-
551
276
  Incorrect(Scene next) {
552
-
553
277
  this.next = next;
554
-
555
- }
278
+ }
556
-
557
-
558
-
279
+
559
- void display() {
280
+ void display() {
560
-
561
281
  background(255, 255, 0);
562
-
563
- textAlign(CENTER);
282
+ textAlign(CENTER);
564
-
565
283
  textSize(80);
566
-
567
284
  text("残念", width / 2, 300);
568
-
569
285
  textSize(20);
570
-
571
286
  text("あなたはルイザ・グロス・ホロウィッツ賞の", width / 2, 500);
572
-
573
287
  text("日本人受賞者を1人も覚えていません", width / 2, 535);
574
-
575
- }
288
+ }
576
-
577
-
578
289
 
579
290
  Scene getNext(int x, int y) {
580
-
581
291
  return next;
582
-
583
- }
292
+ }
584
-
585
- }
293
+ }
586
-
587
294
  ```
588
295
 
589
-
590
-
591
296
  元のテイストをできるだけ残したので不自然なところもありますが、それなりに「オブジェクトを活用」できたかなと思います。
592
-
593
297
  もちろんこれが「答え」というものではありません。人それぞれいろいろな作りが考えられます。
594
-
595
298
  その辺が面白いところでもあり、難しいところでもありますね。
596
299
 
597
-
598
-
599
300
  ---
600
301
 
601
-
602
-
603
302
  乗りかかった船なので回答しましたが、
604
303
 
605
-
606
-
607
304
  > processingのオブジェクトの考え方
608
305
 
609
-
610
-
611
306
  とタイトルにある以上、
612
-
613
307
  * 難しすぎて全くわからない
614
-
615
308
  * 理解するのに時間が欲しい
616
-
617
309
  * 実行できない
618
-
619
310
  * ここの意味がよくわからない
620
-
621
311
  * 参考になった
622
312
 
623
-
624
-
625
313
  等、回答について何かしらのアクションを期待しているのですが、これでは単なる**作業依頼**です。
626
-
627
- もちろん「そういうつもりではなかった。」というのはわかっていますが。