importing os and json
*os helps with finding file paths
*json allows saving and loading data in JSON format.
save_location = os.path.join(os.path.expanduser("~"), "Desktop", "save_file.json")
^- creates save_file.json on desktop
def save_game(): data = {"player_gold": player_gold}
Creates a dictionary with the players gold, and stores the gold amount.
directory = os.path.dirname(save_location) if not os.path.exists(directory): os.makedirs(directory)
^- checks if directory exists, if it does not. It'll create it.
with open(save_location, "w") as file: json.dump(data, file) print("Game has been successfully saved.")
^- Opens save_file.json "w" means write mode json.dump(data, file) Saves gold as JSON.
try: if os.path.exists(save_location):
^- Checks if save_file.json exists. If it don't it loads save gold.
with open(save_location, "r") as file: data = json.load(file) player_gold = data.get("player_gold", 0) print("Game has successfully loaded.")
^-
Opens save file in READ MODE "r", Reads the JSON data and gets the variables 'player_gold'
except FileNotFoundError: print("No save file has been found.")
^- If an error happens, it'll catch it and not CAUSE A CRASH.
Create a Save File
import os
import json
player_gold = 0
save_location = os.path.join(os.path.expanduser("~"), "Desktop", "save_file.json")
def save_game():
data = {"player_gold": player_gold}
directory = os.path.dirname(save_location)
if not os.path.exists(directory):
os.makedirs(directory)
with open(save_location, "w") as file:
json.dump(data, file)
print("Game has been successfully saved.")
def load_game():
global player_gold
try:
if os.path.exists(save_location):
with open(save_location, "r") as file:
data = json.load(file)
player_gold = data.get("player_gold", 0)
print("Game has successfully loaded.")
else:
print("No save file found, creating new save.")
save_game()
except FileNotFoundError:
print("No save file has been found.")
def main():
global player_gold
print(f"You have {player_gold} Gold!")
input("Press any key to quit.")
save_game()
exit()
main()
import tkinter as tk
^- Imports tkinter and renames it to tk. Tkinter is used to create GUI Applications (like Windows / buttons / Etc.)
root = tk.Tk()
^- Creates the main window (root) where everything will be displayed.
root.title("Window Name")
^- The name of the Window
root.geometry("640x480")
^- The size of our Window
root.resizable(False, False)
^- Disables resizing Window.
button = tk.Button(root, text ="CLICK ME!", bg="green" width=20, height=5, relief=tk.RAISED, fg="black", font=("Arial", 13, "bold"))
^- Creates button that says CLICK ME! / bg="green" Button background color / relief=tk.RAISED (Button is clickable) / fg="black" color of TEXT
button.place(x=0, y=0)
^- Location of the BUTTON.
root.mainloop()
^- Allows the Window to run Infinitely.
Create a BUTTON
import tkinter as tk
root = tk.Tk()
root.title("Button")
root.geometry("640x480")
root.resizable(False, False)
button = tk.Button(root, text="CLICK ME!", bg="green", width=20, height=5, relief=tk.RAISED, fg="black", font=("Arial", 13, "bold"))
button.place(x=225, y=125)
root.mainloop()
Create a message popup when closing Window
import tkinter as tk
from tkinter import ttk, messagebox
root = tk.Tk()
root.title("Close Me for a Message")
root.geometry("600x350")
root.resizable(width=False, height=False)
def on_close():
messagebox.showinfo("Andrew Says", "see you!")
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_close)
root.mainloop()
#When closing the window, a Popup box will appear saying:
TITLE: Andrew Says
Text Inside Popup Box: see you!