回答編集履歴

4

不要なグローバル宣言の排除

2022/12/23 12:33

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -82,13 +82,8 @@
82
82
  key = ""
83
83
 
84
84
 
85
- cx = 400
86
- cy = 300
87
-
88
-
89
-
90
- def func1():
85
+ def func1(cx=400, cy=300):
91
- global cx, cy
86
+ # NOTE: ジェネレーター内なので、cx, cy はローカル変数で管理できるようになります
92
87
  while True:
93
88
  if key == "w":
94
89
  cy = cy - 1

3

スペル修正

2022/12/23 11:05

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -131,11 +131,11 @@
131
131
  def blinker(canvas):
132
132
  # "hidden" と "normal" を繰り返す無限リスト
133
133
  from itertools import cycle
134
- states = cycle(["hidden", "normal"])
134
+ status = cycle(["hidden", "normal"])
135
135
 
136
136
  while True:
137
137
  # "blink" タグの付いた要素の表示非表示を 300ms 毎に切り替え
138
- canvas.itemconfigure("blink", state=next(states))
138
+ canvas.itemconfigure("blink", state=next(status))
139
139
  yield 0.3
140
140
 
141
141
 

2

コード例追記

2022/12/23 11:01

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -48,3 +48,111 @@
48
48
  点滅させるために毎回 delete/create していますが、
49
49
  特定のタグのみを表示非表示状態を切り替えるようにすることも可能です。
50
50
  タグは複数指定可能です。
51
+
52
+ ```python
53
+ import tkinter
54
+ # import asyncio
55
+ # import time
56
+ import random
57
+
58
+ try:
59
+ # pip install gentimer
60
+ from gentimer.timer_tk import gen_timer
61
+ except ImportError:
62
+ # 代替実装、必要な部分のみ抜粋
63
+ def gen_timer(root, gen):
64
+ def _tick():
65
+ interval = next(gen, None)
66
+ if interval is not None:
67
+ root.after(max(1, interval*1000), _tick)
68
+ root.after_idle(_tick)
69
+
70
+
71
+
72
+ key = ""
73
+
74
+
75
+ def key_down(e):
76
+ global key
77
+ key = e.keysym
78
+
79
+
80
+ def key_up(e):
81
+ global key
82
+ key = ""
83
+
84
+
85
+ cx = 400
86
+ cy = 300
87
+
88
+
89
+
90
+ def func1():
91
+ global cx, cy
92
+ while True:
93
+ if key == "w":
94
+ cy = cy - 1
95
+ if key == "s":
96
+ cy = cy + 1
97
+ if key == "a":
98
+ cx = cx - 1
99
+ if key == "d":
100
+ cx = cx + 1
101
+ canvas.coords("SHIP", cx, cy)
102
+ yield 0.01
103
+
104
+
105
+ def draw_1(x=200, y=400):
106
+ canvas.create_text(x, y, text="acc", fill="yellow", tag=("acc", "blink"))
107
+
108
+
109
+ def draw_2(x=200, y=400):
110
+ canvas.create_text(x, y, text="敵1", fill="green", tag=["enemy1", "blink"])
111
+
112
+
113
+ def draw_3(x=500, y=300):
114
+ canvas.create_text(x, y, text="敵2", fill="red", tag="enemy2 blink")
115
+
116
+
117
+ def accident():
118
+ draw_1()
119
+ draw_2()
120
+ yield 3
121
+ canvas.delete("enemy1")
122
+
123
+
124
+ def accident2():
125
+ draw_1()
126
+ draw_3()
127
+ yield 3
128
+ canvas.delete("enemy2")
129
+
130
+
131
+ def blinker(canvas):
132
+ # "hidden" と "normal" を繰り返す無限リスト
133
+ from itertools import cycle
134
+ states = cycle(["hidden", "normal"])
135
+
136
+ while True:
137
+ # "blink" タグの付いた要素の表示非表示を 300ms 毎に切り替え
138
+ canvas.itemconfigure("blink", state=next(states))
139
+ yield 0.3
140
+
141
+
142
+ root = tkinter.Tk()
143
+ root.title("避け")
144
+ root.bind("<KeyPress>", key_down)
145
+ root.bind("<KeyRelease>", key_up)
146
+ canvas = tkinter.Canvas(width=800, height=600, bg="blue")
147
+ canvas.pack()
148
+
149
+ canvas.create_text(cx, cy, text="船", fill="black", tag="SHIP")
150
+
151
+ gen_timer(root, accident())
152
+ gen_timer(root, accident2())
153
+ gen_timer(root, func1()) # 船の移動
154
+ gen_timer(root, blinker(canvas)) # 点滅アニメーション用
155
+
156
+ root.mainloop()
157
+
158
+ ```

1

追記

2022/12/23 10:49

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -40,3 +40,11 @@
40
40
 
41
41
  使い方は、sleep したいところを yield で遅延秒数を返し、
42
42
  タイマーとして実行するだけです。
43
+
44
+
45
+ -----
46
+ 追記:
47
+ もう少し掘り下げると、
48
+ 点滅させるために毎回 delete/create していますが、
49
+ 特定のタグのみを表示非表示状態を切り替えるようにすることも可能です。
50
+ タグは複数指定可能です。