class PlayerClient(Dialog): def __init__(self, master, **kwargs): Dialog.__init__(self, master=master, bg_color=GREEN_DARK, **kwargs) params_for_all_buttons = { "font": ("calibri", 30), "bg": GREEN, "hover_bg": GREEN_LIGHT, "active_bg": GREEN_DARK, "highlight_color": YELLOW, "outline": 3 } self.start_game = NavySetup(2) self.text_title = Text("Connect to Player 1", ("calibri", 50)) self.ip = Entry(self, width=15, font=("calibri", 40), bg=GREEN, highlight_color=YELLOW, outline=2) self.text_ip_address = Text("IP address", ("calibri", 40), YELLOW) self.port = Entry(self, width=15, font=("calibri", 40), bg=GREEN, highlight_color=YELLOW, outline=2) self.text_port_of_connection = Text("Port", ("calibri", 40), YELLOW) self.text_connection = Text(font=("calibri", 25), color=YELLOW) self.text_connection.hide() self.button_connect = Button(self, "Connection", callback=self.connection, **params_for_all_buttons) self.button_cancel = Button(self, "Return to menu", callback=self.stop, **params_for_all_buttons) self.lets_play_countdown = CountDown(self, 3, "Connected.\nGame start in {seconds} seconds", font=("calibri", 35), color=YELLOW, justify="center") def on_dialog_quit(self): self.disable_text_input() self.stop_connection() def place_objects(self): self.frame.move(center=self.center) self.text_title.move(centerx=self.frame.centerx, top=self.frame.top + 50) self.lets_play_countdown.move(center=self.text_title.center) self.ip.move(centerx=self.frame.centerx + self.frame.w // 10, bottom=self.frame.centery - 10) self.text_ip_address.move(centery=self.ip.centery, right=self.ip.left - 10) self.port.move(left=self.ip.left, top=self.ip.bottom + 20) self.text_port_of_connection.move(centery=self.port.centery, right=self.port.left - 10) self.text_connection.move(centerx=self.frame.centerx, top=self.port.bottom + 5) self.button_connect.move(centerx=self.frame.centerx - (self.frame.width // 4), bottom=self.frame.bottom - 10) self.button_cancel.move(centerx=self.frame.centerx + (self.frame.width // 4), bottom=self.frame.bottom - 10) def connection(self): self.text_connection.show() self.text_connection.message = "Connection..." self.draw_and_refresh() if not self.connect_to_server(self.ip.get(), int(self.port.get()), 3): self.text_connection.message = "Connection failed. Try again." else: self.text_connection.hide() self.text_title.hide() self.button_connect.state = self.button_cancel.state = Button.DISABLED self.button_connect.focus_leave() self.lets_play_countdown.start(at_end=self.play) def play(self): self.start_game.mainloop() self.stop()
class PlayerClient(Dialog): def __init__(self, master, **kwargs): Dialog.__init__(self, master=master, bg_color=GREEN_DARK, **kwargs) self.start_game = master.start_game self.text_title = Text("Connect to Player 1", font=("calibri", 50)) self.form = Form(self) self.form.add_entry("IP", Text("IP address", font=("calibri", 40), color=YELLOW), Entry(self, width=15, font=("calibri", 30), bg=GREEN, highlight_color=YELLOW, outline=2)) self.form.add_entry("Port", Text("Port", font=("calibri", 40), color=YELLOW), Entry(self, width=15, font=("calibri", 30), bg=GREEN, highlight_color=YELLOW, outline=2)) self.text_connection = Text(font=("calibri", 25), color=YELLOW) self.text_connection.hide() self.button_connect = Button(self, "Connection", theme="option", callback=self.connection) self.button_cancel = Button(self, "Return to menu", theme="option", callback=self.stop) self.lets_play_countdown = CountDown(self, 3, "Connected.\nGame start in {seconds} seconds", font=("calibri", 35), color=YELLOW, justify="center") def on_quit(self): self.stop_connection() def place_objects(self): self.frame.move(center=self.center) self.text_title.move(centerx=self.frame.centerx, top=self.frame.top + 50) self.lets_play_countdown.move(center=self.text_title.center) self.form.move(center=self.frame.center) self.text_connection.move(centerx=self.frame.centerx, top=self.form.bottom + 5) self.button_connect.move(centerx=self.frame.centerx - (self.frame.width // 4), bottom=self.frame.bottom - 10) self.button_cancel.move(centerx=self.frame.centerx + (self.frame.width // 4), bottom=self.frame.bottom - 10) def connection(self): self.text_connection.show() self.text_connection.message = "Connection..." self.draw_and_refresh() try: address = self.form.get("IP") port = int(self.form.get("Port")) except ValueError: self.text_connection.message = "The port of connection must be a number." return if not self.connect_to_server(address, port, 3): self.text_connection.message = "Connection failed. Try again." else: self.text_connection.hide() self.text_title.hide() self.button_connect.state = self.button_cancel.state = Button.DISABLED self.button_connect.focus_leave() self.lets_play_countdown.start(at_end=self.play) def play(self): self.start_game.start(2) self.stop()