Advertisement
here2share

# tk_games_generic.py

Mar 24th, 2023
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.39 KB | None | 0 0
  1. # tk_games_generic.py
  2.  
  3. import tkinter as tk
  4. import random
  5.  
  6. # Define game mechanics and objectives
  7. class Game:
  8.     def __init__(self):
  9.         self.score = 0
  10.         self.tasks = []
  11.         self.rules = []
  12.         self.task_labels = []
  13.         self.time_left = 300 # 5 minutes in seconds
  14.         self.players = [] # List of Player objects
  15.         self.turn = 0 # Index of current player's turn
  16.         self.mode = "solo" # Game mode ("solo" or "multiplayer")
  17.         self.game_over = False
  18.  
  19.     def generate_rules(self):
  20.         self.rules = [ # Randomly generated rules based on game mechanics
  21.             "Score 10 points for each completed task before time runs out.",
  22.             "Add 5 seconds to the timer for each task completed within 30 seconds.",
  23.             "Tasks can only be completed in alphabetical order.",
  24.             "Choose a teammate to switch scores with.",
  25.             # ...
  26.         ]
  27.         random.shuffle(self.rules)
  28.  
  29.     def generate_tasks(self):
  30.         self.score = 0
  31.         self.tasks = [ # Randomly generated tasks based on game mechanics
  32.             "Frobnicate the widget",
  33.             "Buff the banana",
  34.             "Groom the cat",
  35.             "Polish the trombone",
  36.             # ...
  37.         ]
  38.         random.shuffle(self.tasks)
  39.  
  40.     def update_task_labels(self):
  41.         for i, task_label in enumerate(self.task_labels):
  42.             task_label.configure(text=f"{chr(65+i)}. {self.tasks[i]}")
  43.  
  44.     def play(self):
  45.         while self.time_left > 0 and not self.game_over:
  46.             # Update game timer
  47.             self.time_left -= 1
  48.  
  49.             # Update score label
  50.             self.score_label.configure(text="Score: " + str(self.score))
  51.  
  52.             # Check for task completion
  53.             for i, task in enumerate(self.tasks):
  54.                 if task == "": # Skip completed tasks
  55.                     continue
  56.  
  57.                 if chr(65+i) in self.completed_keys:
  58.                     self.tasks[i] = "" # Mark task as complete
  59.                     self.score += 10 # Add points to score
  60.                     if self.time_left > 30: # Add time bonus if more than 30 seconds left
  61.                         self.time_left += 5
  62.  
  63.             # Update task labels
  64.             self.update_task_labels()
  65.  
  66.             # Check for game over
  67.             if not any(self.tasks) or self.time_left <= 0:
  68.                 self.end_game()
  69.  
  70.             # Check for rule changes
  71.             if self.time_left % 60 == 0:
  72.                 self.generate_rules()
  73.                 self.rule_label.configure(text=self.rules[0])
  74.  
  75.             # AI player turn
  76.             if self.mode == "solo" and self.turn != 0 and not self.game_over:
  77.                 self.play_ai()
  78.  
  79.     def play_ai(self):
  80.         ai_player = self.players[self.turn]
  81.         completed_icons = set([chr(65+i) for i, task in enumerate(self.tasks) if task == ""])
  82.         possible_moves = list(completed_icons - ai_player.completed_tasks)
  83.         if possible_moves:
  84.             ai_player.completed_tasks.add(random.choice(possible_moves))
  85.             self.completed_keys.add(random.choice(possible_moves))
  86.         else:
  87.             self.turn = (self.turn + 1) % len(self.players)
  88.  
  89.     def end_game(self):
  90.         self.game_over = True
  91.         if self.mode == "solo":
  92.             self.rule_label.configure(text="Game over! Final score: " + str(self.score))
  93.         else:
  94.             self.rule_label.configure(text="Game over! Winner: " + self.players[0].name)
  95.  
  96.         for player in self.players:
  97.             player.score = self.score - sum([len(task) * 10 for task in player.completed_tasks])
  98.             player.end_game()
  99.  
  100. # Define player object
  101. class Player:
  102.     def __init__(self, name, score=0):
  103.         self.name = name
  104.         self.score = score
  105.         self.completed_tasks = set()
  106.  
  107.     def end_game(self):
  108.         # Update display with final scores, etc.
  109.         pass
  110.  
  111. # Set up tkinter interface
  112. class Application:
  113.     def __init__(self, master=None):
  114.         self.master = master
  115.         self.game = Game()
  116.         self.completed_keys = set()
  117.         self.player_entry = None
  118.         self.player_name = None
  119.         self.socket = None
  120.         self.lobby_text = None
  121.  
  122.     def create_widgets(self):
  123.         # Create title label
  124.         self.title_label = tk.Label(self.master, text="My Strategic Game!")
  125.         self.title_label.pack()
  126.  
  127.         # Create rules label
  128.         self.rule_label = tk.Label(self.master, text="")
  129.         self.rule_label.pack()
  130.         self.game.generate_rules()
  131.         self.rule_label.configure(text=self.game.rules[0])
  132.  
  133.         # Create task labels
  134.         for i in range(4):
  135.             task_label = tk.Label(self.master, text="")
  136.             task_label.pack()
  137.             self.game.task_labels.append(task_label)
  138.         self.game.generate_tasks()
  139.         self.game.update_task_labels()
  140.  
  141.         # Create score label
  142.         self.score_label = tk.Label(self.master, text="Score: " + str(self.game.score))
  143.         self.score_label.pack()
  144.  
  145.         # Create timer label
  146.         self.time_label = tk.Label(self.master, text="Time: " + str(self.game.time_left))
  147.         self.time_label.pack()
  148.  
  149.         # Create player entry form
  150.         self.player_entry = tk.Entry(self.master)
  151.         self.player_entry.pack()
  152.         self.player_entry.focus_set()
  153.  
  154.         # Create player name label
  155.         self.player_name = tk.Label(self.master, text="")
  156.         self.player_name.pack()
  157.  
  158.         # Create lobby text widget
  159.         self.lobby_text = tk.Text(self.master, height=10, state=tk.DISABLED)
  160.         self.lobby_text.pack()
  161.  
  162.         # Create start button
  163.         self.start_button = tk.Button(self.master, text="Start", command=self.start_game)
  164.         self.start_button.pack()
  165.  
  166.     def connect(self):
  167.         # Connect players to game
  168.         pass
  169.  
  170.     def start_game(self):
  171.         # Get player name from entry form
  172.         name = self.player_entry.get().strip()
  173.         if name:
  174.             self.player_name.configure(text="Player name: " + name)
  175.  
  176.         # Start playing game
  177.         self.game.mode = "solo" # For now, only have a solo mode
  178.         self.game.players.append(Player(name))
  179.         self.game.play()
  180.  
  181.     def on_mouse_click(self, event):
  182.         for i, task_label in enumerate(self.game.task_labels):
  183.             if task_label == event.widget:
  184.                 self.completed_keys.add(chr(65+i))
  185.  
  186.     def on_keypress(self, event):
  187.         # Handle key presses
  188.         pass
  189.  
  190.     def update_time_label(self):
  191.         # Update time label with time remaining
  192.         self.time_label.configure(text="Time: " + str(self.game.time_left))
  193.         self.master.after(1000, self.update_time_label) # Update once per second
  194.  
  195.     def update_lobby_text(self, message):
  196.         self.lobby_text.configure(state=tk.NORMAL)
  197.         self.lobby_text.insert(tk.END, message + "\n")
  198.         self.lobby_text.configure(state=tk.DISABLED)
  199.  
  200.     def start_server(self):
  201.         # Start server and listen for clients
  202.         pass
  203.  
  204.     def join_lobby(self):
  205.         # Join an existing lobby on a remote server
  206.         pass
  207.  
  208. # Run application
  209. root = tk.Tk()
  210. app = Application(master=root)
  211. app.create_widgets()
  212. app.start_server() # Start server for multiplayer games
  213. app.join_lobby() # Join a multiplayer game lobby
  214. app.start_timer()
  215. for task_label in app.game.task_labels:
  216.     task_label.bind("<Button-1>", app.on_mouse_click) # Bind mouse events
  217. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement