def __init__(self, master, gameplay: FourInARowGameplay): Section.__init__(self, master, "Local Multiplaying", gameplay) self.form = Form(self) self.form.add_entry("P1", Text("P1 Name:", theme="form"), Entry(self)) self.form.add_entry("P2", Text("P2 Name:", theme="form"), Entry(self)) self.button_play = Button(self, "Play", theme=["option", "section"], callback=self.play)
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 __init__(self, master, gameplay: FourInARowGameplay): Section.__init__(self, master, "Play as P1", gameplay) self.form = Form(self) self.form.add_entry("P1", Text("Your Name:", theme="form"), Entry(self)) self.button_start_server = Button(self, theme=["option", "section"]) self.text_game_status = Text(font=RESOURCES.font("heavy", 20), shadow_x=1, shadow_y=1) self.text_server_ip = Text(font=RESOURCES.font("afterglow", 45), shadow_x=2, shadow_y=1) self.text_server_port = Text(font=RESOURCES.font("afterglow", 45), shadow_x=2, shadow_y=1)
def __init__(self, master, gameplay: FourInARowGameplay): Section.__init__(self, master, "Play as P2", gameplay) self.form = Form(self) self.form.add_entry("name", Text("Your Name:", theme="form"), Entry(self)) self.form.add_entry("IP", Text("IP address:", theme="form"), Entry(self, width=15)) self.form.add_entry("port", Text("Port:", theme="form"), Entry(self, width=15)) self.button_connect = Button(self, "Connect to P1", theme=["option", "section"], callback=self.connection) self.text_game_status = Text(font=RESOURCES.font("heavy", 30), shadow_x=1, shadow_y=1)
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()
class LocalPlayingSection(Section): def __init__(self, master, gameplay: FourInARowGameplay): Section.__init__(self, master, "Local Multiplaying", gameplay) self.form = Form(self) self.form.add_entry("P1", Text("P1 Name:", theme="form"), Entry(self)) self.form.add_entry("P2", Text("P2 Name:", theme="form"), Entry(self)) self.button_play = Button(self, "Play", theme=["option", "section"], callback=self.play) def on_start_loop(self) -> None: super().on_start_loop() self.button_play.focus_set() def place_objects(self) -> None: Section.place_objects(self) self.form.move(left=self.frame.left + 10, top=self.title.bottom + 50) self.button_play.move(centerx=self.frame.centerx, bottom=self.frame.bottom - 10) def set_grid(self) -> None: self.button_back.set_obj_on_side(on_bottom=self.form[0], on_right=self.form[0]) self.form.set_obj_on_side(on_top=self.button_back, on_left=self.button_back, on_bottom=self.button_play) self.button_play.set_obj_on_side(on_top=self.form[-1]) def play(self) -> None: player_1_name = self.form.get("P1") or None player_2_name = self.form.get("P2") or None self.gameplay.start(LOCAL_PLAYER, player_name=player_1_name, enemy_name=player_2_name) self.stop()
class LANPlayingP2(Section): def __init__(self, master, gameplay: FourInARowGameplay): Section.__init__(self, master, "Play as P2", gameplay) self.form = Form(self) self.form.add_entry("name", Text("Your Name:", theme="form"), Entry(self)) self.form.add_entry("IP", Text("IP address:", theme="form"), Entry(self, width=15)) self.form.add_entry("port", Text("Port:", theme="form"), Entry(self, width=15)) self.button_connect = Button(self, "Connect to P1", theme=["option", "section"], callback=self.connection) self.text_game_status = Text(font=RESOURCES.font("heavy", 30), shadow_x=1, shadow_y=1) def place_objects(self) -> None: Section.place_objects(self) self.form.move(left=self.frame.left + 10, top=self.title.bottom + 50) self.text_game_status.move(centerx=self.frame.centerx, top=self.form.bottom + 10) self.button_connect.move(centerx=self.frame.centerx, bottom=self.frame.bottom - 10) def update(self) -> None: self.button_connect.state = Button.NORMAL if self.form.get( "name") else Button.DISABLED def on_start_loop(self) -> None: super().on_start_loop() self.form.get_entry("name").focus_set() self.form.get_entry("name").start_edit() self.text_game_status.hide() def on_quit(self) -> None: super().on_quit() self.stop_connection() def set_grid(self) -> None: self.button_back.set_obj_on_side(on_bottom=self.form[0], on_right=self.form[0]) self.form.set_obj_on_side(on_top=self.button_back, on_left=self.button_back, on_bottom=self.button_connect) self.button_connect.set_obj_on_side(on_top=self.form[-1]) def connection(self) -> None: self.text_game_status.show() self.text_game_status.message = "Connection..." self.draw_and_refresh() try: address = self.form.get("IP") port = int(self.form.get("port")) except ValueError: self.text_game_status.message = "The port of connection must be a number." return if not self.connect_to_server(address, port, 3): self.text_game_status.message = "Connection failed. Try again." else: self.gameplay.start(LAN_PLAYER, player=2, player_name=self.form.get("name")) self.stop()
class LANPlayingP1(Section): def __init__(self, master, gameplay: FourInARowGameplay): Section.__init__(self, master, "Play as P1", gameplay) self.form = Form(self) self.form.add_entry("P1", Text("Your Name:", theme="form"), Entry(self)) self.button_start_server = Button(self, theme=["option", "section"]) self.text_game_status = Text(font=RESOURCES.font("heavy", 20), shadow_x=1, shadow_y=1) self.text_server_ip = Text(font=RESOURCES.font("afterglow", 45), shadow_x=2, shadow_y=1) self.text_server_port = Text(font=RESOURCES.font("afterglow", 45), shadow_x=2, shadow_y=1) def place_objects(self) -> None: Section.place_objects(self) self.form.move(left=self.frame.left + 10, top=self.title.bottom + 50) self.text_game_status.move(centerx=self.frame.centerx, top=self.form.bottom + 10) self.button_start_server.move(centerx=self.frame.centerx, top=self.form.bottom + 50) self.text_server_ip.move(centerx=self.frame.centerx, top=self.button_start_server.bottom + 30) self.text_server_port.move(centerx=self.frame.centerx, top=self.text_server_ip.bottom + 10) def update(self) -> None: if not self.client_socket.connected(): self.button_start_server.state = Button.NORMAL if self.form.get( "P1") else Button.DISABLED self.form.get_entry("P1").state = Entry.NORMAL else: self.button_start_server.state = Button.NORMAL self.form.get_entry("P1").state = Entry.DISABLED if self.get_server_clients_count() == 2: self.set_server_listen(0) self.gameplay.start(LAN_PLAYER, player=1, player_name=self.form.get("P1")) self.stop() def on_start_loop(self) -> None: super().on_start_loop() self.form.get_entry("P1").focus_set() self.form.get_entry("P1").start_edit() self.stop_server() def on_quit(self) -> None: super().on_quit() self.stop_connection() def set_grid(self) -> None: self.button_back.set_obj_on_side(on_bottom=self.form[0], on_right=self.form[0]) self.form.set_obj_on_side(on_top=self.button_back, on_left=self.button_back, on_bottom=self.button_start_server) self.button_start_server.set_obj_on_side(on_top=self.form[-1], on_left=self.button_back) def start_server(self) -> None: try: ip, port = self.create_server(12800, 1) except OSError: self.text_game_status.message = "Cannot create server" else: self.text_game_status.message = "Waiting for player 2 to connect" self.text_server_ip.message = "IP: {}".format(ip) self.text_server_port.message = "Port: {}".format(port) self.text_server_ip.show() self.text_server_port.show() self.button_start_server.text = "Stop server" self.button_start_server.callback = self.stop_server self.text_game_status.show() self.place_objects() def stop_server(self) -> None: self.stop_connection() self.text_game_status.hide() self.text_server_ip.hide() self.text_server_port.hide() self.button_start_server.text = "Start server" self.button_start_server.callback = self.start_server