diff --git a/.gitignore b/.gitignore index 550d67d..29291d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -src/__pycache__ +src/**/*__pycache__ diff --git a/readme.md b/readme.md index 758cd16..ed57ae0 100644 --- a/readme.md +++ b/readme.md @@ -3,8 +3,8 @@ An advanced **Python** _rock-paper-scissors_ console application. ## How to play: -To run this game, clone the repository by typing this in the terminal: `git clone ` and `cd ` -Alternatively, you can look at the [releases page](https://github.com/clark-john/rps-python/releases) to download the source files. +To run this game, clone the repository by typing this in the terminal: `git clone ` and
+`cd `. Alternatively, you can look at the [releases page](https://github.com/clark-john/rps-python/releases) to download the source files. After cloning the repo, please run the **index.py** file first, otherwise it won't work.
You can also type this into terminal: `python index.py` or `py index.py` @@ -29,19 +29,23 @@ These are the **Python** modules that I used: + tkinter + csv + shutil ++ re #### What *Python* modules are required to install? These modules are required to install before running this game: + matplotlib + colorama +Bonus: Install the required modules by running `pip install -r requirements.txt`. +Make sure your current working directory in your terminal is the same path as the _requirements.txt_ file. + #### My Python Version: 3.10.2 #### rps-python Latest Version: -v1.0.3 +v1.0.4 ## Documentation Coming Soon:) diff --git a/src/colors/colors.py b/src/colors/colors.py new file mode 100644 index 0000000..b61f43b --- /dev/null +++ b/src/colors/colors.py @@ -0,0 +1,152 @@ +# CSS Colors List + +colors_list = [ +"aliceblue", +"antiquewhite", +"aqua", +"aquamarine", +"azure", +"beige", +"bisque", +"black", +"blanchedalmond", +"blue", +"blueviolet", +"brown", +"burlywood", +"cadetblue", +"chartreuse", +"chocolate", +"coral", +"cornflowerblue", +"cornsilk", +"crimson", +"cyan", +"darkblue", +"darkcyan", +"darkgoldenrod", +"darkgray", +"darkgrey", +"darkgreen", +"darkkhaki", +"darkmagenta", +"darkolivegreen", +"darkorange", +"darkorchid", +"darkred", +"darksalmon", +"darkseagreen", +"darkslateblue", +"darkslategray", +"darkslategrey", +"darkturquoise", +"darkviolet", +"deeppink", +"deepskyblue", +"dimgray", +"dimgrey", +"dodgerblue", +"firebrick", +"floralwhite" +"forestgreen", +"fuchsia", +"gainsboro", +"ghostwhite", +"gold", +"goldenrod", +"gray", +"grey", +"green", +"greenyellow", +"honeydew", +"hotpink", +"indianred", +"indigo", +"ivory", +"khaki", +"lavender", +"lavenderblush", +"lawngreen", +"lemonchiffon", +"lightblue", +"lightcoral" +"lightcyan", +"lightgoldenrodyellow", +"lightgray", +"lightgrey", +"lightgreen", +"lightpink", +"lightsalmon", +"lightseagreen", +"lightskyblue", +"lightslategray", +"lightslategrey", +"lightsteelblue", +"lightyellow", +"lime", +"limegreen", +"linen", +"magenta", +"maroon", +"mediumaquamarine", +"mediumblue", +"mediumorchid", +"mediumpurple" +"mediumseagreen", +"mediumslateblue", +"mediumspringgreen", +"mediumturquoise", +"mediumvioletred", +"midnightblue", +"mintcream", +"mistyrose", +"moccasin", +"navajowhite", +"navy", +"oldlace", +"olive", +"olivedrab", +"orange", +"orangered", +"orchid", +"palegoldenrod", +"palegreen", +"paleturquoise", +"palevioletred", +"papayawhip", +"peachpuff", +"peru", +"pink", +"plum", +"powderblue", +"purple", +"rebeccapurple", +"red", +"rosybrown", +"royalblue", +"saddlebrown", +"salmon", +"sandybrown", +"seagreen", +"seashell", +"sienna", +"silver", +"skyblue", +"slateblue", +"slategray", +"slategrey", +"snow", +"springgreen", +"steelblue", +"tan", +"teal", +"thistle", +"tomato", +"turquoise", +"violet", +"wheat", +"white", +"whitesmoke", +"yellow", +"yellowgreen" +] \ No newline at end of file diff --git a/src/colors/colors.txt b/src/colors/colors.txt new file mode 100644 index 0000000..25d0503 --- /dev/null +++ b/src/colors/colors.txt @@ -0,0 +1,148 @@ +aliceblue +antiquewhite +aqua +aquamarine +azure +beige +bisque +black +blanchedalmond +blue +blueviolet +brown +burlywood +cadetblue +chartreuse +chocolate +coral +cornflowerblue +cornsilk +crimson +cyan +darkblue +darkcyan +darkgoldenrod +darkgray +darkgrey +darkgreen +darkkhaki +darkmagenta +darkolivegreen +darkorange +darkorchid +darkred +darksalmon +darkseagreen +darkslateblue +darkslategray +darkslategrey +darkturquoise +darkviolet +deeppink +deepskyblue +dimgray +dimgrey +dodgerblue +firebrick +floralwhite +forestgreen +fuchsia +gainsboro +ghostwhite +gold +goldenrod +gray +grey +green +greenyellow +honeydew +hotpink +indianred +indigo +ivory +khaki +lavender +lavenderblush +lawngreen +lemonchiffon +lightblue +lightcoral +lightcyan +lightgoldenrodyellow +lightgray +lightgrey +lightgreen +lightpink +lightsalmon +lightseagreen +lightskyblue +lightslategray +lightslategrey +lightsteelblue +lightyellow +lime +limegreen +linen +magenta +maroon +mediumaquamarine +mediumblue +mediumorchid +mediumpurple +mediumseagreen +mediumslateblue +mediumspringgreen +mediumturquoise +mediumvioletred +midnightblue +mintcream +mistyrose +moccasin +navajowhite +navy +oldlace +olive +olivedrab +orange +orangered +orchid +palegoldenrod +palegreen +paleturquoise +palevioletred +papayawhip +peachpuff +peru +pink +plum +powderblue +purple +rebeccapurple +red +rosybrown +royalblue +saddlebrown +salmon +sandybrown +seagreen +seashell +sienna +silver +skyblue +slateblue +slategray +slategrey +snow +springgreen +steelblue +tan +teal +thistle +tomato +turquoise +violet +wheat +white +whitesmoke +yellow +yellowgreen \ No newline at end of file diff --git a/src/commands.py b/src/commands.py index 94aab81..a84e2f1 100644 --- a/src/commands.py +++ b/src/commands.py @@ -6,10 +6,17 @@ import matplotlib.pyplot as mtp import sys from shutil import rmtree +from re import compile + +sys.path.append('./colors/') + +from colors import colors_list config = ConfigParser() config.read('./settings.ini') +hex_pattern = compile('^[a-fA-F\\d]{6}$') + chart_types = ['bar', 'dots', 'plot', @@ -46,16 +53,39 @@ def nonstop_toggle(): def changedatacolor(): if path.exists('./settings.ini'): - datachartcolor = input("Type a name of color or hex code of a color.\n") - if datachartcolor == "exit": - return - elif datachartcolor == '': - print(critical + 'Data color cannot be null.'+r) - else: + + datatype = input("Hex code or color name? (h/n)\n").lower() + + def set_color(): config.set('Chart', 'datachartcolor', datachartcolor) with open ('settings.ini', 'w') as settingsfile: config.write(settingsfile) - print(success + 'Bar color updated successfully.'+r) + msgbox.showinfo('Success', 'Bar color updated successfully.') + + if datatype == 'h': + datachartcolor = input("Type a hexadecimal code for a color.\n") + if datachartcolor == "exit": + return + elif datachartcolor == '': + msgbox.showerror('Error', "Bar color cannot be null.") + elif hex_pattern.match(datachartcolor) == None: + msgbox.showerror('Error', "Invalid hex code.") + else: + set_color() + elif datatype == 'n': + datachartcolor = input("Type a name of a color.\n") + if datachartcolor not in colors_list: + msgbox.showerror('Error', "Invalid color name.") + elif datachartcolor == '': + msgbox.showerror('Error', "Bar color cannot be null.") + elif datachartcolor == 'exit': + exit() + else: + set_color() + + elif datatype == 'exit': + exit() + else: msgbox.showerror("Error", "Couldn\'t find settings.ini file.") @@ -142,7 +172,7 @@ def currentconsolecolor(): if path.exists('./settings.ini'): print(config.get('Console', 'default_console_color')) else: - msgbox.showerror("Error", "Couldn\'t find settings.ini file."). + msgbox.showerror("Error", "Couldn\'t find settings.ini file.") def datacolorpreview(): x = ['A','B','C'] @@ -153,8 +183,9 @@ def datacolorpreview(): mtp.show() def clearcache(): - if path.exists('./__pycache__'): + if path.exists('./__pycache__') and path.exists('./colors/__pycache__'): rmtree('__pycache__') + rmtree('./colors/__pycache__') msgbox.showinfo('Success','Cleared the cache successfully.') else: msgbox.showinfo('Clear cache', '__pycache__ folder not found.') @@ -165,4 +196,7 @@ def clearscore(): msgbox.showinfo('Success','Cleared the score successfully.') else: msgbox.showinfo('Clear score', 'score.csv not found.') - \ No newline at end of file + +def showcolorlist(): + with open('./colors/colors.txt', 'r') as colors: + print(colors.read()) \ No newline at end of file diff --git a/src/commands_help.py b/src/commands_help.py index d5fb01c..91522d3 100644 --- a/src/commands_help.py +++ b/src/commands_help.py @@ -12,7 +12,6 @@ def nonstop_toggle_help(): def changedatacolor_help(): print("Description: It prompts for changing the data color by hexcode or name of a color.") - print("Make sure you add the \"#\" before the hexcode, or specify an appropriate name of a color.") print("List of available named colors: "+yel+"https://matplotlib.org/stable/gallery/color/named_colors.html"+consolecolor) print("Alias: cdc") @@ -57,4 +56,8 @@ def clearcache_help(): print("Alias: uncache") def clearscore_help(): - print("Description: Clears/resets the csv score file.") \ No newline at end of file + print("Description: Clears/resets the csv score file.") + +def showcolorlist_help(): + print("Description: It shows the list of accepted color names.") + print("Alias: colors") \ No newline at end of file diff --git a/src/index.py b/src/index.py index 9ed8516..d181026 100644 --- a/src/index.py +++ b/src/index.py @@ -42,4 +42,4 @@ else: print(red+"Try again.\n") -# Version v1.0.3 \ No newline at end of file +# Version v1.0.4 \ No newline at end of file diff --git a/src/rps.py b/src/rps.py index 128e60f..7c098b2 100644 --- a/src/rps.py +++ b/src/rps.py @@ -3,10 +3,10 @@ from random import choice from datetime import datetime from colorama import init, Fore, Style -from os import system from console_colors import consolecolor from tkinter import messagebox as msgbox from csv import writer +from platform import system init() def play(): @@ -21,12 +21,21 @@ def play(): total_games = 0 # Colorama Variables + + #Style + checking the system - #Style - dim = Style.DIM - normal = Style.NORMAL - bright = Style.BRIGHT + if system() == 'Windows': + dim = Style.DIM + normal = Style.BRIGHT # since Windows has only dim and bright colors. + bright = Style.BRIGHT + + elif system() == 'Linux': + dim = Style.DIM + normal = Style.NORMAL + bright = Style.BRIGHT + #Fore + critical = Fore.RED success = Fore.GREEN yellow = Fore.YELLOW white = Fore.WHITE @@ -53,13 +62,13 @@ def play(): if user_action not in possible_actions \ and not user_action == 'stop': - print(critical + "Invalid option. Try again.") + print(critical + "Invalid option. Try again." + consolecolor) continue if user_action == computer_actions: print(bright+"You choose", user_action.lower()) print(compname, "chooses", computer_actions) - print(yellow++ "It's a tie"+consolecolor) + print(normal+yellow+ "It's a tie"+consolecolor) tie += 1 total_games += 1 @@ -152,23 +161,26 @@ def play(): datacolor = config.get('Chart', 'datachartcolor') if charttype == 'bar': - mtp.bar(userscores, compscores, color=datacolor) + mtp.bar(userscores, compscores, color=f"#{datacolor}") mtp.title('Your RPS Scoreboard') print("As of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() elif charttype == 'plot': - mtp.plot(userscores, compscores, marker='o', color=datacolor) + mtp.plot(userscores, compscores, marker='o', color=f"#{datacolor}") mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() elif charttype == 'dots': - mtp.scatter(userscores, compscores, color=datacolor, s=[280,140,200]) + mtp.scatter(userscores, compscores, color=f"#{datacolor}", s=[280,140,200]) mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() @@ -176,13 +188,15 @@ def play(): mtp.stem(userscores, compscores, markerfmt='o') mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() elif charttype == 'step': - mtp.step(userscores, compscores, color=datacolor) + mtp.step(userscores, compscores, color=f"#{datacolor}") mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() else: @@ -234,23 +248,26 @@ def play(): datacolor = config.get('Chart', 'datachartcolor') if charttype == 'bar': - mtp.bar(userscores, compscores, color=datacolor) + mtp.bar(userscores, compscores, color=f"#{datacolor}") mtp.title('Your RPS Scoreboard') print("As of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() elif charttype == 'plot': - mtp.plot(userscores, compscores, marker='o', color=datacolor) + mtp.plot(userscores, compscores, marker='o', color=f"#{datacolor}") mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() elif charttype == 'dots': - mtp.scatter(userscores, compscores, color=datacolor, s=[280,140,200]) + mtp.scatter(userscores, compscores, color=f"#{datacolor}", s=[280,140,200]) mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() @@ -258,20 +275,17 @@ def play(): mtp.stem(userscores, compscores, markerfmt='o') mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() elif charttype == 'step': - mtp.step(userscores, compscores, color=datacolor) + mtp.step(userscores, compscores, color=f"#{datacolor}") mtp.title('Your RPS Scoreboard') print("\nAs of", dateasstr) + mtp.savefig('score.png') mtp.show() exit() else: print("Exiting...") return - elif user_prompt != 'y' and not(user_prompt == 'n'): - print("Try again.") - continue - elif user_prompt == 'y': - break \ No newline at end of file diff --git a/src/rps_tui.py b/src/rps_tui.py index 988f712..c6bb29a 100644 --- a/src/rps_tui.py +++ b/src/rps_tui.py @@ -3,114 +3,120 @@ from commands_help import * commands = ['toggle_nonstop', #0 - 'changedatacolor', #1 - 'currentdatacolor', #2 - 'game', #3 - 'changecharttype', #4 - 'currentcharttype', #5 - 'isitnonstop', #6 - 'resetsettings', #7 - 'changeconsolecolor', #8 - 'currentconsolecolor', #9 - 'datacolorpreview', #10 - 'clearcache', #11 - 'clearscore', #12 - ] + 'changedatacolor', #1 + 'currentdatacolor', #2 + 'game', #3 + 'changecharttype', #4 + 'currentcharttype', #5 + 'isitnonstop', #6 + 'resetsettings', #7 + 'changeconsolecolor', #8 + 'currentconsolecolor', #9 + 'datacolorpreview', #10 + 'clearcache', #11 + 'clearscore', #12 + 'showcolorlist' #13 + ] # Main cmd def cmd(): - print(""" - Welcome to rps-tui! + print(""" + Welcome to rps-tui! - List of available commands: - - changedatacolor - - toggle_nonstop - - currentdatacolor - - game - - changecharttype - - currentcharttype - - isitnonstop - - resetsettings - - changeconsolecolor - - currentconsolecolor - - datacolorpreview - - clearcache - - clearscore + List of available commands: + - changedatacolor + - toggle_nonstop + - currentdatacolor + - game + - changecharttype + - currentcharttype + - isitnonstop + - resetsettings + - changeconsolecolor + - currentconsolecolor + - datacolorpreview + - clearcache + - clearscore + - showcolorlist - Other commands: - - python_version - - system - - rps - - Type \"\" to get started with the command. - or type \"help \" to see the usage and description of a command.""") - while True: - com = input(">> ") - - if com == "exit": - break - elif com == "rps": - cmd() - elif com == 'python_version': - print(python_version()) - elif com == 'system': - print(system()) + Other commands: + - python_version + - system + - rps + + Type \"\" to get started with the command. + or type \"help \" to see the usage and description of a command.""") + while True: + com = input(">> ") + + if com == "exit": + break + elif com == "rps": + cmd() + elif com == 'python_version': + print(python_version()) + elif com == 'system': + print(system()) # Calling commands - if com == commands[0] or com == 'ns_tog': - nonstop_toggle() - elif com == commands[1] or com == 'chdc': - changedatacolor() - elif com == commands[2] or com == 'cdc': - currentdatacolor() - elif com == commands[3] or com == 'g': - game() - elif com == commands[4] or com == 'chct': - changecharttype() - elif com == commands[5] or com == 'cct': - currentcharttype() - elif com == commands[6] or com == 'isitns': - isitnonstop() - elif com == commands[7] or com == 'reset': - resetsettings() - elif com == commands[8] or com == 'chcc': - changeconsolecolor() - elif com == commands[9] or com == 'ccc': - currentconsolecolor() - elif com == commands[10] or com == 'viewcolor': - datacolorpreview() - elif com == commands[11] or com == 'uncache': - clearcache() - elif com == commands[12]: - clearscore() + if com == commands[0] or com == 'ns_tog': + nonstop_toggle() + elif com == commands[1] or com == 'chdc': + changedatacolor() + elif com == commands[2] or com == 'cdc': + currentdatacolor() + elif com == commands[3] or com == 'g': + game() + elif com == commands[4] or com == 'chct': + changecharttype() + elif com == commands[5] or com == 'cct': + currentcharttype() + elif com == commands[6] or com == 'isitns': + isitnonstop() + elif com == commands[7] or com == 'reset': + resetsettings() + elif com == commands[8] or com == 'chcc': + changeconsolecolor() + elif com == commands[9] or com == 'ccc': + currentconsolecolor() + elif com == commands[10] or com == 'viewcolor': + datacolorpreview() + elif com == commands[11] or com == 'uncache': + clearcache() + elif com == commands[12]: + clearscore() + elif com == commands[13] or com == 'colors': + showcolorlist() # Calling commands with usage argument - - if com == "help " + commands[0]: - nonstop_toggle_help() - elif com == "help " + commands[1]: - changedatacolor_help() - elif com == "help " + commands[2]: - currentdatacolor_help() - elif com == "help " + commands[3]: - game_help() - elif com == "help " + commands[4]: - changecharttype_help() - elif com == "help " + commands[5]: - currentcharttype_help() - elif com == "help " + commands[6]: - isitnonstop_help() - elif com == "help " + commands[7]: - resetsettings_help() - elif com == "help " + commands[8]: - changeconsolecolor_help() - elif com == "help " + commands[9]: - currentconsolecolor_help() - elif com == "help " + commands[10]: - datacolorpreview_help() - elif com == "help " + commands[11]: - clearcache_help() - elif com == "help " + commands[12]: - clearscore_help() \ No newline at end of file + + if com == "help " + commands[0]: + nonstop_toggle_help() + elif com == "help " + commands[1]: + changedatacolor_help() + elif com == "help " + commands[2]: + currentdatacolor_help() + elif com == "help " + commands[3]: + game_help() + elif com == "help " + commands[4]: + changecharttype_help() + elif com == "help " + commands[5]: + currentcharttype_help() + elif com == "help " + commands[6]: + isitnonstop_help() + elif com == "help " + commands[7]: + resetsettings_help() + elif com == "help " + commands[8]: + changeconsolecolor_help() + elif com == "help " + commands[9]: + currentconsolecolor_help() + elif com == "help " + commands[10]: + datacolorpreview_help() + elif com == "help " + commands[11]: + clearcache_help() + elif com == "help " + commands[12]: + clearscore_help() + elif com == "help " + commands[13]: + showcolorlist_help()