回答編集履歴

2

見直しキャンペーン中

2023/07/30 08:40

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -96,3 +96,4 @@
96
96
  global current
97
97
  current = (current + 1) % 9
98
98
  ```
99
+

1

別バリエーション

2022/05/28 21:59

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -37,3 +37,62 @@
37
37
 
38
38
  配列のシフトがPythonicな感じです^^
39
39
  [Python リストのインデックスをずらす!! - Qiita](https://qiita.com/Saijo_Tsubasa/items/dcbe64a1ce7550353672)
40
+
41
+ ---
42
+
43
+ 色の配列にしたのは、色を増やしたりが楽だろうと思ってのことです。
44
+
45
+ 要件を満たすだけなら、グローバル変数はintひとつで十分です。
46
+ ```Python
47
+ current = 0
48
+
49
+ def setup():
50
+ size(600, 600)
51
+
52
+ def draw():
53
+ background(128)
54
+ x, y = 100, 100
55
+ for i in range(0, 3):
56
+ fill(0 if 0 + i * 3 == current else 255)
57
+ ellipse(x, y + 150 * i, 50, 50)
58
+
59
+ fill(0 if 1 + i * 3 == current else 255)
60
+ rect(x + 150, y - 25 + 150 * i, 50, 50)
61
+
62
+ fill(0 if 2 + i * 3 == current else 255)
63
+ triangle(x + 350, y - 25 + 150 * i, x + 325, y + 25 + 150 * i, x + 375, y + 25 + 150 * i)
64
+
65
+ def mouseClicked():
66
+ global current
67
+ current = (current + 1) % 9
68
+ ```
69
+
70
+ ---
71
+
72
+ ループを9回まわすほうが、`fill`に関してはわかりやすいかもしれません。
73
+ ```Python
74
+ current = 0
75
+
76
+ def setup():
77
+ size(600, 600)
78
+
79
+ def draw():
80
+ background(128)
81
+ translate(100, 100)
82
+ # for i, (col, row) in enumerate([(i % 3, i / 3) for i in range(9)]):
83
+ for i in range(9):
84
+ fill(0 if i == current else 255)
85
+ col = i % 3
86
+ row = i / 3
87
+ y = 150 * row
88
+ if col == 0:
89
+ ellipse(0, y, 50, 50)
90
+ elif col == 1:
91
+ rect(150, y - 25, 50, 50)
92
+ else:
93
+ triangle(350, y - 25, 325, y + 25, 375, y + 25)
94
+
95
+ def mouseClicked():
96
+ global current
97
+ current = (current + 1) % 9
98
+ ```