def __init__(self, master): Window.__init__(self, master=master, bg_music=master.bg_music) self.bg = RectangleShape(*self.size, (0, 0, 0, 170)) self.frame = RectangleShape(0.50 * self.width, 0.50 * self.height, GREEN, outline=3) self.text = Text("Are you sure you want\nto buy this car ?", (RESOURCES.FONT["algerian"], 60), justify=Text.T_CENTER) self.button_yes = Button(self, "Yes", self.text.font, bg=GREEN, hover_bg=GREEN_LIGHT, active_bg=GREEN_DARK, hover_sound=RESOURCES.SFX["select"], on_click_sound=RESOURCES.SFX["validate"], highlight_color=YELLOW, callback=self.buy) self.button_red_cross = ImageButton( self, img=RESOURCES.IMG["red_cross"], active_img=RESOURCES.IMG["red_cross_hover"], hover_sound=RESOURCES.SFX["select"], on_click_sound=RESOURCES.SFX["back"], callback=self.stop, highlight_color=YELLOW) self.buyed = False self.bind_key(pygame.K_ESCAPE, lambda event: self.stop(sound=RESOURCES.SFX["back"])) self.bind_joystick( 0, "B", lambda event: self.stop(sound=RESOURCES.SFX["back"]))
class EnemyQuitGame(Window): def __init__(self, master): Window.__init__(self, master=master, bg_music=master.bg_music) self.bg = RectangleShape(self.width, self.height, (0, 0, 0, 170)) self.frame = RectangleShape(0.5 * self.width, 0.5 * self.height, GREEN_DARK, outline=2) self.text_finish = Text("The enemy has left\nthe game", font=(None, 70)) params_for_all_buttons = { "font": (None, 40), "bg": GREEN, "hover_bg": GREEN_LIGHT, "active_bg": GREEN_DARK, "highlight_color": YELLOW } self.button_return_to_menu = Button(self, "Return to menu", callback=self.stop, **params_for_all_buttons) self.bind_key(pygame.K_ESCAPE, lambda event: self.stop()) def place_objects(self): self.frame.center = self.center self.text_finish.center = self.frame.center self.button_return_to_menu.move(bottom=self.frame.bottom - 20, centerx=self.frame.centerx)
def __init__(self, master, *args, **kwargs): RectangleShape.__init__(self, *args, **kwargs) self.prog_name = Text(str(), (FONT["death_star"], 80), YELLOW, justify="center") self.prog_comment = Text(str(), (FONT["death_star"], 80), YELLOW, justify="center") self.infos_nb_selected = Text(f"0/{MAX_NB_PLAYERS}", (FONT["death_star"], 80), YELLOW) params_button = { "bg": BLUE, "hover_bg": BLUE_LIGHT, "active_bg": BLUE_DARK, "active_fg": YELLOW, "hover_sound": AUDIO["clank"], "on_click_sound": AUDIO["laser"], "outline": 5, } self.button_battle = Button(master, "Play", font=(FONT["death_star"], 80), **params_button, command=master.launch_game) self.default_width = 0
class EnemyQuitGame(Dialog): def __init__(self, master: Window): Dialog.__init__(self, master, bg_color=BLUE, outline=2, outline_color=WHITE) self.master = master self.bg = RectangleShape(self.width, self.height, (0, 0, 0, 170)) self.text = Text(font=RESOURCES.font("heavy", 50)) self.button_quit = Button(self, "Return to menu", theme=["section"], font=RESOURCES.font("heavy", 40), y_add_size=-25, callback=self.stop) self.objects.set_priority(self.bg, 0) self.master = master def on_quit(self) -> None: self.master.stop() def on_start_loop(self) -> None: self.button_quit.focus_set() def place_objects(self) -> None: self.text.center = self.frame.center self.button_quit.move(centerx=self.frame.centerx, bottom=self.frame.bottom - 10)
def __init__(self, master, victory: bool): Window.__init__( self, master=master, bg_music=None if victory is None else RESOURCES.MUSIC["end"]) self.master = master self.bg = RectangleShape(self.width, self.height, (0, 0, 0, 170)) self.frame = RectangleShape(0.5 * self.width, 0.5 * self.height, GREEN_DARK, outline=2) self.victory = victory if victory is not None: message = "{winner} won".format( winner="You" if victory else "Enemy") else: message = "The enemy has left\nthe game" self.text_finish = Text(message, font=(None, 70)) params_for_all_buttons = { "font": (None, 40), "bg": GREEN, "hover_bg": GREEN_LIGHT, "active_bg": GREEN_DARK, "highlight_color": YELLOW } self.button_restart = Button(self, "Restart", callback=self.restart, **params_for_all_buttons) self.button_return_to_menu = Button(self, "Return to menu", callback=self.stop, **params_for_all_buttons) self.ask_restart = False self.bind_key(pygame.K_ESCAPE, lambda event: self.stop())
def __init__(self, master): Window.__init__(self, master=master, bg_music=master.bg_music) self.bind_key(pygame.K_ESCAPE, lambda event: self.stop()) self.bind_joystick(0, "START", lambda event: self.stop()) self.bind_joystick(0, "B", lambda event: self.stop()) self.master = master self.bg = RectangleShape(*self.size, (0, 0, 0, 170)) params_for_all_buttons = { "font": (RESOURCES.FONT["algerian"], 100), "bg": GREEN, "hover_bg": GREEN_LIGHT, "active_bg": GREEN_DARK, "hover_sound": RESOURCES.SFX["select"], "on_click_sound": RESOURCES.SFX["validate"], "outline": 3, "highlight_color": YELLOW } self.menu_buttons = ButtonListVertical(offset=30) self.menu_buttons.add( Button(self, "Return", **params_for_all_buttons, callback=self.stop), Button(self, "Options", **params_for_all_buttons, callback=self.show_options), Button(self, "Garage", **params_for_all_buttons, callback=self.return_to_garage), Button(self, "Menu", **params_for_all_buttons, callback=self.return_to_menu))
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("Waiting for Player 2", font=("calibri", 50)) self.text_ip_address = Text(font=("calibri", 40)) self.text_port_of_connection = Text(font=("calibri", 40)) self.button_cancel = Button(self, "Return to menu", theme="option", callback=self.stop) self.lets_play_countdown = CountDown(self, 3, "Player 2 connected.\nGame start in {seconds} seconds", font=("calibri", 35), color=YELLOW, justify="center")
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): Window.__init__(self, master=master) self.master = master self.bg = RectangleShape(self.width, self.height, (0, 0, 0, 170)) self.frame = RectangleShape(0.5 * self.width, 0.5 * self.height, GREEN_DARK, outline=2) self.victory = None self.text_finish = Text(font=(None, 70)) self.button_restart = Button(self, "Restart", font=(None, 40), callback=self.restart) self.button_return_to_menu = Button(self, "Return to menu", font=(None, 40), callback=self.stop) self.ask_restart = False self.bind_key(pygame.K_ESCAPE, lambda event: self.stop())
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, player_id: int): Window.__init__(self, bg_color=(0, 200, 255), bg_music=RESOURCES.MUSIC["setup"]) self.gameplay = Gameplay(player_id) self.count_down = CountDown(self, 60, "Time left: {seconds}", font=(None, 70), color=WHITE) self.start_count_down = lambda: self.count_down.start( at_end=self.timeout) if self.client_socket.connected() else None params_for_all_buttons = { "bg": GREEN, "hover_bg": GREEN_LIGHT, "active_bg": GREEN_DARK, "highlight_color": YELLOW } self.button_back = ImageButton(self, RESOURCES.IMG["arrow_blue"], **params_for_all_buttons, rotate=180, size=50, callback=self.stop) self.navy_grid = DrawableListVertical(offset=0, bg_color=(0, 157, 255)) for i in range(NB_LINES_BOXES): box_line = DrawableListHorizontal(offset=0) for j in range(NB_COLUMNS_BOXES): box_line.add(BoxSetup(self, size=BOX_SIZE, pos=(i, j))) self.navy_grid.add(box_line) self.ships_list = DrawableListVertical(offset=70, justify="left") for ship_name, ship_infos in SHIPS.items(): ship_line = DrawableListHorizontal(offset=ship_infos["offset"]) for _ in range(ship_infos["nb"]): ship_line.add(ShipSetup(self, ship_name, ship_infos["size"])) self.ships_list.add(ship_line) option_size = 50 self.button_restart = Button.withImageOnly( self, Image(RESOURCES.IMG["reload_blue"], size=option_size), callback=self.reinit_all_ships, **params_for_all_buttons) self.button_random = Button.withImageOnly(self, Image( RESOURCES.IMG["random"], size=option_size), callback=self.shuffle, **params_for_all_buttons) self.button_play = Button(self, "Play", font=(None, 40), callback=self.play, **params_for_all_buttons)
def __init__(self, master): Window.__init__(self, bg_color=master.bg_color, bg_music=master.bg_music) params_for_button = { "highlight_color": YELLOW, "hover_sound": RESOURCES.SFX["select"], "on_click_sound": RESOURCES.SFX["back"] } self.master = master self.button_back = ImageButton(self, img=RESOURCES.IMG["blue_arrow"], **params_for_button, callback=self.stop) self.objects.add(master.text_highscore, master.text_money) self.text_title = Text("ENVIRONMENT", (RESOURCES.FONT["algerian"], 90), GREEN_DARK, shadow=True, shadow_x=2, shadow_y=2) self.environment = ButtonListHorizontal(offset=15) self.texts = DrawableList() for name, color in ENVIRONMENT.items(): b = Button(self, img=Image(RESOURCES.IMG[name], max_width=180, max_height=180), compound="center", outline=3, callback=lambda env=name: self.play(env), bg=color, hover_bg=change_saturation(color, -20), active_bg=change_brightness(color, -20), hover_sound=RESOURCES.SFX["select"], on_click_sound=RESOURCES.SFX["validate"], highlight_color=YELLOW) b.set_size(200) self.texts.add( Text(name.upper(), (RESOURCES.FONT["algerian"], 50), GREEN_DARK, shadow=True, shadow_x=2, shadow_y=2)) self.environment.add(b) self.bind_key(pygame.K_ESCAPE, lambda event: self.stop(sound=RESOURCES.SFX["back"])) self.bind_joystick( 0, "B", lambda event: self.stop(sound=RESOURCES.SFX["back"]))
def __init__(self, master, navy, size: tuple[int, int], pos: tuple[int, int]): params = { "size": size, "bg": TRANSPARENT, "hover_bg": GREEN, "active_bg": GREEN_DARK, "disabled_bg": TRANSPARENT, "disabled_hover_bg": RED, "disabled_active_bg": RED_DARK, "outline_color": WHITE, "highlight_color": WHITE, } Button.__init__(self, master=master, callback=lambda: master.hit_a_box(navy, self), **params) self.pos = pos
class ConfirmPayement(Window): def __init__(self, master): Window.__init__(self, master=master, bg_music=master.bg_music) self.bg = RectangleShape(*self.size, (0, 0, 0, 170)) self.frame = RectangleShape(0.50 * self.width, 0.50 * self.height, GREEN, outline=3) self.text = Text("Are you sure you want\nto buy this car ?", (RESOURCES.FONT["algerian"], 60), justify=Text.T_CENTER) self.button_yes = Button(self, "Yes", self.text.font, bg=GREEN, hover_bg=GREEN_LIGHT, active_bg=GREEN_DARK, hover_sound=RESOURCES.SFX["select"], on_click_sound=RESOURCES.SFX["validate"], highlight_color=YELLOW, callback=self.buy) self.button_red_cross = ImageButton( self, img=RESOURCES.IMG["red_cross"], active_img=RESOURCES.IMG["red_cross_hover"], hover_sound=RESOURCES.SFX["select"], on_click_sound=RESOURCES.SFX["back"], callback=self.stop, highlight_color=YELLOW) self.buyed = False self.bind_key(pygame.K_ESCAPE, lambda event: self.stop(sound=RESOURCES.SFX["back"])) self.bind_joystick( 0, "B", lambda event: self.stop(sound=RESOURCES.SFX["back"])) def place_objects(self): self.text.center = self.frame.center = self.center self.button_red_cross.move(left=self.frame.left + 5, top=self.frame.top + 5) self.button_yes.move(bottom=self.frame.bottom - 10, centerx=self.frame.centerx) def set_grid(self): self.button_red_cross.set_obj_on_side(on_bottom=self.button_yes) self.button_yes.set_obj_on_side(on_top=self.button_red_cross) def buy(self): self.buyed = True self.stop()
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, size: tuple[int, int], pos: tuple[int, int]): params = { "size": size, "bg": TRANSPARENT, "hover_bg": GREEN, "disabled_bg": TRANSPARENT, "disabled_hover_bg": RED, "outline_color": WHITE, "highlight_color": WHITE, } Button.__init__(self, master=master, **params) self.disable_key_joy() self.disable_mouse() self.pos = pos self.ship = None
class PlayerServer(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, "outline": 3, "highlight_color": YELLOW } self.start_game = master.start_game self.text_title = Text("Waiting for Player 2", ("calibri", 50)) self.text_ip_address = Text(font=("calibri", 40)) self.text_port_of_connection = Text(font=("calibri", 40)) self.button_cancel = Button(self, "Return to menu", callback=self.stop, **params_for_all_buttons) self.lets_play_countdown = CountDown(self, 3, "Player 2 connected.\nGame start in {seconds} seconds", font=("calibri", 35), color=YELLOW, justify="center") def on_dialog_start_loop(self): try: ip, port = self.create_server(12800, 1) self.text_ip_address.message = f"IP address: {ip}" self.text_port_of_connection.message = f"Port: {port}" except OSError: self.stop() def on_dialog_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.text_ip_address.move(centerx=self.centerx, bottom=self.frame.centery - 10) self.text_port_of_connection.move(centerx=self.text_ip_address.centerx, top=self.text_ip_address.bottom + 20) self.button_cancel.move(centerx=self.frame.centerx, bottom=self.frame.bottom - 10) def update(self) -> None: if self.get_server_clients_count() > 1 and not self.lets_play_countdown.started(): self.set_server_listen(0) self.text_title.hide() self.button_cancel.state = Button.DISABLED self.lets_play_countdown.start(at_end=self.play) def play(self): self.start_game.mainloop() self.stop()
class NavyWindow(Window): def __init__(self): Window.__init__(self, size=(1280, 720), flags=pygame.DOUBLEBUF, bg_music=RESOURCES.MUSIC["menu"]) self.set_icon(RESOURCES.IMG["icon"]) self.set_title(f"Navy - v{__version__}") self.set_fps(60) self.disable_key_joy_focus_for_all_window() self.bg = Image(RESOURCES.IMG["menu_bg"], self.size) self.logo = Image(RESOURCES.IMG["logo"]) params_for_all_buttons = { "font": (None, 100), "bg": GREEN, "hover_bg": GREEN_LIGHT, "active_bg": GREEN_DARK, "outline": 3, "highlight_color": YELLOW, } params_for_dialogs = { "outline": 5, "hide_all_without": [self.bg, self.logo] } self.start_game = NavySetup(1) self.multiplayer_server = PlayerServer(self, **params_for_dialogs) self.multiplayer_client = PlayerClient(self, **params_for_dialogs) self.dialog_credits = Credits(self, **params_for_dialogs) self.dialog_options = Options(self, **params_for_dialogs) self.menu_buttons = ButtonListVertical(offset=30) self.menu_buttons.add( Button(self, "Play against AI", **params_for_all_buttons, callback=self.start_game.mainloop), Button(self, "Play as P1", **params_for_all_buttons, callback=self.multiplayer_server.mainloop), Button(self, "Play as P2", **params_for_all_buttons, callback=self.multiplayer_client.mainloop), Button(self, "Quit", **params_for_all_buttons, callback=self.stop) ) self.button_credits = Button(self, "Credits", callback=self.dialog_credits.mainloop, **params_for_all_buttons) self.button_settings = Button.withImageOnly(self, Image(RESOURCES.IMG["settings"], size=self.button_credits.height - 20), callback=self.dialog_options.mainloop, **params_for_all_buttons) def place_objects(self): self.bg.center = self.center self.logo.centerx = self.centerx self.menu_buttons.move(centerx=self.centerx, bottom=self.bottom - 20) self.button_settings.move(left=10, bottom=self.bottom - 10) self.button_credits.move(right=self.right - 10, bottom=self.bottom - 10)
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 __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, "outline": 3, "highlight_color": YELLOW } self.start_game = master.start_game self.text_title = Text("Waiting for Player 2", ("calibri", 50)) self.text_ip_address = Text(font=("calibri", 40)) self.text_port_of_connection = Text(font=("calibri", 40)) self.button_cancel = Button(self, "Return to menu", callback=self.stop, **params_for_all_buttons) self.lets_play_countdown = CountDown(self, 3, "Player 2 connected.\nGame start in {seconds} seconds", font=("calibri", 35), color=YELLOW, justify="center")
def __init__(self, master: Window): Window.__init__(self, bg_color=BACKGROUND_COLOR) self.bind_key(pygame.K_ESCAPE, lambda event: self.stop()) self.master = master self.logo = Image(RESOURCES.IMG["logo"]) arrow = pygame.transform.flip(RESOURCES.IMG["arrow"], True, False) self.button_back = ImageButton(self, img=arrow, width=100, callback=self.stop, active_offset=(0, 5), highlight_color=YELLOW) self.grid = FourInARowGrid(self, self.width / 2, self.height * 0.75) self.__player_turn = 0 self.player_who_start_first = 0 self.player = 0 self.__turn = str() self.__turn_dict = dict() self.__score_player = self.__score_enemy = 0 self.__highlight_line_window_callback = None self.enemy = str() self.ai = FourInARowAI() self.text_score = Text() self.text_player_turn = Text() self.left_options = ButtonListVertical(offset=50) self.left_options.add( Button(self, "Restart", theme="option", callback=self.restart), Button(self, "Quit game", theme="option", callback=self.quit_game)) self.text_winner = Text() self.text_drawn_match = Text("Drawn match.") self.token_players = Grid(self) for row in range(2): self.token_players.place(CircleShape(20, PLAYER_TOKEN_COLOR[row + 1], outline=2, outline_color=WHITE), row, column=1, padx=5, pady=5, justify="left") self.enemy_quit_dialog = EnemyQuitGame(self)
def __init__(self, master: Window): Dialog.__init__(self, master, bg_color=BLUE, outline=2, outline_color=WHITE) self.master = master self.bg = RectangleShape(self.width, self.height, (0, 0, 0, 170)) self.text = Text(font=RESOURCES.font("heavy", 50)) self.button_quit = Button(self, "Return to menu", theme=["section"], font=RESOURCES.font("heavy", 40), y_add_size=-25, callback=self.stop) self.objects.set_priority(self.bg, 0) self.master = master
class Options(Dialog): def __init__(self, master: Window, **kwargs): Dialog.__init__(self, master=master, bg_color=GREEN_DARK, **kwargs) self.text_title = Text("Options", ("calibri", 50)) params_for_all_scales = { "width": 0.45 * self.frame.w, "height": 30, "color": TRANSPARENT, "scale_color": GREEN, "from_": 0, "to": 100, "highlight_color": YELLOW, "outline": 3 } params_for_all_buttons = { "font": ("calibri", 30), "bg": GREEN, "hover_bg": GREEN_LIGHT, "active_bg": GREEN_DARK, "highlight_color": params_for_all_scales["highlight_color"], "outline": params_for_all_scales["outline"] } self.scale_music = Scale( self, **params_for_all_scales, default=Window.music_volume() * 100, callback=lambda value, percent: Window.set_music_volume(percent) ) self.scale_music.show_label("Music: ", Scale.S_LEFT, font=params_for_all_buttons["font"]) self.scale_music.show_value(Scale.S_RIGHT, font=params_for_all_buttons["font"]) self.scale_sound = Scale( self, **params_for_all_scales, default=Window.sound_volume() * 100, callback=lambda value, percent: Window.set_sound_volume(percent) ) self.scale_sound.show_label("SFX: ", Scale.S_LEFT, font=params_for_all_buttons["font"]) self.scale_sound.show_value(Scale.S_RIGHT, font=params_for_all_buttons["font"]) self.button_cancel = Button(self, "Return to menu", callback=self.stop, **params_for_all_buttons) def place_objects(self): self.text_title.move(centerx=self.frame.centerx, top=self.frame.top + 50) self.scale_music.move(centerx=self.frame.centerx, bottom=self.frame.centery - 20) self.scale_sound.move(centerx=self.frame.centerx, top=self.frame.centery + 20) self.button_cancel.move(centerx=self.frame.centerx, bottom=self.frame.bottom - 10)
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")
class GameMenu(Window): def __init__(self, master: Window): Window.__init__(self) self.bg = master.bg self.title = Text("Game", font=(FONT["death_star"], 220), color=YELLOW) params_for_all_buttons = { "bg": BLUE, "hover_bg": BLUE_LIGHT, "active_bg": BLUE_DARK, "active_fg": YELLOW, "hover_sound": AUDIO["clank"], "on_click_sound": AUDIO["laser"], "outline": 5, } params_for_section_button = { "font": (FONT["death_star"], 200) } self.button_battle = Button(self, "Battle", **params_for_all_buttons, **params_for_section_button, command=self.launch_battle) self.button_edit = Button(self, "Editor", **params_for_all_buttons, **params_for_section_button, command=self.launch_editor) self.button_menu = Button(self, "Menu", **params_for_all_buttons, font=(FONT["death_star"], 80), command=self.stop) def place_objects(self): self.title.move(centerx=self.centerx, top=10) self.button_battle.move(centerx=self.centerx - (self.w // 4), centery=self.centery) self.button_edit.move(centerx=self.centerx + (self.w // 4), centery=self.centery) self.button_menu.move(centerx=self.centerx, bottom=self.bottom - 100) def launch_editor(self): Editor(self).mainloop() def launch_battle(self): Battle(self).mainloop()
def __init__(self, master, gameplay: FourInARowGameplay): Section.__init__(self, master, "Choose AI level", gameplay) self.buttons_ai_level = ButtonListVertical(offset=100) theme = {"font": self.title.font, "y_add_size": -30} disabled_levels = [FourInARowAI.HARD] self.buttons_ai_level.add_multiple( Button(self, text=string.capwords(level), theme=["section"], **theme, callback=lambda ai_level=level: self.play(ai_level), state=Button.DISABLED if level in disabled_levels else Button.NORMAL) for level in FourInARowAI.get_available_levels())
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()
def __init__(self, master: Window): Window.__init__(self) self.bg = master.bg self.title = Text("Game", font=(FONT["death_star"], 220), color=YELLOW) params_for_all_buttons = { "bg": BLUE, "hover_bg": BLUE_LIGHT, "active_bg": BLUE_DARK, "active_fg": YELLOW, "hover_sound": AUDIO["clank"], "on_click_sound": AUDIO["laser"], "outline": 5, } params_for_section_button = { "font": (FONT["death_star"], 200) } self.button_battle = Button(self, "Battle", **params_for_all_buttons, **params_for_section_button, command=self.launch_battle) self.button_edit = Button(self, "Editor", **params_for_all_buttons, **params_for_section_button, command=self.launch_editor) self.button_menu = Button(self, "Menu", **params_for_all_buttons, font=(FONT["death_star"], 80), command=self.stop)
def __init__(self): Window.__init__(self, bg_color=(0, 200, 255)) self.gameplay = Gameplay() self.enemy_quit_window = EnemyQuitGame(self) self.transition = GameSetupTransition() self.count_down = CountDown(self, 60, "Time left: {seconds}", font=(None, 70), color=WHITE) self.start_count_down = lambda: self.count_down.start( at_end=self.timeout) if self.client_socket.connected() else None self.button_back = ImageButton(self, RESOURCES.IMG["arrow_blue"], rotate=180, size=50, callback=self.stop) self.navy_grid = Grid(self, bg_color=(0, 157, 255)) self.__boxes_dict = {(i, j): BoxSetup(self, size=BOX_SIZE, pos=(i, j)) for i in range(NB_LINES_BOXES) for j in range(NB_COLUMNS_BOXES)} self.__boxes_list = list(self.__boxes_dict.values()) self.navy_grid.place_multiple(self.__boxes_dict) self.ships_list = DrawableListVertical(offset=70, justify="left") for ship_name, ship_infos in SHIPS.items(): ship_line = DrawableListHorizontal(offset=ship_infos["offset"]) for _ in range(ship_infos["nb"]): ship_line.add(ShipSetup(self, ship_name, ship_infos["size"])) self.ships_list.add(ship_line) option_size = 50 self.button_restart = Button(self, img=Image(RESOURCES.IMG["reload_blue"], size=option_size), callback=self.reinit_all_ships) self.button_random = Button(self, img=Image(RESOURCES.IMG["random"], size=option_size), callback=self.shuffle) self.button_play = Button(self, "Play", font=(None, 40), callback=self.play)