-
Notifications
You must be signed in to change notification settings - Fork 0
/
disco.py
67 lines (52 loc) · 1.72 KB
/
disco.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# turn your terminal into a disco party
import random
import time
import sys
import os
import signal
def getClearCMD():
# for windows
if os.name == "nt":
return "cls"
# for mac and linux(here, os.name is 'posix')
else:
return "clear"
# set sorrect console clear command
clear = getClearCMD()
# handler for if ctrl+c is pressed to exit loop
def handler(signum, frame):
print("\n")
quit()
# set handler
signal.signal(signal.SIGINT, handler)
# set terminal/disco floor board lenght and width
floorLength = int(os.get_terminal_size()[1]) # terminal lines
floorWidth = int(os.get_terminal_size()[0] / 2) # terminal columns
frame = 0
offsetBool = False
while True:
if frame % 5 == 0:
frame = 0
offsetBool += 1 # flip bool to offset the board from □ to ■
ll = ""
for l in range(floorLength):
wl = ""
for w in range(floorWidth):
if (((w + l + int(offsetBool)) % 2)) == 0:
# random color from "\033[90m" to "\033[97m"
wl += (
f"\033[9{str(random.randint(0, 7))}m" + "~~" + "\033[0m"
# f"\x1b[6;30;4{str(random.randint(0, 7))}m" + "■ " + "\x1b[0m"
) # print even tile ("\033[0m" ends color code)
else:
wl += (
# f"\033[9{str(random.randint(0, 7))}m" + "■ " + "\033[0m"
f"\x1b[6;30;4{str(random.randint(1, 7))}m" + " " + "\x1b[0m"
) # print odd tile
ll += "\n" + wl
sys.stdout.write("\r" + str(ll)) # write board
time.sleep(0.2)
frame += 1
os.system(
clear
) # clear terminal for new frame, "clear" is used for linux and "cls" for windows