Learn to Code in 30 Days: with MiniScript and Mini Micro by Strout Joe
Author:Strout, Joe [Strout, Joe]
Language: eng
Format: epub
Publisher: MiniScript Press
Published: 2021-09-23T16:00:00+00:00
* * *
1
print "Press any key to stop the count."
2
print "See if you can stop on exactly 50! Get ready..."
3
wait
4
print "GO!"
5
for i in range(1, 100)
6
if key.available then break
7
print i
8
wait 0.05
9
end for
10
key.clear
11
diff = abs(i - 50)
12
print "You stopped " + diff + " away from 50."
13
if diff < 5 then print "Amazing!"
* * *
Reaction test.
Save this program as âreactionTestâ, then try it a few times. Notice how weâre checking key.available within the loop, and when one is available (i.e. the user has pressed a key), we break out of the loop. Then we use key.clear to clear out the keyboard buffer. In fact, try taking that out, and see what happens!
The second way to detect key presses without waiting for them is to use key.pressed . This function is quite different from the other key functions weâve looked at so far. The key.available , key.get , and key.clear functions all deal with the keyboard buffer , the set of printable characters (plus a few editing characters as discovered before) that the user has typed. These can include modified keys like capital letters, $, or Ï, but they donât include the modifier keys themselves. Also, when you hold a printable key down, it will be entered into the keyboard buffer multiple times, following the same key-repeat behavior you see when typing. Basically, despite being in the âkeyâ module, what these functions actually report is keyboard input , one character at a time.
The key.pressed function is different. It reports on the status (pressed or not pressed) of individual, physical keys on the keyboard. That includes the left and right shift keys, the alt keys, the function keys, and so on. It also includes the printable keys like letters and numbers, but not the shifted version of those; to key.pressed , shift-2 is just two keys being pressed, not the at-sign that key.get would report.
To illustrate, hereâs a program that counts up only while the left Shift key is held.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6797)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6524)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6391)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4315)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3982)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3771)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3714)
The Ultimate iOS Interview Playbook by Avi Tsadok(3688)
