Added monochrome compatibility

This commit is contained in:
xereeto 2024-03-09 18:07:17 +00:00
parent dfecaf222d
commit 99d9b71a89
1 changed files with 62 additions and 30 deletions

View File

@ -1,19 +1,36 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from colored import bg, fg, attr from colored import bg, fg, attr
import blessed
import datetime import datetime
import time import time
from getch import getch from getch import getch
import possible import possible
import os import os
term = blessed.Terminal()
unicode=False unicode=False
eightColor=True
termwhite = term.bold_white
termgreen = term.bold_green
if 'TERM' in os.environ: if 'TERM' in os.environ:
if 'xterm' in os.environ['TERM']: if 'xterm' in os.environ['TERM']:
unicode=True unicode=True
if not 'ansi' in os.environ['TERM']:
eightColor=False
termwhite=term.bright_white
termgreen=term.bright_green
if eightColor:
colors =[ term.white_on_white, term.gray_on_gray,term.yellow_on_yellow,term.green_on_green,term.black_on_lightgray]
cardLetterColors =[ term.black_on_white, term.black_on_gray,term.bold_white_on_yellow,term.bold_white_on_green,term.black_on_lightgray]
colors =[ bg("white")+fg("black"), bg("dark_gray")+fg("white"),bg("light_yellow")+fg("dark_gray"),bg("green")+fg("white"),bg("light_gray")] keycolors = [term.bold_white_on_black, term.black_on_black,term.yellow_on_black,term.bold_green_on_black]
keycolors = [fg("white")+bg("black"), fg("dark_gray")+bg("black"),fg("light_yellow")+bg("black"),fg("green")+bg("black")] reset = term.white_on_black
reset = bg("black")+fg("white") else:
colors =[ term.white_on_white, term.dimgray_on_dimgray,term.bright_yellow_on_bright_yellow,term.reverse_green_on_bright_white,term.lightgray_on_lightgray]
cardLetterColors =[ term.black_on_white, term.bright_white_on_dimgray,term.reverse_bright_yellow_on_dimgray,term.reverse_green_on_bright_white,term.on_lightgray]
keycolors = [termwhite, term.black_on_black,term.bright_yellow_on_black,term.bold_green_on_black]
reset=term.white_on_black + term.normal
class Wordle: class Wordle:
def check(self): def check(self):
@ -59,10 +76,13 @@ class Wordle:
for line in range(3): for line in range(3):
placeCursor(baseCol, baseRow+line) placeCursor(baseCol, baseRow+line)
for k,letter in enumerate(guess[0]): for k,letter in enumerate(guess[0]):
if line!=1: drawing=["_|","_|","_|"," |","_|"];
put(colors[letter]+" "+reset+" ") if line==0:
put(colors[letter]+" "+reset+term.black_on_black+drawing[letter][1]+reset)
elif line==2:
put(colors[letter]+drawing[letter][0]*5+reset+term.black_on_black+drawing[letter][1]+reset)
else: else:
put(colors[letter]+" "+guess[1][k]+" "+reset+" ") put(colors[letter]+" "+cardLetterColors[letter]+guess[1][k]+term.normal+colors[letter]+" "+reset+term.black_on_black(drawing[letter][1])+reset)
def drawStatusLine(self): def drawStatusLine(self):
placeCursor(9,21) placeCursor(9,21)
put(reset+str(self.number)) put(reset+str(self.number))
@ -71,27 +91,28 @@ class Wordle:
t=self.turn t=self.turn
else: else:
t=self.turn+1 t=self.turn+1
put("Turn: "+str(min(t,6))+"/6"+colors[0]) placeCursor(40,21)
put(str(min(t,6))+"/6"+colors[0])
def drawKeyboard(self): def drawKeyboard(self):
def putKey(key):
if key.upper() in self.guessedKeys:
if self.guessedKeys[key.upper()] > 1:
put(keycolors[self.guessedKeys[key.upper()]]+key+' '+term.normal)
else:
put(reset+' ')
else:
put(reset+key+' ')
placeCursor(13,17) placeCursor(13,17)
for key in "qwertyuiop": for key in "qwertyuiop":
if key.upper() in self.guessedKeys: putKey(key)
put(keycolors[self.guessedKeys[key.upper()]]+key+' ')
else:
put(reset+key+' ')
placeCursor(14,18) placeCursor(14,18)
for key in "asdfghjkl": for key in "asdfghjkl":
if key.upper() in self.guessedKeys: putKey(key)
put(keycolors[self.guessedKeys[key.upper()]]+key+' ')
else:
put(reset+key+' ')
placeCursor(16,19) placeCursor(16,19)
for key in "zxcvbnm": for key in "zxcvbnm":
if key.upper() in self.guessedKeys: putKey(key)
put(keycolors[self.guessedKeys[key.upper()]]+key+' ')
else:
put(reset+key+' ')
put(colors[0]) put(colors[0])
@ -111,10 +132,15 @@ class Wordle:
self.drawStatusLine() self.drawStatusLine()
self.drawKeyboard() self.drawKeyboard()
self.errorStatus="" self.errorStatus=""
def updateError(self,e=reset+" "+colors[0]): def updateError(self,e=" +"):
if e[-1]=="+":
e=e[:-1]
put(reset)
else:
put(term.white_on_black_reverse)
self.errorStatus = e self.errorStatus = e
placeCursor(3, 22) placeCursor(3, 22)
put(e) put(e+colors[0]+term.normal)
self.placeCursor() self.placeCursor()
@ -139,28 +165,31 @@ def putCursorInside(guess,letter):
placeCursor(col,row); placeCursor(col,row);
put(colors[0]) put(colors[0])
def blankScreen(): def blankScreen():
put(reset+term.clear)
placeCursor(0,0)
'''
put(reset+'\033[2J') put(reset+'\033[2J')
placeCursor(0,0) placeCursor(0,0)
put(reset+((" "*80+"\n")*24)); put(reset+((" "*80+"\n")*24));
placeCursor(0,0) placeCursor(0,0)'''
def main(): def main():
cursorLocation = 0 cursorLocation = 0
blankScreen() blankScreen()
if not unicode: if not unicode:
screen=fg('green')+''' _ _ screen=termgreen+''' _ _
__ _____ _ __ __| | | ___ __ _____ _ __ __| | | ___
\ \ /\ / / _ \| '__/ _` | |/ _ \ \ \ /\ / / _ \| '__/ _` | |/ _ \
\ V V / (_) | | | (_| | | __/ \ V V / (_) | | | (_| | | __/
\_/\_/ \___/|_| \__,_|_|\___| ''' \_/\_/ \___/|_| \__,_|_|\___| '''
else: else:
screen=fg('green')+''' screen=term.bright_green+'''
''' '''
screen +=''' screen +='''
clone by xereeto clone by xereeto
original by josh wardle '''+fg('white')+''' original by josh wardle '''+termwhite+'''
---------------------------------------- ----------------------------------------
Guess the WORDLE in 6 tries. Guess the WORDLE in 6 tries.
Each guess must be a valid 5 letter word. Each guess must be a valid 5 letter word.
@ -175,7 +204,7 @@ def main():
a s d f g h j k l a s d f g h j k l
z x c v b n m z x c v b n m
---------------------------------------- ----------------------------------------
Game: Game: Turn:
''' '''
put(screen) put(screen)
goHome() goHome()
@ -189,6 +218,7 @@ def main():
char = 0 char = 0
w.placeCursor() w.placeCursor()
while not turnOver: while not turnOver:
put(term.black)
try: try:
char = getch().upper() char = getch().upper()
except OverflowError: except OverflowError:
@ -220,7 +250,7 @@ def main():
elif w.check(): elif w.check():
turnOver=True turnOver=True
else: else:
w.updateError("You have to enter a real word!") w.updateError("Word not in dictionary!")
if w.wonGame(): if w.wonGame():
s="s" s="s"
if w.turn==1: if w.turn==1:
@ -243,13 +273,15 @@ def main():
import sys import sys
w=None w=None
try: try:
w=main() with term.fullscreen(), term.hidden_cursor():
w=main()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
blankScreen() with term.fullscreen():
print("Goodbye!") put(term.clear)
print(reset+"Goodbye!")
#put("Goodbye!") #put("Goodbye!")
cliflag = sys.argv[1] if len(sys.argv) > 1 else '' cliflag = sys.argv[1] if len(sys.argv) > 1 else ''
if(w and cliflag != "--no-unicode" and unicode): if(w and cliflag != "--no-unicode" and unicode):