I've written a very simple program that has multiple threads that is supposed to print out to the terminal. One thread prints out an array of 10 stars, where the other thread is supposed to run in the background waiting to detect any keyboard presses. What I cannot solve is the output in the terminal does not print out properly if the second thread is running. If the second thread is stopped then the output is what is desired.
Full code (as requested):
#ASCII Frogger
from random import randint
import time
import sys, termios, tty, os
import threading
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def resetPositions():
pos = ["*", "*", "*", "*", "*", "*", "*", "*", "*", "*"]
return pos
def threadKeyPressDetection():
button_delay = 0.2
while True:
char = getch();
if (char == "p"):
#print("Stop!")
exit(0)
def threadGame():
positions = ["*", "*", "*", "*", "*", "*", "*", "*", "*", "*"]
startingPos = randint(0, 9)
frogPosition = startingPos
i = 0
ii = 0
while i < 10:
# if player press left go down in grid if space
# if player press right go up in grid if space
while ii < 10:
if i < 5:
positions[4] = (5 - i)
print positions[ii],
ii += 1
ii = 0
if i == 5:
print "\n\n\n GO "
print "\n"
positions = resetPositions()
if i > 5:
positions[frogPosition] = '!'
i += 1
time.sleep(1)
try:
t1 = threading.Thread(target=threadKeyPressDetection)
t2 = threading.Thread(target=threadGame)
t1.start()
t2.start()
t1.join()
t2.join()
except:
print("Error occured - unable to start thread")
Desired output:
* * * * 3 * * * * *
* * * * 2 * * * * *
* * * * 1 * * * * *
GO
* * * * * * * * * *
* * * * * * ! * * *
Current output:
* * * * 5 * * * * *
* * * * 4 * * * * *
* * * * 3 * * * * *
* * * * 2 * * * * *
* * * * 1 * * * * *
* * * * * * * * * *
GO