88 — Time: a stopwatch and cooldowns
Games keep needing two things: how long did that take, and you cannot do that again yet. Both are sentences.
Steps
Make
timer.nme:
run it →start the timer wait 3 seconds show elapsedA number close to
3.0appears. Korean is시간 재기 시작해and잰시간 말해줘.elapsedis a value, so it goes wherever a value goes. Store it:
run it →start the timer wait 1 second set spent to elapsed show spentOr put it in a condition:
run it →start the timer if elapsed is greater than 10 show That took too long endReading
elapsedwithout starting the timer is refused while compiling. If you see errorE0226, addstart the timeron an earlier line.A cooldown means "this cannot happen again for a few seconds". Give it a name:
run it →put attack on cooldown for 3 secondsKorean is
공격 쿨타임 3초 걸어.Ask whether it is over:
run it →put attack on cooldown for 3 seconds when attack is on cooldown show Not yet endThe opposite is
when attack is ready. Korean is공격 쿨타임이 남았으면and공격 쿨타임이 끝났으면.Or wait it out:
run it →put attack on cooldown for 2 seconds wait for attack show You can attack nowKorean is
공격 쿨타임 끝날때까지 기다려.
Try it
Put a door on cooldown, wait for it, and show how long it took:
start the timer
put door on cooldown for 2 seconds
wait for door
show The door opened
show elapsed
run it →What you learned
start the timer/시간 재기 시작해starts the stopwatch.elapsed/잰시간is the seconds since it started. It is a value, so it goes anywhere a value goes.put attack on cooldown for 3 seconds/공격 쿨타임 3초 걸어sets a cooldown.when attack is ready/is on cooldownasks about it.wait for attack/공격 쿨타임 끝날때까지 기다려sleeps out what is left.- Cooldowns are named, so several can run at once and each is counted separately.