Question:
My code is unbearably slow, I want to speed it up to be fast and smooth. What’s making my code slow and how can I fix it?
import threading, replit, time, random
from getkey import getkey, keys
print('\033[?25l', end="")
keys = ["-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
score = 0
speed = 0.1
def keypress():
global score, speed
while True:
key = getkey()
if key == keys[0]:
score += 1
speed / 1.25
else:
speed * 1.25
score -= 1
t1 = threading.Thread(target=keypress)
t1.start()
while True:
print(score)
for i in keys:
print(i, end="", flush=True)
time.sleep(speed)
keys.pop(0)
keys.append(random.choice(["-", "w"]))
replit.clear()
t1.join()