teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2020/02/15 09:10

投稿

k0gane
k0gane

スコア10

title CHANGED
File without changes
body CHANGED
@@ -146,4 +146,143 @@
146
146
 
147
147
  ```
148
148
 
149
- ligntning関数を用いてボタンを光らせているかの判定をしたく、ボタンを押してから0.3秒間はGUIの該当部分を赤くするようにしたいのですが、現状では図形が上の画像のまま変色しません。どこを改善したらよいかご教授お願い致します。
149
+ ligntning関数を用いてボタンを光らせているかの判定をしたく、ボタンを押してから0.3秒間はGUIの該当部分を赤くするようにしたいのですが、現状では図形が上の画像のまま変色しません。どこを改善したらよいかご教授お願い致します。
150
+
151
+
152
+ # 追記
153
+
154
+ shiracamusさんの記述を参考にしたのですが、main関数にて記述したpygameの部分が認識されません。ターミナルにもprintで来ていない状態です。
155
+
156
+ ```python
157
+ import os
158
+ import sys
159
+ import time
160
+ import threading
161
+ import pygame
162
+ from pygame.locals import *
163
+ os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
164
+ import tkinter as tk
165
+ from tkinter import font, IntVar
166
+
167
+
168
+ class Switch:
169
+
170
+ def __init__(self, canvas, shape, x1, y1, x2, y2, color):
171
+ self.canvas = canvas
172
+ self.on = shape(x1, y1, x2, y2, fill='red')
173
+ self.off = shape(x1, y1, x2, y2, fill=color)
174
+ self.count = IntVar()
175
+
176
+ def push(self):
177
+ self.count.set(self.count.get() + 1)
178
+ self.light_on()
179
+
180
+ def light_on(self):
181
+ self.canvas.tag_raise(self.on, self.off)
182
+ self.canvas.after(300, self.light_off)
183
+
184
+ def light_off(self):
185
+ self.canvas.tag_raise(self.off, self.on)
186
+
187
+
188
+ class Key(Switch):
189
+
190
+ def __init__(self, canvas, x, y, width=70, height=135, color='white'):
191
+ super().__init__(canvas, canvas.create_rectangle,
192
+ x, y, x + width, y + height, color)
193
+
194
+
195
+ class Disk(Switch):
196
+
197
+ def __init__(self, canvas, x, y, size, color='white'):
198
+ super().__init__(canvas, canvas.create_oval,
199
+ x, y, x + size, y + size, color)
200
+
201
+
202
+ class Game:
203
+
204
+ def __init__(self, title):
205
+ self.title = title
206
+
207
+ def run(self):
208
+ self.running = True
209
+ self.window = tk.Tk()
210
+ self.window.title(self.title)
211
+ self.window.geometry('640x360')
212
+
213
+ self.switches = self._make_switches()
214
+
215
+ self.window.after(1000, self._check_to_quit)
216
+ self.window.mainloop()
217
+ # need to delete variables that reference tkinter objects in the thread
218
+ del self.switches
219
+ del self.window
220
+
221
+ def _make_switches(self):
222
+ canvas = tk.Canvas(self.window, width=640, height=360)
223
+ canvas.place(x=0, y=0)
224
+
225
+ white_keys = [Key(canvas, x, y=195)
226
+ for x in range(280, 280 + 90 * 4, 90)]
227
+ black_keys = [Key(canvas, x, y=30, color='black')
228
+ for x in range(325, 325 + 90 * 3, 90)]
229
+ disk = Disk(canvas, x=35, y=65, size=220)
230
+
231
+ font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
232
+ for i, key in enumerate(white_keys):
233
+ label = tk.Label(self.window, font=font1, textvariable=key.count)
234
+ label.place(x=310 + 90 * i, y=333)
235
+ for i, key in enumerate(black_keys):
236
+ label = tk.Label(self.window, font=font1, textvariable=key.count)
237
+ label.place(x=355 + 90 * i, y=5)
238
+ label = tk.Label(self.window, font=font1, textvariable=disk.count)
239
+ label.place(x=205, y=300)
240
+
241
+ return dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
242
+ (*white_keys, *black_keys, disk)))
243
+
244
+ def push_switch(self, index):
245
+ self.switches[index].push()
246
+
247
+ def _check_to_quit(self):
248
+ if self.running:
249
+ self.window.after(1000, self._check_to_quit)
250
+ else:
251
+ self.window.destroy()
252
+
253
+ def quit(self):
254
+ self.running = False
255
+
256
+ def main():
257
+ game = Game('Contoller input')
258
+ thread = threading.Thread(target=game.run)
259
+ thread.start()
260
+ pygame.joystick.init()
261
+ pygame.init()
262
+
263
+ try:
264
+ j = pygame.joystick.Joystick(0) # create a joystick instance
265
+ j.init() # init instance
266
+ print(j.get_name())
267
+ except pygame.error:
268
+ print('Joystickが見つかりませんでした。')
269
+ game.quit()
270
+
271
+ for e in pygame.event.get():
272
+ print(e)
273
+ if(e.type == pygame.locals.JOYBUTTONDOWN or e.type == pygame.locals.JOYAXISMOTION or e.type == QUIT):
274
+ if(e.type == QUIT):
275
+ print("quit")
276
+ game.quit()
277
+ thread.join()
278
+ if(e.type == pygame.locals.JOYBUTTONDOWN):
279
+ print("button " + str(e.button))
280
+ if(e.button < 7):
281
+ game.push_switch(e.button)
282
+ elif(e.type == pygame.locals.JOYAXISMOTION):
283
+ print("turntable spin")
284
+ game.push_switch(7)
285
+
286
+ if __name__ == '__main__':
287
+ main()
288
+ ```