An Adventure Game GUI In Python

PHOTO EMBED

Tue Jul 18 2023 12:08:20 GMT+0000 (Coordinated Universal Time)

Saved by @pythonHub #python #python-hub #day17

from customtkinter import *
from PIL import Image

class adventure_game(CTk):
    def __init__(self):
        super().__init__()

        self.images = [CTkImage(light_image=Image.open("Timages\\two roads.jpg"), size=(364,456)),
                       CTkImage(light_image=Image.open("Timages\\island.jpg"),size=(402,226)),
                       CTkImage(light_image=Image.open("Timages\\hole.jpg"),size=(595,396)),
                       CTkImage(light_image=Image.open("Timages\\Three doors.png"),size=(420,306)),
                       CTkImage(light_image=Image.open("Timages\\shark.jpg"),size=(400,266)),
                       CTkImage(light_image=Image.open("Timages\\fire.jpg"),size=(589,396)),
                       CTkImage(light_image=Image.open("Timages\\wolves.jpg"),size=(600,400)),
                    ]

        self.first_label = CTkLabel(self, text="You're at a cross road. Where do you want to go? Type \"left\" or \"right\"", font=("Arial", 16))
        self.first_label.pack()
        self.first_image = CTkLabel(self, image=self.images[0], text="")
        self.first_image.pack()

        self.choice1 = CTkEntry(self)
        self.choice1.pack()

        self.confirm = CTkButton(self, text="Confirm", command=self.choice_one)
        self.confirm.pack()

    def choice_one(self):
        ch1 = self.choice1.get()
        # Clear the app window
        for widget in self.winfo_children():
            widget.destroy()
        if ch1 == "left":
            self.second_label = CTkLabel(self, text="You've come to a lake. There is an island in the middle of the lake. Type \"wait\" to wait for a boat. Type \"swim\" to swim across.", font=("Arial", 16))
            self.second_label.pack()
            self.second_image = CTkLabel(self, image=self.images[1], text="")
            self.second_image.pack()

            self.choice2 = CTkEntry(self)
            self.choice2.pack()

            self.confirm = CTkButton(self, text="Confirm",command=self.choice_two)
            self.confirm.pack()
        else:
            self.second_label = CTkLabel(self, text="You fell into a hole. Game Over.", font=("Arial", 16))
            self.second_label.pack()
            self.second_image = CTkLabel(self, image=self.images[2], text="")
            self.second_image.pack()

            # exit button
            self.qb = CTkButton(self, text="Exit", fg_color="red", hover_color="pink", text_color="white", command=self.quit)
            self.qb.pack()

    def choice_two(self):
        ch2 = self.choice2.get()
        # Clear the app window
        for widget in self.winfo_children():
            widget.destroy()
        if ch2 == "wait":
            self.third_label = CTkLabel(self, text="You arrive at the island unharmed. There is a house with 3 doors. One red, one yellow and one blue. Which colour do you choose?", font=("Arial", 16))
            self.third_label.pack()
            self.third_image = CTkLabel(self, image=self.images[3], text="")
            self.third_image.pack()

            self.choice3 = CTkEntry(self)
            self.choice3.pack()

            self.confirm = CTkButton(self, text="Confirm",command=self.choice_three)
            self.confirm.pack()
        else:
            self.third_label = CTkLabel(self, text="You get attacked by an angry trout. Game Over.", font=("Arial", 16))
            self.third_label.pack()
            self.third_image = CTkLabel(self, image=self.images[4], text="")
            self.third_image.pack()

            # exit button
            self.qb = CTkButton(self, text="Exit", fg_color="red", hover_color="pink", text_color="white", command=self.quit)
            self.qb.pack()

    def choice_three(self):
        ch3 = self.choice3.get()
        # Clear the app window
        for widget in self.winfo_children():
            widget.destroy()

        if ch3=="yellow":
            self.fourth_label = CTkLabel(self, text="You win\nHere's the treasure:-\nIt's a comic website.\n-----------Happy Reading-----------")
            self.fourth_label.pack()
            import antigravity
            from time import sleep
            sleep(1)
            self.quit()

        elif ch3 == "red":
            self.fourth_label = CTkLabel(self, text="It's a room full of fire. Game Over.", font=("Arial", 16))
            self.fourth_label.pack()
            self.fourth_image = CTkLabel(self, image=self.images[5], text="")
            self.fourth_image.pack()

            # exit button
            self.qb = CTkButton(self, text="Exit", fg_color="red", hover_color="pink", text_color="white", command=self.quit)
            self.qb.pack()

        elif ch3 == "blue":
            self.fourth_label = CTkLabel(self, text="You enter a room of beasts. Game Over.", font=("Arial", 16))
            self.fourth_label.pack()
            self.fourth_image = CTkLabel(self, image=self.images[6], text="")
            self.fourth_image.pack()

            # exit button
            self.qb = CTkButton(self, text="Exit", fg_color="red", hover_color="pink", text_color="white", command=self.quit)
            self.qb.pack()
            
        else:
            self.fourth_label = CTkLabel(self, text="Game Over.", font=("Arial", 16))
            self.fourth_label.pack()
            # exit button
            self.qb = CTkButton(self, text="Exit", fg_color="red", hover_color="pink", text_color="white", command=self.quit)
            self.qb.pack()


app = adventure_game()
app.mainloop()
content_copyCOPY

This has a very bad coding style...