I couldn't post the script here because it's too long; you can find it here: https://scratch.mit.edu/discuss/post/7791614/
You'll need the PyGame library