MTG Deck Builder Source Code 6/1/25
C:\Home
Here is the second iteration of my code. I've also included the 2 mini programs "logomaker" and "pycolor". Feel free to implement them in any of your own projects. As always, feel free to email me if you have any questions. I'm not a professional by any means, but I'm happy to help if I can. Also keep in mind that this source code may not be formatted properly (I may end up uploading the files in the future for simplicity sake).
import requests
import os
import time
import logomaker
import pycolor
def clear_console():
os.system('cls' if os.name == 'nt' else 'clear')
def get_card_by_name(card_name):
url = f"https://api.scryfall.com/cards/named?fuzzy={card_name}"
response = requests.get(url)
if response.status_code == 200:
card_data = response.json()
print(f"Name: {card_data['name']}")
print(f"Mana Cost: {card_data['mana_cost']}")
print(f"Type Line: {card_data['type_line']}")
print(f"Oracle Text: {card_data['oracle_text']}")
else:
print(f"Error: {response.status_code} - {response.json().get('details', 'Unknown error')}")
def card_name(card_name):
url = f"https://api.scryfall.com/cards/named?fuzzy={card_name}"
response = requests.get(url)
if response.status_code == 200:
card_data = response.json()
name = (f"{card_data['name']}")
return name
else:
print(f"Error: {response.status_code} - {response.json().get('details', 'Unknown error')}")
mainmenu = True
deckbuilding = False
#Display Logo
logomaker.DisplayLogo()
print(pycolor.ColorMyText("","white"))
#First input, asking user if they want to create a new deck, or load an existing deck
while mainmenu == True:
print("Would you like to create a new deck or load an existing deck? \n")
loadDeck = input("type new load or quit \n")
clear_console()
if loadDeck == "new":
#Creates a new deck name and gets the name of the commander.
#Commander is not factored into deck construction.
#As long as there's no textfile with the same deckName, create the new file to eventually store the deck.
deckName = input("What is the name of your new deck? \n")
deckCommander = input("What is the name of your new deck's Commander? \n")
try:
f = open(deckName + ".txt", "x")
print("Decklist for " + deckName + " has been created! \n")
mainmenu = False
deckbuilding = True
decklist = []
except:
clear_console()
print ("Failed to create deck, try another name \n")
continue
elif loadDeck == "load":
print("Load Deck")
elif loadDeck == "quit":
quit()
else:
clear_console()
print("Invalid input \n")
while deckbuilding == True:
options = input("Add/Delete/Save/Quit \n").lower()
if options == "add":
NewCard = input("Enter a card name \n")
ChosenCard = get_card_by_name(NewCard)
#Verify card entry is correct before adding. If card is accepted, write new card to deck txt file.
print(ChosenCard)
check = input("Is this card info correct? Type yes or no. \n").lower()
if check == "yes":
CardName = card_name(NewCard)
decklist.append(CardName)
print(decklist)
else:
print("Please reenter card data")
elif options == "save":
clear_console()
try:
with open(deckName + ".txt", "w") as file:
for item in decklist:
file.write(str(item) + "\n")
print("Deck has been Saved!")
except:
clear_console()
print ("Failed to save deck, file may be missing or corrupt")
elif options == "quit":
clear_console()
quit()
else:
print("Invalid input TEST")
time.sleep(2)
clear_console()
Text Color Program
def ColorMyText(string, color):
red ='\x1b' + '[31m'
green ='\x1b' + '[32m'
yellow ='\x1b' + '[33m'
blue = '\x1b' + '[34m'
purple ='\x1b' + '[35m'
white ='\x1b' + '[37m'
match color:
case "red":
final_string = red + string
case "green":
final_string = green + string
case "yellow":
final_string = yellow + string
case "blue":
final_string = blue + string
case "purple":
final_string = purple + string
case "white":
final_string = white + string
case _:
final_string = white + string
return final_string
Logo Generator
import pycolor
def DisplayLogo():
with open('logoart.txt', 'r') as file:
file_content = file.read()
ColoredLogo = pycolor.ColorMyText(file_content,"red")
print(ColoredLogo)