-1

Here is my code

#"We Love Random!" #import random
import random
import os
import pickle

FILE = 'PlayerPickle.pkl'
#Player class
class Player:
    level = 1
    player_name = "bob"
    health = 100
    max_health = 100
    damage = 10
    gold = 400
    pet = ""
    experience = 0
    defense = 2
    weapons = ["Paper Sword"]
    items = []
    equipped_weapon = ("Rat")
    experience_threshhold = 10


def askSave(player):
    ask = input("Do you want to save?\n--> ").upper()
    if ask == "Y" or ask == "YES":
        pickle.dump(player, open(FILE, "wb",))

    elif ask == "N" or ask == "NO":
        print("Okay, maybe next time!")
    else:
        print("Sorry, that does not compute with me! Please try again!")
        askSave(player)


def load(player):
    if os.path.isfile(FILE):
        return pickle.load(open(FILE,"rb")) #Loads the file
    return player

#funtion that allows a player to equip a weapon and increase their damage.        
def weapon_equip():
    print("Weapons: " + str(Player.weapons))
    selection = input("Which weapon would you like to equip? ")

    if selection == "Paper Sword":
        Player.weapons.remove("Paper Sword")
        Player.weapons.append(Player.equipped_weapon)
        Player.equipped_weapon = ()
        Player.equipped_weapon = "Paper Sword"
        print("You have equipped " + str(Player.equipped_weapon))
    elif selection == "Spear":
        Player.weapons.remove("Spear")
        Player.weapons.append(Player.equipped_weapon)
        Player.equipped_weapon = ()
        Player.equipped_weapon = "Spear"
        print("You have equipped " + str(Player.equipped_weapon))
    elif selection == "Knight's Longsword":
        Player.weapons.remove("Knight's Longsword")
        Player.weapons.append(Player.equipped_weapon)
        Player.equipped_weapon = ()
        Player.equipped_weapon = "Knight's Longsword"
        print("You have equipped " + str(Player.equipped_weapon))
    elif selection == "Dwarvn Scimitar":
        Player.weapons.remove("Dwarvn Scimitar")
        Player.weapons.append(Player.equipped_weapon)
        Player.equipped_weapon = ()
        Player.equipped_weapon = "Dwarvn Scimitar"
        print("You have equipped " + str(Player.equipped_weapon))
    else:
        print("No such thing has been found in thy inventory! ")
    
    main_menu()

#function for the weapons section of the main shop()
def weapons_shop():
    weapons_list = ["Paper Sword - Price: 40G - Damage: 15", "Spear - Price: 275G - Damage: 30", "Knight's Longsword - Price: 550G - Damage: 60", "Dwarvn Scimitar (forged from hell) - Price: 1111G - Damage:150" ]
    print(weapons_list[0])
    print(weapons_list[1])
    print(weapons_list[2])
    print(weapons_list[3])
    weapons_shop_selection = int(input("Select a weapon to purchase from 1-4: "))

    if ((weapons_shop_selection == 1) and (Player.gold >= 40) and ("Paper Sword" not in Player.weapons)):
        Player.gold = Player.gold - 40
        Player.weapons.append("Paper Sword")
        print("You have purchased a Paper Sword")
        print("     ")
        shop()
    
    elif weapons_shop_selection == 2 and Player.gold >= 275 and "Spear" not in Player.weapons:
        Player.gold = Player.gold - 40
        Player.weapons.append("Spear")
        print("You have purchased a Spear")
        print("     ")
        shop()
    
    else:
        print("You are too poor, or you already have that item...")
        print("     ")
        shop()
#function for blackjack game
def blackjack():
    print("        ")
    print("Your balance: " + str(Player.gold) +"G")
    bet = int(input("How much would gold would you like to bet? (Type a negative number to leave) "))
    if bet > Player.gold:
        print("INVALID INPUT... ")
        shop()
    if bet >= 1:
        card_deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
        ace = [1, 11]
        
        class PlayerHand:
            player_cards_hand = []
            print("     ")
            player_cards_hand.append(random.choice(card_deck))
            player_cards_hand.append(random.choice(card_deck))
            player_card_total = 0
            player_card_total_soft = 0
            w = False
            aces = 0

        class DealerHand:
            dealer_cards_hand = []
            print("     ")
            dealer_cards_hand.append(random.choice(card_deck))
            dealer_cards_hand.append(random.choice(card_deck))
            player_card_total = 0
            dealer_cards_total_soft = 0
            w = False
            aces = 0
        
        ongoing = True
        ongoing_dealer = True
    else:
        main_menu()
    #This is the loop for the player's turn.
    while ongoing:
        if 2 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 2 * (PlayerHand.player_cards_hand.count(2))
        if 3 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 3 * (PlayerHand.player_cards_hand.count(3))
        if 4 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 4 * (PlayerHand.player_cards_hand.count(4))
        if 5 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 5 * (PlayerHand.player_cards_hand.count(5))
        if 6 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 6 * (PlayerHand.player_cards_hand.count(6))
        if 7 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 7 * (PlayerHand.player_cards_hand.count(7))
        if 8 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 8 * (PlayerHand.player_cards_hand.count(8))
        if 9 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 9 * (PlayerHand.player_cards_hand.count(9))
        if 10 in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 10 * (PlayerHand.player_cards_hand.count(10))
        
        if 'J' or 'Q' or 'K' in PlayerHand.player_cards_hand:
            PlayerHand.player_card_total = PlayerHand.player_card_total + 10 * ((PlayerHand.player_cards_hand.count("J")) + (PlayerHand.player_cards_hand.count("Q")) + (PlayerHand.player_cards_hand.count("K")))

        PlayerHand.player_card_total_soft = PlayerHand.player_card_total

        PlayerHand.aces = PlayerHand.aces + int(PlayerHand.player_cards_hand.count("A"))


        PlayerHand.player_card_total = PlayerHand.player_card_total + ((PlayerHand.player_cards_hand.count("A")) * (ace[0]))
        
        if PlayerHand.aces > 1:
            PlayerHand.player_card_total_soft = PlayerHand.player_card_total_soft + 11 + ((PlayerHand.player_cards_hand.count("A")) * (ace[0]))
        elif PlayerHand.aces == 1:
            PlayerHand.player_card_total_soft = PlayerHand.player_card_total_soft + 11
        else:
            PlayerHand.player_card_total_soft = 0
            
        
        if PlayerHand.aces >= 1:
            print(str(PlayerHand.player_card_total) + " / "+ str(PlayerHand.player_card_total_soft))
            
        else:
        
            print("Hand: " + str(PlayerHand.player_cards_hand))
            print(PlayerHand.player_card_total)
        
        if PlayerHand.player_card_total > 21:
            print("BUSTED")
            Player.gold = Player.gold - bet
            ongoing = False
            shop()
        
        print("Dealer's Hand: " + str(DealerHand.dealer_cards_hand[1]))



        player_choice = input(("Would you like to hit or stand? "))
            
        if player_choice == "hit":
            PlayerHand.player_card_total = 0
            PlayerHand.aces = 0
            PlayerHand.player_cards_hand.append(random.choice(card_deck))
        else:
            if PlayerHand.player_card_total < PlayerHand.player_card_total_soft & PlayerHand.player_card_total_soft <= 21:
                PlayerHand.player_card_total = PlayerHand.player_card_total_soft
            print("You stood")
            break
    #This is the loop for the dealer's turn.
    while ongoing_dealer:
        if 2 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 2 * (DealerHand.dealer_cards_hand.count(2))
        if 3 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 3 * (DealerHand.dealer_cards_hand.count(3))
        if 4 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 4 * (DealerHand.dealer_cards_hand.count(4))
        if 5 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 5 * (DealerHand.dealer_cards_hand.count(5))
        if 6 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 6 * (DealerHand.dealer_cards_hand.count(6))
        if 7 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 7 * (DealerHand.dealer_cards_hand.count(7))
        if 8 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 8 * (DealerHand.dealer_cards_hand.count(8))
        if 9 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 9 * (DealerHand.dealer_cards_hand.count(9))
        if 10 in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 10 * (DealerHand.dealer_cards_hand.count(10))
        
        if 'J' or 'Q' or 'K' in DealerHand.dealer_cards_hand:
            DealerHand.player_card_total = DealerHand.player_card_total + 10 * ((DealerHand.dealer_cards_hand.count("J")) + (DealerHand.dealer_cards_hand.count("Q")) + (DealerHand.dealer_cards_hand.count("K")))

        DealerHand.dealer_cards_total_soft = DealerHand.player_card_total

        DealerHand.aces = DealerHand.aces + int(DealerHand.dealer_cards_hand.count("A"))


        DealerHand.player_card_total = DealerHand.player_card_total + ((DealerHand.dealer_cards_hand.count("A")) * (ace[0]))
        
        if DealerHand.aces > 1:
            DealerHand.dealer_cards_total_soft = DealerHand.dealer_cards_total_soft + 11 + ((DealerHand.dealer_cards_hand.count("A")) * (ace[0]))
        elif DealerHand.aces == 1:
            DealerHand.dealer_cards_total_soft = DealerHand.dealer_cards_total_soft + 11
        else:
            DealerHand.dealer_cards_total_soft = 0

        
            
        
        
        if DealerHand.player_card_total > 21:
            print("Dealer Busts! ")
            print("You Win! $" + str(bet))
            Player.gold = Player.gold + bet
            ongoing_dealer = False
            shop()

        
        if DealerHand.player_card_total > PlayerHand.player_card_total:
            if DealerHand.dealer_cards_total_soft > DealerHand.player_card_total & DealerHand.dealer_cards_total_soft <= 21:
                DealerHand.player_card_total = DealerHand.dealer_cards_total_soft
            break
        elif DealerHand.player_card_total == 17:
            ongoing_dealer = False
            break
    print(DealerHand.dealer_cards_hand)
    #This determines who wins or if the game is a tie.
    if DealerHand.player_card_total == PlayerHand.player_card_total:
        print("Tie! ")
    elif DealerHand.player_card_total < PlayerHand.player_card_total:
        print("You Win! G" + str(bet))
        Player.gold = Player.gold + bet
    elif DealerHand.player_card_total > PlayerHand.player_card_total:
        print("You Lose. . . $-" + str(bet))
        Player.gold = Player.gold - bet

        main_menu()
        
            
    
    
    
    
    
        



#function for basic items and consumables.
def item_shop():
    print("       ")
    print("Welcome to the shop sir! Explore the catalogs and let me know if you want to purcahse anything... ")
    item_shop_selection = int(input("Catalog: 2 for items, 3 for Ranks, 4 for petshop: "))






#function for the general shop with misc things.
def shop():

    print("WELCOME TO THE SHOP!")
    print('''
    ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⢔⢒⡿⠯⠥⢦⣦⣾⣄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣾⢮⠊⠁⠀⠀⠀⠀⠈⠉⠛⠳⡀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣧⣿⣝⡴⡔⠀⠀⠀⠀⠀⠀⠀⠀⠘⡀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣀⣀⣦⣶⣿⣿⣯⣿⢽⠁⢰⣢⣶⣦⣌⠠⠴⠆⠘⣀⠀⠀⠀
⠀⠀⠀⠀⢀⠔⠁⠀⢂⠘⢻⢛⣛⠿⣝⠁⠀⠼⣁⡴⣖⣫⠙⠙⠿⡳⡅⠀⠀⠀
⠀⠀⠀⢀⠂⠰⠀⠀⠈⡄⢠⢓⣺⢇⡇⣊⠐⠀⠉⠁⠲⠒⠀⠀⠀⠑⠅⠀⠀⠀
⠀⠀⠀⣨⠀⡇⠀⠀⠀⠰⢸⠄⠄⣸⣷⡦⣄⢤⠄⢄⡀⣀⠤⠠⡀⠀⠈⡄⠀⠀
⠀⠀⢠⠁⠙⣇⠀⠀⠀⠀⢾⠘⢠⣿⣟⣿⣿⣪⣮⣶⣸⣮⣖⣢⣌⠁⠀⠁⠀⠀
⠀⠀⢸⠀⠀⢹⠀⠀⠀⠀⠸⠀⢝⣻⣯⣿⢿⡫⢺⡩⠍⣉⣉⣨⡗⠉⠂⠊⠀⠀
⠀⠀⡈⠂⠀⣾⠀⠀⠀⠀⠀⠆⡸⣹⡿⣿⣯⣷⣱⣙⠫⠧⠷⣦⠀⠀⠀⠀⠀⠀
⠀⢠⠇⠀⠀⢉⠀⠀⠀⠀⠀⠈⠕⣻⣿⣿⣿⣟⣿⣿⡷⣶⣾⠿⠒⡁⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠈⡇⠀⠀⠀⠀⠀⠈⢇⠻⠽⣿⡿⢟⣿⣻⡟⠁⠰⣯⡕⡰⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢩⠀⠀⠀⠀⠀⢀⠬⣍⠱⠨⠯⠛⠙⢏⠀⢀⡀⣨⡀⠤⢚⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢂⠀⠀⠀⠀⠀⢠⠁⠀⠀⠀⠀⠀⢸⠄⠰⡶⠲⢦⠓⠍⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠡⡀⠀⠀⠀⠀⠀⠀⡄⠀⢀⠄⠊⠉⠙⠑⠒⠊⠉⠀⠀''')

    print("Select 1 to view items;\
     Select 2 to view weapons; \
     Select 3 to play blackjack;\
     Select 4 to exit the shop;")
    shop_selection = int(input("Input your shop selection: "))

    if shop_selection == 4:
        main_menu()
    if shop_selection == 3:
        blackjack()
    elif shop_selection == 2:
        weapons_shop()
    elif shop_selection == 1:
        item_shop()


#function to fight monsters .
def mob_fight():
    mob_attack = (random.randint(0,15))
    mob_health = (random.randint(50,70))
    mob_xp_drop = (random.randint(2,10))
    mob_gold_drop = (random.randint(10, 25))

    print("A GOBLIN APPROACHES!")
    print('''
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣶⣿⣿⣶⣄⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠢⣤⣀⡀⠀⠀⠀⢿⣧⣄⡉⠻⢿⣿⣿⡿⠟⢉⣠⣼⡿⠀⠀⠀⠀⣀⣤⠔⠀
⠀⠀⠈⢻⣿⣶⠀⣷⠀⠉⠛⠿⠶⡴⢿⡿⢦⠶⠿⠛⠉⠀⣾⠀⣶⣿⡟⠁⠀⠀
⠀⠀⠀⠀⠻⣿⡆⠘⡇⠘⠷⠠⠦⠀⣾⣷⠀⠴⠄⠾⠃⢸⠃⢰⣿⠟⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠋⢠⣾⣥⣴⣶⣶⣆⠘⣿⣿⠃⣰⣶⣶⣦⣬⣷⡄⠙⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢋⠛⠻⠿⣿⠟⢹⣆⠸⠇⣰⡏⠻⣿⠿⠟⠛⡙⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠈⢧⡀⠠⠄⠀⠈⠛⠀⠀⠛⠁⠀⠠⠄⢀⡼⠁⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠈⢻⣦⡀⠃⠀⣿⡆⢰⣿⠀⠘⢀⣴⡟⠁⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⣦⡀⠘⠇⠸⠃⢀⣴⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢿⣿⣷⣄⣠⣾⣿⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠻⣿⣿⠟⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀''')
    
    while mob_health > 0 and Player.health:
        player_input = int(input("1 to attack; 2 to defend; 3 to taunt;"))
        mob_attack = (random.randint(0,15))
        if player_input == 1:
            mob_health = mob_health - Player.damage
            print("You dealt " + str(Player.damage) + " damage!")
            Player.health = Player.health - mob_attack
            print("You took " + str(mob_attack) + " damage!")
            print("Player HP: " + str(Player.health))
            print("Mob HP: " + str(mob_health))

        elif player_input == 2:
            Player.health = Player.health - (mob_attack - Player.defense)
            
            print("You only took " + str(mob_attack - Player.defense) + " damage!")
            print("Player HP: " + str(Player.health))
            print("Mob HP: " + str(mob_health))
        elif player_input == 3:
            print("The goblin laughs at you :) ")
        else:
            print("Invalid choice... ")
    
    if Player.health > 0:
        Player.health = Player.max_health
        print("You win!")
        Player.experience = Player.experience + mob_xp_drop
        print("You have gained " + str(mob_xp_drop) + " XP")
        Player.gold = Player.gold + mob_gold_drop
        print("You have gained " + str(mob_gold_drop) + " G")

    else:
        print("You Lose!")
        Player.health = Player.max_health
    
    if Player.experience >= Player.experience_threshhold:
        Player.level = Player.level + 1
        Player.experience_threshhold = Player.experience_threshhold * 2
        Player.experience = Player.experience - Player.experience_threshhold

    print("         ")
    print("MAIN MENU")
    main_menu()
    
   #Player main menu. 
def main_menu():

    player = Player()
    player = load(player)
    print(Player.gold)
    print(player.gold)
    Player.player_name = player.player_name
    Player.experience = player.experience
    Player.experience_threshhold = player.experience_threshhold
    Player.weapons = player.weapons
    Player.equipped_weapon = player.equipped_weapon
    Player.health = player.health
    Player.max_health = player.max_health
    Player.damage = player.damage
    Player.defense = player.defense
    Player.gold = player.gold
    Player.items = player.items
    Player.level = player.level
    Player.pet = player.pet



    print("Select 1 to fight mobs; Select 2 to go to shop; Select 3 to view stats; Select 4 to equip a weapon; Select 5 to save; Select 6 to load")
    menu_selection = int(input("Input your menu selection: "))

    if menu_selection == 1:
        mob_fight()
        print("You go to the woods to fight mobs... ")
    elif menu_selection == 2:
        print("      ")
        shop()
    elif menu_selection == 3:
        print('''⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
            ⢀⣠⣴⣶⣶⣶⣶⣶⣶⣶⣦⣄⡀⠀⠐⡆⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢠⣴⣿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣄⣼⡆⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿⣿⣿⣷⡦⠄⣀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣤⠀⠈⠀⠀⠀⠀
⠀⠀⠀⠀⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀
⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⣿⡇⠀⠀⠀⠀
⠀⠀⠀⢰⣿⣿⣿⣯⣿⣿⣿⣿⣿⣿⣿⣿⣟⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀
⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿⣿⣿⡫⣁⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀
⠀⠀⠀⠀⢿⣿⣿⣿⣿⣿⣿⣿⡟⠃⣿⣿⣿⡟⠁⠀⠉⠛⠛⢻⣿⣿⣿⣿⣿⣿⡟⠀⠀⠀⠀
⠀⠀⠀⠀⢸⣿⣿⣿⣿⡿⢿⣿⣯⡽⠿⠛⠉⠀⠀⠀⣬⠽⢷⣶⣿⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀
⠀⠀⠀⣠⠋⢀⠙⡏⠁⣀⣾⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⣠⣿⣿⣿⡇⠘⣿⢉⡄⠱⡄⠀⠀⠀
⠀⠀⠀⢇⠀⠲⣹⣿⠀⠀⠈⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠀⢸⡟⡡⠀⠠⠃⠀⠀⠀
⠀⠀⠀⠈⠱⢄⡈⠹⡆⠀⠀⠀⠀⠀⠀⠀⠀⠂⠀⠀⠀⠀⠀⠀⠀⢀⡎⠁⢀⠔⠃⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠉⠙⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠜⠉⠉⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠒⡀⠀⠀⠀⠀⠐⠒⠀⠀⠀⠀⢀⠖⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢢⣤⡀⠀⠀⠀⣠⣤⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⢾⠿⣿⣿⣿⣿⣿⡿⣗⡆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠊⠘⡞⠀⠈⠙⠛⠛⠉⠀⢻⠇⠈⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣀⣀⣴⡿⠀⠀⠰⣀⠀⠀⠀⠀⠀⢠⠇⠀⠀⢹⣦⣄⣀⠀⠀⠀⠀⠀⠀⠀''')
        print("PLAYER STATS: ")
        print("Level: " + str(Player.level))
        print("HP: " + str(Player.max_health))
        print("Damage: " + str(Player.damage))
        print("Gold: " + str(Player.gold))
        print("XP: " + str(Player.experience))
        print("Defense: " + str(Player.defense))
        print("Items: " + str(Player.items[:]))
        print("Weapons: " + str(Player.weapons[:]))
        print("Equipped weapon: " + str(Player.equipped_weapon[:]))
        main_menu()
    elif menu_selection == 4:
        weapon_equip()
    elif menu_selection == 5:
        askSave(player)
    elif menu_selection == 6:
        load(player)
    else:
        main_menu()

    askSave(player)

#We want the player to start and return on the menu, so we only call the main_menu.
print("WELCOME TO LISBANE! ")
print("                 __ \n"
"                /  >\n"
"  *            /  /________________________________________________\n"
" (O)77777777777)  7                                                `~~--__\n"
"8OO>>>>>>>>>>>>] <===                           __-\n"
" (O)LLLLLLLLLLL)  L________________________________________________.--~~\n"
"  *            \  \ \n"
"                \__>\n"
)
main_menu()

I don't know what is wrong since I told it to save the state of the Player class, but when i load and it displays, it shows the values it would show always.

class Player:
level = 1
player_name = "bob"
health = 100
max_health = 100
damage = 10
gold = 400
pet = ""
experience = 0
defense = 2
weapons = ["Paper Sword"]
items = []
equipped_weapon = ("Rat")
experience_threshhold = 10

my player definition

I my code gets stuck like it was saving and loading at some point, but i don't know what happened, and it stopped, also the manual saving process is bugged as hell, the AskSave() function loops, and load() just ends the game.

Deadmou5
  • 13
  • 5
  • 2
    Did you mean: `Player = pickle.load(open("Save File","rb"))`? – quamrana Aug 22 '23 at 14:12
  • 1
    the print statement is printing the function itself rather than printing the result of the function call: you want `Player.display()` rather than `Player.display` – Mouse Aug 22 '23 at 14:13
  • Don't do this *pickle.dump(Save, open("Save File", "wb",))* Where/when do you think the file will be closed? – DarkKnight Aug 22 '23 at 14:22
  • `Player.display` is a method and you want to print the result of calling it, not the definition of the method – JonSG Aug 22 '23 at 14:24
  • @DarkKnight: surely the `file` param of `dump` would release the open file object when `dump()` returns and thus close the file. – quamrana Aug 22 '23 at 14:25
  • When I use, Player = pickle.load(open("Save File","rb")) then it gives me the values that the class is set to having always when staring. – Deadmou5 Aug 22 '23 at 14:41
  • Please update your question with the definition of `Player`. – quamrana Aug 22 '23 at 14:41
  • @quamrana I just set Player as a public class and assign values to it, nothing fancy. – Deadmou5 Aug 22 '23 at 14:44
  • Also, my save file shows all my variables in the class but in binary txt format. – Deadmou5 Aug 22 '23 at 14:47
  • I think that `pickle` will work better if you create, save and load an *instance* of your `Player` class. Use `player = Player()` first, then `pickle.dump(player, open("Save File", "wb",))` and `player = pickle.load(open("Save File","rb"))` – quamrana Aug 22 '23 at 14:47
  • @quamrana i will try that but won.t the load just assign the saved values to the wrong variable? – Deadmou5 Aug 22 '23 at 14:49
  • I have also tried using dill as pickle and same issues. – Deadmou5 Aug 22 '23 at 14:51
  • I would try it first and get back to me later. I've never used `pickle` as such, but logically it must work this way. `Player` is a `class` defined to have those initial values. A `player` instance picks up those values when created, but when you update them, the `dump(player, ...)` should pick up those updates and write them to disk. Then `load(...)` should be able to re-create the instance. – quamrana Aug 22 '23 at 14:52
  • @quamrana didn't work, got some errors for displaying and adding values. – Deadmou5 Aug 22 '23 at 14:55
  • @quamrana Wouldn't the file handle "dangle" and only be closed when it goes out of scope? I could easily be wrong (I often am). Either way, I don't like that style - but that's just a personal thing – DarkKnight Aug 22 '23 at 15:03
  • @DarkKnight Yeah I tried it out and it wasn't it. It does "dangle," along with not even getting me the values from the save into Player. – Deadmou5 Aug 22 '23 at 15:10
  • @DarkKnight: See my answer. At least on my OS (Windows 10) the file seems to close ok. – quamrana Aug 22 '23 at 15:13
  • @DarkKnight: See the answers to this [question](https://stackoverflow.com/questions/57562182/does-a-file-stays-open-if-i-open-it-and-not-assign-it) – quamrana Aug 22 '23 at 15:19
  • @quamrana I updated my code now. – Deadmou5 Aug 23 '23 at 15:15
  • You are still using `Player. ... = ...`. You should be using `player. ... =` instead. – quamrana Aug 23 '23 at 18:41
  • @quamrana but then my Player class is never updated? – Deadmou5 Aug 25 '23 at 15:05
  • Yes. Generally you don't want a class like yours to be updated. You want instances to be updated like I have in my answer: `player.gold += delta` – quamrana Aug 25 '23 at 15:10
  • @quamrana but my code accesses the Player class? How does it still work out? – Deadmou5 Aug 28 '23 at 13:25

1 Answers1

0

I think you are doing it all wrong.

I think the root of the problem is in thinking that your Player is an instance, when it is a class which defines its attributes and initial values. (I suspect that pickle detects that Player is a class and saves and loads it as such).

See this code:

import os, pickle

FILE = 'PlayerPickle.pkl'

class Player:
    level = 1
    player_name = "bob"
    health = 100
    max_health = 100
    damage = 10
    gold = 400
    pet = ""
    experience = 0
    defense = 2
    weapons = ["Paper Sword"]
    items = []
    equipped_weapon = ("Rat")
    experience_threshhold = 10


def load(player):
    if os.path.isfile(FILE):
        return pickle.load(open(FILE,"rb")) #Loads the file
    return player

def save(player):
    pickle.dump(player, open(FILE, "wb",))

def main():
    player = Player()
    player = load(player)
    print(f'Initial Player name {player.player_name}, gold:{player.gold}')
    
    delta = int(input('Enter change in gold: '))
    
    player.gold += delta

    print(f'Current Player name {player.player_name}, gold:{player.gold}')
    
    save(player)

main()

I have used this code to modify the player.gold value and have it stored as a pickle file.

Each time I run the program I get the previous value, which I can update, and I see that new value on the next run.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • I did things exactly how you did, made a separate file, and it gives me: TypeError: askSave() missing 1 required positional argument: 'player' – Deadmou5 Aug 22 '23 at 15:19
  • But your `askSave()` function above takes no parameters. If you have some different code now you should ask a different question. – quamrana Aug 22 '23 at 15:21
  • @quamarana I setup my save system just like you. – Deadmou5 Aug 22 '23 at 15:23
  • It sounds like your new function takes the `player` parameter. You should call it like this: `askSave(player)` – quamrana Aug 22 '23 at 15:24
  • my Player class never gets updated with the player in your code? how would I add that? – Deadmou5 Aug 23 '23 at 14:20