Problem
This is a game in which a sequence of 4 characters is displayed to the player, one at a time with a delay between them. Then the player must input that sequence as it was displayed.
Sequences are displayed until input is incorrect.
The characters composing the sequences are chosen at random and they are all lines.
__all__ = []
import random
import time
from colorama import deinit, init
MSG_ASK = 'What was the sequence? '
MSG_CORRECT = ' 33[32mCorrect! 33[0m'
MSG_INCORRECT = ' 33[31mIncorrect 33[0m'
def main():
k = 4
lines = r'|-/'
seconds = 1
init()
while True:
s = ''
sequence = random.choices(lines, k=k)
sequence = ''.join(sequence)
for i in range(k):
if not i:
s = ''
elif i == 1:
s = ' '
else:
s = i * ' '
print(s, sequence[i], end='r')
time.sleep(seconds)
print(f'{s} ')
if input(MSG_ASK) == sequence:
print(MSG_CORRECT)
else:
print(MSG_INCORRECT)
break
deinit()
if __name__ == '__main__':
main()
Solution
Using __all__ = []
is mostly useless in the main program, and should probably be removed.
This is hard to read:
MSG_CORRECT = ' 33[32mCorrect! 33[0m'
MSG_INCORRECT = ' 33[31mIncorrect 33[0m'
This is much easier:
from colorama import Fore
MSG_CORRECT = Fore.GREEN + 'Correct!' + Fore.RESET
MSG_INCORRECT = Fore.RED + 'Incorrect' + Fore.RESET
Note: Style.RESET_ALL
would be an exact translation of