私は敵をノックアウトしなければならない学校用のボクシングプログラムを作っていますが、敵も数秒ごとに攻撃します。彼は攻撃しようとしているときに感嘆符を表示し、速やかに反応してブロックする必要があります。反応タイマエラー
基本的には、ボタンを十分に速く押さないとコマンドを実行するランダムな時間を生成することです(私の場合は攻撃)。反応タイマ。
私は、ブロック機能と敵の攻撃機能を同時に実行できるようにマルチスレッドを使用しました。将来、私はあなたが攻撃できるようにスレッドを追加したいと思います。
これは私の反応タイマーです:
from random import *
from turtle import *
from time import *
from threading import *
def Reaction():
vTime = 0
while vTime < 3: #The time in which you have to react
vNow = time()
vStep = vNow + 0.25
while time() < vStep:
vJack = True #Useless arguments to fill up the time between each step
vJack = False
vTime += 0.25
print(vTime)
if vReaction == True: #Checking if you have pressed the key
print("Oke")
break
print("af")
def key():
global vReaction
vReaction = True
def Block():
global vTimer
while vTimer < 5:
onkey(key, "space")
listen()
def AttackEnemy():
global vTimer
while vTimer < 5:
vHitTime = randrange(4, 12)/4 * 1000 # Generating the random time after which the enemy is going to hit
ontimer(Reactie, vHitTime)
vTimer += 1
vTimer = 0
vBlock = Thread(target = Block, args =())
vAttack = Thread(target = AttackEnemy, args =())
vBlock.start()
vAttack.start()
vBlock.join()
vAttack.join()
print("End")
私はこれらのエラーを取得するプログラムを実行している:
Exception in thread Thread-2:
Traceback (most recent call last):
File "D:\Programma's\Python\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "D:\Programma's\Python\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "G:\test getal.py", line 35, in AanvalVijand
ontimer(Reactie, vSlagtijd)
File "<string>", line 6, in ontimer
File "D:\Programma's\Python\lib\turtle.py", line 3662, in Screen
Turtle._screen = _Screen()
File "D:\Programma's\Python\lib\turtle.py", line 3690, in __init__
TurtleScreen.__init__(self, _Screen._canvas)
File "D:\Programma's\Python\lib\turtle.py", line 985, in __init__
"blank" : Shape("image", self._blankimage())
File "D:\Programma's\Python\lib\turtle.py", line 470, in _blankimage
img = TK.PhotoImage(width=1, height=1)
File "D:\Programma's\Python\lib\tkinter\__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "D:\Programma's\Python\lib\tkinter\__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
RuntimeError: main thread is not in main loop
私は数週間のためにこれだけ単にコードのすべてに新たなんですだから、私が間違ってやっていることとそれを修正する方法を本当に知りたいです。
PS:プロジェクトでは拡張ライブラリを使用できません。
をチェックアウトする可能性があるscreen.listen()関数はしていませんキーを押すまでプログラムを停止しますか? – Rek3beef21
@ Rek3beef21、no。これは、ウィンドウがデフォルトで有効になっていないキーイベントを受信するだけです。ドキュメンテーションによると、 "TurtleScreenに焦点を当てる(キーイベントを収集するために)。"あなたは一度だけそれをプログラムで呼びます。 – cdlane
あなたのアドバイスや訂正に感謝しています。どうもありがとう!! – Rek3beef21