import tkinter as tk from sys import exit root = tk.Tk() class Framework: def build(self): self.foundation = tk.Frame(root) self.foundation.pack() self.tasks = [] self.keybinds = [] self.blueprint() def start(self): self.run = True while self.run == True: for item in self.tasks: item() root.update() root.update_idletasks() def stop(self): self.run = False def demolish(self): for item in self.keybinds: root.unbind_all(item) self.foundation.destroy() del self class Menu(Framework): def blueprint(self): self.window_x = 500 self.Window_y = 250 root.minsize(self.window_x,self.Window_y) root.maxsize(self.window_x,self.Window_y) self.foundation.configure(width = self.window_x, height = self.Window_y, bg = "#000") self.lable = tk.Label(self.foundation, bg = "#000", fg = "#FFF", text = "Py-Pong!", font = ("Lucida Console", 30)) self.exit_button = tk.Button(self.foundation, bg = "#FFF", fg = "#000", text = "Exit", width = 20, height = 1, font = ("Lucida Console", 10), command = Terminate) self.play_button = tk.Button(self.foundation, bg = "#FFF", fg = "#000", text = "Play", width = 20, height = 1, font = ("Lucida Console", 10), command = self.Callback_A) self.lable.place(anchor = "n", x = 250, y = 10) self.exit_button.place(anchor = "s", x = 250, y = 240) self.play_button.place(anchor = "s", x = 250, y = 200) def Callback_A(self): Interface_Manager(1) class Game(Framework): def blueprint(self): self.window_x = 750 self.window_y = 500 root.minsize(self.window_x,self.window_y) root.maxsize(self.window_x,self.window_y) root.bind("", self.shift_l_pressed) root.bind("", self.ctrl_l_pressed) root.bind("", self.shift_l_released) root.bind("", self.ctrl_l_released) root.bind("", self.shift_r_pressed) root.bind("", self.ctrl_r_pressed) root.bind("", self.shift_r_released) root.bind("", self.ctrl_r_released) self.keybinds = ["","","""","","","",""] self.foundation.configure(width = self.window_x, height = self.window_y, bg = "#000") self.board = tk.Canvas(self.foundation, width = self.window_x, height = self.window_y, bg = "#000", bd = 0) self.board.create_line(self.window_x/2,0,self.window_x/2,500, fill = "#FFF") self.score_A = tk.Label(self.board, bg = "#000", fg = "#FFF", text = "0", font = ("Lucida Console", 30)) self.score_B = tk.Label(self.board, bg = "#000", fg = "#FFF", text = "0", font = ("Lucida Console", 30)) self.paddle_A = self.board.create_rectangle(25,200,35,300, fill = "#FFF") self.paddle_B = self.board.create_rectangle(715,200,725,300, fill = "#FFF") self.ball = self.board.create_rectangle(self.window_x/2 - 10, self.window_y/2 - 10 , self.window_x/2 + 10, self.window_y/2 + 10, fill = "#FFF") self.board.pack() self.score_A.place(anchor = "ne", x = self.window_x/2 - 10, y = 10) self.score_B.place(anchor = "nw", x = self.window_x/2 + 10, y = 10) self.shift_l = False self.ctrl_l = False self.shift_r = False self.ctrl_r = False self.tasks.append(self.event_loop) def event_loop(self): if self.shift_l and not self.ctrl_l and int(self.board.coords(self.paddle_A)[1]) > 0: self.board.move(self.paddle_A,0,-0.1) if self.ctrl_l and not self.shift_l and int(self.board.coords(self.paddle_A)[3]) < self.window_y: self.board.move(self.paddle_A,0,0.1) if self.shift_r and not self.ctrl_r and int(self.board.coords(self.paddle_B)[1]) > 0: self.board.move(self.paddle_B,0,-0.1) if self.ctrl_r and not self.shift_r and int(self.board.coords(self.paddle_B)[3]) < self.window_y: self.board.move(self.paddle_B,0,0.1) def shift_l_pressed(self, event): self.shift_l = True def shift_l_released(self, event): self.shift_l = False def ctrl_l_pressed(self, event): self.ctrl_l = True def ctrl_l_released(self, event): self.ctrl_l = False def shift_r_pressed(self, event): self.shift_r = True def shift_r_released(self, event): self.shift_r = False def ctrl_r_pressed(self, event): self.ctrl_r = True def ctrl_r_released(self, event): self.ctrl_r = False Interfaces = [Menu,Game] current_interface = None def Interface_Manager(Queue): global current_interface current_interface.stop() current_interface.demolish() current_interface = Interfaces[Queue]() current_interface.build() current_interface.start() def Terminate(): current_interface.stop() current_interface.demolish() exit() current_interface = Interfaces[0]() current_interface.build() current_interface.start()