Guest User

Games Comp

a guest
Apr 18th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. inter as tk
  2. from tkinter import messagebox
  3.  
  4. file_path = r'C:\Users\adria\Desktop\Python Games\Games Comp.txt'
  5. game_data = []
  6.  
  7. root = tk.Tk()
  8. root.title("Games Competition")
  9. root.geometry("800x600")  # Set window size to 800x600 pixels
  10.  
  11. root.option_add("*Font", "Helvetica 14")
  12. root.option_add("*Entry.Width", 100)
  13.  
  14. label = tk.Label(root, text="Games Competition", font=("Helvetica", 30))
  15. label.pack(pady=50)
  16.  
  17. def load_data(file_path):
  18.     data = []
  19.     try:
  20.         with open(file_path, 'r') as file:
  21.             for line in file:
  22.                 data.append(line.strip().split(','))
  23.     except FileNotFoundError:
  24.         pass
  25.     return data
  26.  
  27.  
  28. def save_data(file_path, new_data):
  29.     existing_data = load_data(file_path)
  30.     all_data = existing_data + new_data
  31.     with open(file_path, 'w') as file:
  32.         for entry in all_data:
  33.             entry_str = [str(item) for item in entry]
  34.             file.write(','.join(entry_str) + '\n')
  35.  
  36.  
  37. class NewGameDialog(tk.Toplevel):
  38.     def __init__(self, parent, game_data):
  39.         super().__init__(parent)
  40.         self.title("New Game")
  41.         self.geometry("400x200+400+300")  # Set the initial size and position of the window
  42.         self.game_data = game_data
  43.         self.game_name = tk.StringVar()
  44.         self.players = ['Cameron', 'Adrian', 'Ryan', 'Liam']
  45.         self.current_player_index = 0
  46.         self.player_points = [5, 3, 2, 1] #Define player points here
  47.         self.create_game_name_entry()
  48.  
  49.     def create_game_name_entry(self):
  50.         self.game_name_label = tk.Label(self, text="Which game would you like to play?")
  51.         self.game_name_label.pack()
  52.         self.game_name_entry = tk.Entry(self, textvariable=self.game_name, font=("Helvetica", 16), width=30)
  53.         self.game_name_entry.pack()
  54.  
  55.         self.game_name_entry.bind("<Return>", self.handle_game_name_entry)
  56.  
  57.     def handle_game_name_entry(self, event):
  58.         self.game_name_label.destroy()
  59.         self.game_name_entry.destroy()
  60.  
  61.         self.position_label = tk.Label(self, text=f"Which position did {self.players[self.current_player_index]} come? (1, 2, 3, 4)")
  62.         self.position_label.pack()
  63.         self.position_entry = tk.Entry(self, font=("Helvetica", 14), width=100)
  64.         self.position_entry.pack()
  65.  
  66.         self.position_entry.bind("<Return>", self.handle_return)
  67.  
  68.     def handle_return(self, event):
  69.         position = self.position_entry.get()
  70.         try:
  71.             position = int(position)
  72.             if position < 1 or position > 4:
  73.                 raise ValueError
  74.         except ValueError:
  75.             messagebox.showerror("Error", "Please enter a valid position (1, 2, 3, 4)")
  76.             return
  77.  
  78.         game_name = self.game_name.get()
  79.         player = self.players[self.current_player_index]
  80.  
  81.         print(f"Processing {player} for {game_name} at position {position}")
  82.  
  83.         existing_entry_index = None
  84.         for i, entry in enumerate(self.game_data):
  85.             if entry[0] == game_name and entry[1] == player:
  86.                 existing_entry_index = i
  87.                 print(f"Existing entry found for {player} in {game_name} at index {existing_entry_index}")
  88.                 break
  89.  
  90.         if existing_entry_index is not None:
  91.             self.game_data[existing_entry_index][2] = position
  92.             self.game_data[existing_entry_index][3] = str(self.player_points[position - 1])
  93.             print("Updated existing entry")
  94.         else:
  95.             self.game_data.append([game_name, player, position, str(self.player_points[position - 1])])
  96.             print("No existing entry found, adding new entry")
  97.  
  98.         print("Current game data:")
  99.         for entry in self.game_data:
  100.             print(entry)
  101.  
  102.         save_data(file_path, self.game_data)
  103.  
  104.         self.current_player_index += 1
  105.         if self.current_player_index < len(self.players):
  106.             self.position_label.config(
  107.                 text=f"Which position did {self.players[self.current_player_index]} come? (1, 2, 3, 4)")
  108.             self.position_entry.delete(0, tk.END)
  109.         else:
  110.             self.destroy()
  111.  
  112.  
  113. def new_game():
  114.     NewGameDialog(root, game_data)
  115.  
  116.  
  117. if __name__ == "__main__":
  118.     load_data(file_path)
  119.  
  120.     new_game_btn = tk.Button(root, text="New Game", width=30, height=3, font=("Helvetica", 16), borderwidth=5,
  121.                               highlightbackground="black", command=new_game)
  122.     new_game_btn.pack()
  123.  
  124.     root.mainloop()
Add Comment
Please, Sign In to add comment