反应计时器错误

时间:2017-06-11 15:23:24

标签: python multithreading python-3.x timer

我正在为学校制作拳击计划,你必须在那里击打敌人的击倒,但敌人每隔几秒钟也会进攻一次。它会在攻击时显示一个惊叹号,你必须快速反应才能阻止它。

基本上我想要它做的是产生一个随机时间,然后当我没有足够快地按下按钮时它执行一个命令(在我的情况下是一个攻击)。简而言之,反应计时器。

我使用多线程能够同时运行阻塞功能和敌人攻击功能。将来我想添加一个线程,这样你就可以攻击了。

这是我的反应计时器:

 @RestController
    @RequestMapping("/pays")
    public class PaysController {
        @Autowired
        IManagerPays iManagerPays;

        @RequestMapping(method = RequestMethod.POST)
        public Pays addPays(@RequestBody Pays pays) {
            return iManagerPays.addPays( pays);
        }
    }

    @Service
    public class ManagerPays implements IManagerPays {
        @Autowired
        private RepositoryPays repositorypays;

        @Override
        public Pays addPays(Pays pays) {

            return repositorypays.save(pays);
        }
    }

    @Entity
    @Table(name="ctr_pays")
    public class Pays {
        @Id
          @GeneratedValue
        private Integer idPays;
        private String nomPays;
        @OneToMany(mappedBy="pays")
        private List<Ville> villes;
        public Pays(){

        }
    //getter and setter
    }

运行程序时出现以下错误:

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")

我是所有这一切的新手,只有几周的代码,所以我真的想知道我做错了什么以及如何解决它。

PS:对于项目我不能使用扩展库。

1 个答案:

答案 0 :(得分:0)

我觉得如果你只是使用龟自己的ontimer()事件就可以避免将线程与乌龟混在一起。这是我使用ontimer()来控制操作的代码修改:

from random import randrange
from turtle import Turtle, Screen

vStep = 0.25 # in seconds
FONT = ('Arial', 18, 'normal')
ROUNDS = 3
TIME_PER_ROUND = 5  # in seconds
TIME_BETWEEN_ROUNDS = 2 # in seconds

def StartAttack():
    global vTimer, vReaction

    magicMarker.undo()
    magicMarker.write("!", align='center', font=FONT)

    vTimer = 3  # you now have 3 seconds to react
    vReaction = False
    Reaction()

def Reaction():
    global vTimer

    if vTimer > 0:  # The time in which you have to react

        if vReaction:  # Check if you have pressed the key
            magicMarker.undo()
            magicMarker.write("Reacted in time!", align='center', font=FONT)
        else:
            vTimer -= vStep
            screen.ontimer(Reaction, int(vStep * 1000))  # recheck in vStep seconds
    else:
        magicMarker.undo()
        magicMarker.write("Didn't react in time!", align='center', font=FONT)

def key():
    global vReaction
    vReaction = True

def Block():
    screen.onkey(key, 'space')
    # disable above event handler in after round ends
    screen.ontimer(lambda: screen.onkey(None, 'space'), int(TIME_PER_ROUND * 1000))

def AttackEnemy():
    global vTimer

    vTimer = -1
    # Generate random time after which the enemy is going to hit
    vHitTime = randrange(4, 12) / 4 * 1000
    screen.ontimer(StartAttack, int(vHitTime))

def NextRound():
    global vRounds

    if vRounds > 0:

        magicMarker.undo()
        magicMarker.write(" ", align='center', font=FONT)

        Block()

        AttackEnemy()

        vRounds -= 1

        screen.ontimer(NextRound, int((TIME_PER_ROUND + TIME_BETWEEN_ROUNDS) * 1000))
    else:
        magicMarker.undo()
        magicMarker.write("Match Over!", align='center', font=FONT)

screen = Screen()
screen.listen()

magicMarker = Turtle(visible=False)
magicMarker.write(" ", align='center', font=FONT)

vTimer = -1  # just to make these exist
vReaction = None
vRounds = ROUNDS

NextRound()

screen.mainloop()

单击弹出的窗口使其激活,以便它可以接收键盘事件。

我相信你遇到的问题是Tkinter(乌龟的基础)与线程交互 - 如果你想走这条路,你可以查看Tkinter and Threads