Esempio n. 1
0
class Varos(object):
    def __init__(self, parent, master, empire, matrozokszama=5):
        self.matrozokszama = IntVar()
        self.matrozokszama.set(matrozokszama)
        self.boss = parent
        self.master = master
        self.empire = empire

    @property
    def nev(self):
        return self.empire.capital

    @property
    def zaszlo(self):
        return self.empire.adjective

    def aktival(self):
        "Működteti a kikötőt."
        self.letrehoz()

    def letrehoz(self):
        self.ablak = Toplevel()
        if self.nev == 'portroyal':
            self.ablak.title((s.language.port + ' - Port Royal'))
        else:
            self.ablak.title((s.language.port, '-', self.nev.capitalize()))
        self.ablak.transient(self.master)
        self.ablak.grab_set()
        self.ujMatrozok(
        )  # A játékos belépésekor a kocka által mutatott számot hozzáadjuk a helyi matrózok létszámához.
        self.tevekenysegek = Frame(
            self.ablak)  # Főkeret: tartalma panelek és gombok
        self.tevekenysegek.pack(side=TOP, ipadx=5)
        # A kép panel
        self.kep = Label(self.tevekenysegek, image=Gallery.get(self.nev))
        self.kep.pack(side=LEFT, pady=5, padx=5, fill=Y)
        # A fogadó panel
        self.fogado = LabelFrame(self.tevekenysegek, text=s.language.tavern)
        line1 = Frame(self.fogado)  # a bérelhető létszám
        Label(line1, text=(s.language.sailors_to_hire + ':')).pack(side=LEFT)
        self.matrozokszama_kiirva = Label(
            line1, textvariable=self.matrozokszama).pack(side=RIGHT)
        line1.pack(side=TOP, fill=X)
        line2 = Frame(self.fogado)  # legénység / hajó max. kapacitás
        Label(line2, text=(s.language.crew + ':')).pack(side=LEFT)
        Label(line2,
              textvariable=self.boss.aktivjatekos.crew_limit).pack(side=RIGHT)
        Label(line2, text='/').pack(side=RIGHT)
        Label(line2, textvariable=self.boss.aktivjatekos.crew).pack(side=RIGHT)
        line2.pack(side=TOP, fill=X)
        Separator(self.fogado, orient=HORIZONTAL).pack(side=TOP,
                                                       fill=X,
                                                       pady=5,
                                                       padx=5)
        line3 = Frame(self.fogado)  # a skála címe
        szoveg = s.language.crew_new
        szoveg = szoveg + ' ' * (33 - len(szoveg))
        Label(line3, text=szoveg).pack(side=LEFT)
        line3.pack(side=TOP, fill=X)
        self.line4 = Frame(self.fogado)  # a skála
        self.berskala = Scale(self.line4)
        self.line4.pack(side=TOP, fill=X)
        self.line5 = Frame(
            self.fogado)  # a skálán beállított értéket érvényesítő gomb
        self.skalaCimke = Label(self.line5)
        self.felberel = Button(self.line5,
                               text=s.language.crew_hire,
                               command=self.matrozFelberelese)
        self.felberel.pack(side=RIGHT, padx=5, pady=5)
        self.line5.pack(side=TOP, fill=X)
        self.fogado.pack(side=LEFT, pady=5, padx=5, fill=Y)
        # A hajóács panel
        self.hajoacs = LabelFrame(self.tevekenysegek,
                                  text=s.language.shipwright)
        self.hajoacs_lekepez()
        self.hajoacs.pack(side=LEFT, fill=Y, pady=5)
        # A kormányzó panel
        pontok = 0
        kormanyzo_mondja = StringVar()
        for pontforras in self.boss.aktivjatekos.scores.keys():
            pontok += self.boss.aktivjatekos.scores[pontforras].get()
        self.kormanyzo = LabelFrame(self.tevekenysegek,
                                    text=s.language.governor)
        if self.empire == 'pirate':
            elsullyesztettHelyiHajok = 0  # A kalózok nem birodalom, nem büntetnek az elsüllyedt kalózhajókért
        else:
            elsullyesztettHelyiHajok = self.boss.aktivjatekos.scores[
                self.empire.adjective].get()
        if elsullyesztettHelyiHajok > 0:
            kormanyzo_mondja.set(s.language.governor_punish %
                                 elsullyesztettHelyiHajok)
            self.boss.aktivjatekos.update_turns_to_miss(
                elsullyesztettHelyiHajok)
            self.boss.aktivjatekos.scores[self.empire.adjective].set(0)
        else:
            maxJutalom = self.jutalomszamolo() * 8
            kormanyzo_mondja.set(s.language.governor_reward % maxJutalom)
            self.boss.aktivjatekos.update_gold(maxJutalom)
            self.penzszamolo()
            for birodalom in self.boss.aktivjatekos.scores.keys():
                self.boss.aktivjatekos.scores[birodalom].set(0)
        Label(self.kormanyzo, wraplength=125,
              textvariable=kormanyzo_mondja).pack(side=LEFT)
        if self.empire != 'pirate' and pontok > 0:
            self.kormanyzo.pack(side=LEFT, pady=5, padx=5, fill=Y)
        # Gombok
        Button(self.ablak, text=s.language.done,
               command=self.ablak.destroy).pack(side=BOTTOM, pady=5)
        self.ablak.update_idletasks()
        w, h = self.ablak.winfo_width(), self.ablak.winfo_height()
        bx, by = self.master.get_window_position()
        bh, bw = self.master.height, self.master.width
        self.ablak.geometry('+' + str(int(bx + (bw + (bh / 3) - w) / 2)) +
                            '+' + str(int(by + (bh - h) / 2)))
        self.master.wait_window(self.ablak)

    def hajoacs_lekepez(self):
        "A hajóácspanel."
        self.hajoframek = {}
        self.hajogombok = {}
        for hajo in self.boss.vehetoHajok:
            self.hajoframek[hajo] = Frame(self.hajoacs)
            self.hajogombok[hajo] = Button(
                self.hajoframek[hajo],
                image=Gallery.get(hajo),
                command=lambda hajo=hajo: self.ujHajo(hajo))
            self.hajogombok[hajo].pack(side=LEFT)
            if self.boss.aktivjatekos.ship in self.boss.vehetoHajok:
                if self.boss.vehetoHajok.index(
                        self.boss.aktivjatekos.ship
                ) < self.boss.vehetoHajok.index(hajo):
                    ar = self.boss.hajotipustar[
                        hajo].price - self.boss.hajotipustar[
                            self.boss.aktivjatekos.ship].price
                    Label(self.hajoframek[hajo],
                          text='%s: %i %s' %
                          (s.language.price, ar, s.language.gold)).pack(
                              side=LEFT, fill=X)
                else:
                    Label(self.hajoframek[hajo],
                          text=s.language.already_bought).pack(side=LEFT,
                                                               fill=X)
            else:
                Label(self.hajoframek[hajo],
                      text='%s: %i %s' %
                      (s.language.price, self.boss.hajotipustar[hajo].price,
                       s.language.gold)).pack(side=LEFT, fill=X)
            self.hajoframek[hajo].pack(side=TOP, pady=5, padx=5, fill=X)
        self.penzszamolo()
        self.hajoacs.pack(fill=Y, pady=5)

    def jutalomszamolo(self):
        "Megmutatja, mennyi jutalmat vehet át a játékos legfeljebb."
        scores = self.boss.aktivjatekos.scores
        pontszam = 0
        for birodalom in scores.keys():
            helyiPontszam = scores[birodalom].get()
            if helyiPontszam / 5 > 0:
                pontszam += int(helyiPontszam / 5) * 7
                helyiPontszam = helyiPontszam % 5
            if helyiPontszam / 3 > 0:
                pontszam += int(helyiPontszam / 3) * 4
                helyiPontszam = helyiPontszam % 3
            pontszam += helyiPontszam
        return pontszam

    def penzszamolo(self):
        "A játékos anyagi lehetőségeinek fényében engedélyezi a hajók vásárlását."
        current_ship = self.boss.aktivjatekos.ship
        if current_ship in self.boss.vehetoHajok:
            for hajo in self.boss.vehetoHajok:
                price = self.boss.hajotipustar[hajo].price
                if hajo == current_ship:
                    self.hajogombok[hajo].config(state=DISABLED)
                elif price < self.boss.hajotipustar[current_ship].price:
                    self.hajogombok[hajo].config(state=DISABLED)
                elif price - self.boss.hajotipustar[
                        current_ship].price > self.boss.aktivjatekos.gold.get(
                        ):
                    self.hajogombok[hajo].config(state=DISABLED)
                else:
                    self.hajogombok[hajo].config(state=NORMAL)
        else:
            for hajo in self.boss.vehetoHajok:
                if self.boss.hajotipustar[
                        hajo].price > self.boss.aktivjatekos.gold.get():
                    self.hajogombok[hajo].config(state=DISABLED)
                else:
                    self.hajogombok[hajo].config(state=NORMAL)
        self.berskalat_letrehoz()

    def berskalat_letrehoz(self):
        "Létrehozza a skálát, a felbérelendő matrózok számának kijelöléséhez."
        crew_limit = self.boss.hajotipustar[
            self.boss.aktivjatekos.
            ship].crew_limit - self.boss.aktivjatekos.crew.get()
        locally_available_sailors = self.matrozokszama.get()
        berskalahossz = min(crew_limit, locally_available_sailors,
                            self.boss.aktivjatekos.gold.get())
        self.berskala.destroy()
        self.skalaCimke.destroy()
        if not berskalahossz:
            if self.boss.hajotipustar[
                    self.boss.aktivjatekos.
                    ship].crew_limit - self.boss.aktivjatekos.crew.get() == 0:
                visszajelzes = s.language.crew_ship_full
            elif self.matrozokszama.get() == 0:
                visszajelzes = s.language.crew_port_empty
            else:
                visszajelzes = s.language.crew_no_money
            self.berskala = Label(self.line4, text=visszajelzes)
            self.felberel.config(state=DISABLED)
        else:
            self.berskala = Scale(self.line4,
                                  from_=0,
                                  to=berskalahossz,
                                  orient=HORIZONTAL,
                                  resolution=1,
                                  takefocus=0,
                                  showvalue=0,
                                  command=self.berskalaErtek)
            self.skalaCimke = Label(self.line5)
            self.skalaCimke.pack()
            self.felberel.config(state=NORMAL)
        self.berskala.pack(side=TOP, fill=X)

    def berskalaErtek(self, event):
        self.skalaCimke.config(text=str(self.berskala.get()))

    def ujMatrozok(self):
        self.matrozokszama.set(self.matrozokszama.get() +
                               self.boss.boss.menu.game_tab.die._current_value)

    def matrozFelberelese(self):
        "Lebonyolítja a metrózok felbérelésével járó tranzakciót"
        delta = self.berskala.get()
        if not delta:
            return
        else:
            self.boss.aktivjatekos.update_crew(delta)
            self.boss.aktivjatekos.update_gold(-delta)
            self.matrozokszama.set(self.matrozokszama.get() - delta)
            self.berskalat_letrehoz()
        self.penzszamolo()

    def ujHajo(self, tipus=''):
        "Lebonyolítja az új hajó vásárlásával járó tranzakciót"
        ar = self.boss.hajotipustar[tipus].price - self.boss.hajotipustar[
            self.boss.aktivjatekos.ship].price
        self.boss.aktivjatekos.update_ship(tipus)
        self.boss.aktivjatekos.update_gold(-ar)
        self.boss.aktivjatekos.crew_limit.set(
            self.boss.hajotipustar[tipus].crew_limit)
        for hajo in self.boss.vehetoHajok:
            self.hajoframek[hajo].destroy()
            self.hajogombok[hajo].destroy()
        self.hajoacs_lekepez()

    def export_matroz(self):
        "Átadja saját adatait a mentéshez."
        return self.matrozokszama.get()
Esempio n. 2
0
class Window:
    def __init__(self):
        self.root = Tk()
        self.root.title("Image Processing")
        self.image_frame = Frame(self.root)
        self.canvas = Canvas(self.image_frame)
        self.canvas.pack()
        self.options_frame = Frame(self.root)
        self.options_frame.pack(side='top')
        self.image_frame.pack()
        self.image = None
        self.image_processor = ImageProcessor
        self.tkImage = None
        self.mode = 'none'
        self.current_option = "Reset Image"
        self.create_menu_bar()
        self.root.after(100, self.run_top_level_windows)
        self.root.mainloop()

    def run_top_level_windows(self):
        """
        Creates top level window which describes the application's function to the user.
        :return: None
        """
        self.root.withdraw()
        top = Toplevel()
        top.title("About image processing application.")
        message = Message(
            top,
            text=
            "This application is used to process image files.  Begin by hitting Open Image "
            "under Options on the title bar and entering the name of a file stored in the "
            "images folder.",
            width=300)
        message.pack()

        def button_hit():
            """
            Actions for when the continue button on the top level window is hit.
            :return: None
            """
            top.destroy()
            self.root.deiconify()
            w, h = self.root.winfo_screenwidth(), self.root.winfo_screenheight(
            )
            self.root.geometry("%dx%d+0+0" % (w, h))

        button = Button(top, text='continue', command=button_hit)
        button.pack()

    def create_menu_bar(self):
        """
        Adds items to the title bar at the top left of the screen. The user can use these to switch between different
        functions of the application or to exit.
        :return: None
        """
        self.menubar = Menu(self.root)

        # create a pulldown menu, and add it to the menu bar
        self.option_menu = Menu(self.menubar, tearoff=0)
        self.option_menu.add_command(label="Open Image",
                                     command=self.open_image_option)
        self.option_menu.add_command(label="Image Processing",
                                     command=self.image_processing_option)
        self.option_menu.add_separator()
        self.option_menu.add_command(label="Save", command=self.save)
        self.option_menu.add_command(label="Exit", command=quit)
        self.menubar.add_cascade(label="Options", menu=self.option_menu)
        self.root.config(menu=self.menubar)

    def open_image_option(self):
        """
        Actions taken when user selects to open an image in the application.
        :return: None
        """
        self.cleanup_option_frame()
        self.mode = 'open image'
        self.entry = Entry(self.options_frame)
        self.entry.pack(side='left')

        def set_image():
            """
            Sets the application to the user's selected image
            :return: None
            """
            self.set_image(self.entry.get())
            self.cleanup_option_frame()
            self.mode = 'none'

        self.get_image_button = Button(self.options_frame,
                                       text="Get Image",
                                       command=set_image)
        self.get_image_button.pack(side='left')

    def image_processing_option(self):
        """
        Actions when user selects the image processing option.  Allows user to change between and execute the different
        image processing operations.
        :return: None
        """
        self.cleanup_option_frame()
        if self.image is None:
            return
        self.mode = 'image processing'
        self.var = StringVar(self.options_frame)
        self.var.set("Reset Image")
        self.options = OptionMenu(self.options_frame, self.var, "Reset Image",
                                  "Tint Color", "Trace Edges", "Re-size",
                                  "Grayscale", "Black and White",
                                  "Change Saturation", "Invert Color",
                                  "Average Color", "Sepia Tone")
        self.options.pack(side="left")

        def option_selected(*_):
            """
            Switches to the type of image processing that the user selected.
            :param _: Unused parameter, necessary to make function a callback for a Stringvar.
            :return: None
            """
            self.cleanup_image_processing_options()
            if self.var.get() == "Reset Image":
                self.current_option = "Reset Image"
                self.image_processor.reset_image()
                self.update_image()
            elif self.var.get() == "Tint Color":
                self.current_option = "Tint Color"
                self.var2 = StringVar(self.options_frame)
                self.var2.set("Red")
                self.color_options = OptionMenu(self.options_frame, self.var2,
                                                "Red", "Orange", "Yellow",
                                                "Green", "Blue", "Cyan",
                                                "Magenta", "Violet")
                self.color_options.pack(side='left')

                def change_color(*_):
                    """
                    Tints the image the color that the user selects.
                    :param _: Unused. Necessary to make function a callback function
                    :return:
                    """
                    self.image_processor.color_filter(self.var2.get())
                    self.update_image()

                self.var2.trace('w', change_color)

            elif self.var.get() == "Trace Edges":
                self.current_option = "Trace Edges"
                self.image_processor.edge_detection()
                self.update_image()

            elif self.var.get() == "Re-size":
                self.current_option = "Re-size"
                self.scale = Scale(self.options_frame,
                                   from_=.1,
                                   to=3,
                                   orient=HORIZONTAL,
                                   resolution=.1)
                self.scale.pack(side='left')

                def resize():
                    """
                    Re-sizes image by multiplying the dimensions by the user's input scale factor.
                    :return: None
                    """
                    self.image_processor.resize(self.scale.get())
                    self.update_image()

                self.scale_button = Button(self.options_frame,
                                           text='Scale',
                                           command=resize)
                self.scale_button.pack(side='left')

            elif self.var.get() == "Grayscale":
                self.current_option = "Grayscale"
                self.image_processor.convert_to_grayscale()
                self.update_image()

            elif self.var.get() == "Black and White":
                self.current_option = "Black and White"
                self.image_processor.convert_to_black_and_white()
                self.update_image()

            elif self.var.get() == "Change Saturation":
                self.current_option = "Change Saturation"
                self.scale = Scale(self.options_frame,
                                   from_=0,
                                   to=2,
                                   orient=HORIZONTAL,
                                   resolution=.1)
                self.scale.pack(side='left')

                def change_saturation():
                    """
                    Modifies the saturation by the user's input modification factor.
                    :return: None
                    """
                    self.image_processor.modify_saturation(self.scale.get())
                    self.update_image()

                self.scale_button = Button(self.options_frame,
                                           text="Change Saturation",
                                           command=change_saturation)
                self.scale_button.pack()

            elif self.var.get() == "Invert Color":
                self.current_option = "Invert Color"
                self.image_processor.invert_colors()
                self.update_image()

            elif self.var.get() == "Average Color":
                self.current_option = "Average Color"
                self.image_processor.average_pixel_color()
                self.update_image()

            elif self.var.get() == "Sepia Tone":
                self.current_option = "Sepia Tone"
                self.image_processor.sepia_tone()
                self.update_image()

        self.var.trace("w", option_selected)

    def cleanup_option_frame(self):
        """
        Removes widgets from option frame.
        :return: None
        """
        if self.mode == "open image":
            self.entry.destroy()
            self.get_image_button.destroy()
        elif self.mode == "image processing":
            self.options.destroy()
            self.cleanup_image_processing_options()

    def cleanup_image_processing_options(self):
        """
        Removes widgets used for specific image processing options.
        :return: None
        """
        if self.current_option == "Tint Color":
            self.color_options.destroy()
        elif self.current_option == "Re-size" or self.current_option == "Change Saturation":
            self.scale.destroy()
            self.scale_button.destroy()

    def set_image(self, name):
        """
        Loads image from file in the images/ folder.
        :param name: name of file as a String.
        :return: None
        """
        try:
            self.image = Image.open("images/" + name)
        except:
            return
        self.image_processor = ImageProcessor(self.image)
        self.image_processor.fit_to_screen(self.root.winfo_screenwidth(),
                                           self.root.winfo_screenheight())
        self.update_image()

    def save(self):
        """
        Save current image in images/ folder with the name output.jpg
        :return: None
        """
        self.image.save("images/output.jpg")

    def update_image(self):
        """
        Updates the application to display the current image from the ImageProcessor object.
        :return: None
        """
        self.image = self.image_processor.image
        self.tkImage = ImageTk.PhotoImage(self.image)
        self.update_canvas()

    def update_canvas(self):
        """
        Updates canvas.  Do not call directly, instead call update_image.
        :return: None
        """
        self.canvas.delete(ALL)
        self.canvas.configure(width=self.image.size[0],
                              height=self.image.size[1])
        self.canvas.create_image(0, 0, image=self.tkImage, anchor=NW)
        self.canvas.update()