Exemplo n.º 1
0
    def new_game(self):
        """Reset the GameWindow and start a new game, and build gui"""
        # Attempt to destroy the ROOT window if it exists.
        try:
            self.ROOT.destroy()
        except Exception as e:
            pass

        # Initialize a new Bletchley Game.
        self.game = BletchleyGame()

        # Initialize the Tunes class.
        self.tunes = music.Tunes(available_songs)

        # Initialize the dictionary that stores the user's guesses per-row.
        self.button_rows = {
            i: [None] * 4 for i in range(6)
        }

        # Construct the main window.
        self.ROOT = Tk()
        self.ROOT.title(game_title)
        self.ROOT.geometry("252x576")
        self.ROOT.resizable(False, False)

        # Construct the menu bar.
        self.MENU_BAR = Menu(self.ROOT)

        # Construct the main menu.
        self.MAIN_MENU = Menu(self.MENU_BAR, tearoff=0)
        self.MENU_BAR.add_cascade(label="Menu", menu=self.MAIN_MENU)
        self.MAIN_MENU.add_command(label="New Game", command=self.new_game)

        self.MAIN_MENU.add_command(
            label="How To play",
            command=partial(
                showinfo,
                "How To play",
                "Click on the blue pegs to change their colours.\nClick the green "  \
                "DECODE button to see if you cracked the code.\n\n"  \
                "Clues will be given on the yellow label about "  \
                "each colour you chose.\n\n"
                "'*' means correct colour and correct position.\n"
                "'X' means you have the colour correct, but in the wrong position.\n"
                "'0000' means you have no colours at all in the hidden code.\n\n"
                "Try to work out what the secret code is within six attempts to win."
                "\nYou can give in by clicking on the reveal button at any time.",
            ),
        )


        self.MAIN_MENU.add_command(
            label="Visit Blog",
            command=partial(
                self.open_browser, "https://stevepython.wordpress.com/"
            ),
        )
        self.MAIN_MENU.add_command(
            label="About",
            command=partial(
                showinfo,
                "About",
                "Bletchley V2.15r by Steve Shambles Feb 2019",
            ),
        )
        self.MAIN_MENU.add_command(label="Exit", command=self.QUIT)


        # Construct the music menu.
        self.MUSIC_MENU = Menu(self.MENU_BAR, tearoff=0)
        self.MENU_BAR.add_cascade(label="Music", menu=self.MUSIC_MENU)
        # Add the menu items for each of the available songs.
        for song_name, song_file in self.tunes.track_list():
            self.MUSIC_MENU.add_command(
                # Define the menu item label.
                label="Play {}".format(song_name),
                # Call the function to play the appropriate song.
                command=partial(self.tunes.play_track, song_file),
            )
        self.MUSIC_MENU.add_separator()
        self.MUSIC_MENU.add_command(
            label="Stop music", command=self.tunes.stop_music
        )
        self.MUSIC_MENU.add_command(
            label="Free music from Bensound.com",
            command=partial(self.open_browser, "https://bensound.com/"),
        )

        # Add stats menu
        self.STATS_MENU = Menu(self.MENU_BAR, tearoff=0)
        self.MENU_BAR.add_cascade(label="Stats", menu=self.STATS_MENU)
        self.STATS_MENU.add_command(
            label="View Your Stats", command=self.view_stats
        )
        self.STATS_MENU.add_command(
            label="Reset Your Stats", command=self.reset_stats
        )


        # Add the menu bar to the ROOT window.
        self.ROOT.config(menu=self.MENU_BAR)

        # Create frame for the image
        FRAME0 = LabelFrame(self.ROOT)
        FRAME0.grid(padx=18, pady=18)

        if not os.path.isfile("blt-panel-v2.png"):
            messagebox.showinfo("Player Stats",  \
            "The file 'blt-panel-v2.png' is missing\n"  \
            "from the current directory. Please replace it.")
            self.QUIT()

        IMAGE = Image.open('blt-panel-v2.png')
        PHOTO = ImageTk.PhotoImage(IMAGE)
        LABEL = Label(FRAME0, image=PHOTO)
        LABEL.IMAGE = PHOTO
        LABEL.grid(padx=2, pady=2)


        # Create the lists and dict that will store the various elements of
        # each row.
        self.BUTTON_FRAMES = list()
        self.BUTTON_FRAME_LABELS = list()
        self.DECODE_BUTTONS = list()
        self.BUTTON_GUESSES = dict()
        # Construct each row.
        for row_index in range(6):
            # Create the frame.
            BUTTON_FRAME = LabelFrame(
                self.ROOT,
                fg="blue",
                text="Attempt {}".format(row_index + 1),
                relief=SUNKEN
            )
            BUTTON_FRAME.grid()
            # Add the frame to the list.
            self.BUTTON_FRAMES.append(BUTTON_FRAME)

            # Create the empty buttons list.
            BUTTON_GUESS_LIST = [None] * 4
            for button_index in range(4):
                # Craft all four buttons.
                BUTTON = Button(
                    BUTTON_FRAME,
                    bg="skyblue",
                    text=" ",
                    command=partial(
                        self.click_button, row_index, button_index
                    ),
                )
                BUTTON.grid(
                    row=(7 + row_index), column=button_index, pady=4, padx=4
                )
                BUTTON_GUESS_LIST[button_index] = BUTTON
            # Assign the button list to the button dictionary.
            self.BUTTON_GUESSES[row_index] = BUTTON_GUESS_LIST[:]

            # Create the solution frame's label.
            FRAME_LABEL = Label(BUTTON_FRAME, bg="yellow", text="        ")
            FRAME_LABEL.grid(row=(7 + row_index), column=4, pady=4, padx=4)
            # Append the label to the label list.
            self.BUTTON_FRAME_LABELS.append(FRAME_LABEL)

            # Create the decode button.
            DECODE_BUTTON = Button(
                BUTTON_FRAME,
                bg="green2",
                text="DECODE",
                command=partial(self.decode_row, row_index),
            )
            DECODE_BUTTON.grid(row=(7 + row_index), column=5, pady=4, padx=4)
            # Append the decode button to the list.
            self.DECODE_BUTTONS.append(DECODE_BUTTON)

        # Make sure player can only decode row 1 to start with
        # by disabling all other decode buttons.
        for index in range(1, 6):
            self.DECODE_BUTTONS[index].configure(state=DISABLED)

        # cover up secret code with a button.
        self.SOLUTION_FRAME = LabelFrame(
            self.ROOT, fg="blue", text="Solution", relief=SUNKEN, padx=4, pady=6
        )
        self.SOLUTION_FRAME.grid()
        self.SOLUTION_BUTTON = Button(
            self.SOLUTION_FRAME,
            bg="gold",
            text="      REVEAL SECRET CODE      ",
            command=self.reveal_solution,
        )
        self.SOLUTION_BUTTON.grid(row=9, column=5, pady=4, padx=4)

        # Run the main loop.
        self.centre_gui()
        self.run()