예제 #1
0
def grid():
    grid = Grid()
    grid.init()
    grid.specs()
    grid.draw()
예제 #2
0
파일: main.py 프로젝트: deplanty/mini-games
class Application(tk.Tk):
    def __init__(self):
        super().__init__()

        self.bomb_percent = 0.15

        self.withdraw()
        self.title("Sweeper")
        self.set_menu()
        self.set_ui()
        self.init_new_game()
        self.set_events()
        self.resizable(False, False)
        self.deiconify()

    def set_menu(self):
        """
        Sets the menu in the UI
        """

        self.var_menu_size = tk.IntVar(self, 0)

        menu = tk.Menu(self)
        menu_game = tk.Menu(menu, tearoff=0)
        menu_game.add_command(label="Nouveau", command=self.init_new_game)
        menu_game.add_separator()
        menu_game.add_radiobutton(label="8x8",
                                  value=0,
                                  variable=self.var_menu_size,
                                  command=self.init_new_game)
        menu_game.add_radiobutton(label="16x16",
                                  value=1,
                                  variable=self.var_menu_size,
                                  command=self.init_new_game)
        menu_game.add_radiobutton(label="32x32",
                                  value=2,
                                  variable=self.var_menu_size,
                                  command=self.init_new_game)
        menu.add_cascade(label="Jeu", menu=menu_game)
        self.configure(menu=menu)

    def set_ui(self):
        """
        Sets the structure of the ui
        """

        self.canvas = tk.Canvas(self)
        self.canvas.pack()
        self.field = Grid(self.canvas)

    def set_events(self):
        """
        Sets events in the ui
        """

        self.bind("<KeyPress-a>", self.cheat_show_all)
        self.bind("<KeyPress-b>", self.cheat_show_bombs)
        self.bind("<KeyRelease-b>", self.cheat_hide_bombs)
        self.canvas.bind("<Button-1>", self.event_click_left)
        self.canvas.bind("<Button-3>", self.event_click_right)

    def init_new_game(self):
        """
        Initializes a new game

        - clear previous game
        - create grid
        - add bombs
        """

        # Create new game grid
        grid_size = {
            0: (8, 8),
            1: (16, 16),
            2: (32, 32)
        }.get(self.var_menu_size.get())

        # Clear previous game
        self.field.init(grid_size)

        # Add bombs
        bombs = itertools.product(range(grid_size[0]), range(grid_size[1]))
        n = grid_size[0] * grid_size[1]
        bombs = random.choices(list(bombs), k=int(n * self.bomb_percent))

        for i, j in bombs:
            self.field.set_bomb(i, j)
        self.field.process_values()

    # Events

    def event_click_left(self, event, *args):
        """
        Manage the left-click event
        """

        result = self.field.clicked(event.x, event.y)

        # Lost
        if result == -1:
            self.cheat_show_bombs()
            tk.messagebox.showerror(title="Perdu", message="Vous avez perdu !")
            self.init_new_game()
            return

        if self.field.has_win():
            self.cheat_show_bombs()
            tk.messagebox.showinfo(title="Gagné", message="Vous avez gagné !")
            self.init_new_game()
            return

    def event_click_right(self, event, *args):
        """
        Manage the right-click event
        """

        self.field.flag(event.x, event.y)

    # Cheat codes

    def cheat_show_bombs(self, *args):
        """
        Shows bombs
        """

        for box in self.field.list_boxes():
            if box.is_bomb():
                self.canvas.itemconfigure(box.iid, outline="red")

    def cheat_hide_bombs(self, *args):
        """
        Shows bombs
        """

        for box in self.field.list_boxes():
            if box.is_bomb():
                self.canvas.itemconfigure(box.iid, outline="#A0B0A0")

    def cheat_show_all(self, *args):
        """
        Shows all
        """

        for box in self.field.list_boxes():
            if box.is_bomb():
                self.canvas.itemconfigure(box.iid, outline="red")
            else:
                self.field.show_box(box)
예제 #3
0
class Application(tk.Tk):
    def __init__(self):
        super().__init__()

        self.withdraw()
        self.title("Sudoku")
        self.set_ui()
        # self.set_menu()
        self.set_events()
        self.init_new_game("resources/Facile-1.txt")
        self.resizable(False, False)
        self.deiconify()

    def set_ui(self):
        """
        Sets the elements in the UI.
        """

        self.canvas = tk.Canvas(self)
        self.canvas.pack()

        self.entry = ttk.Entry(self.canvas,
                               justify="center",
                               font=("Calibri", 12))

        self.grid = Grid(self.canvas)

    def set_events(self):
        """
        Sets events in the UI.
        """

        self.canvas.bind("<Button-1>", self.event_click_left)
        self.bind("<Return>", self.event_return)

    # Game methods

    def init_new_game(self, file: str):
        """
        Inits a new game with a grid from a file.
        """

        self.grid.init(file)

    # Events

    def event_click_left(self, event):
        """
        Manages the mouse left button event.
        """

        i, j = self.grid.get_position(event.x, event.y)

        if self.grid[i][j].fixed:
            return

        # Places the entry
        self.entry["text"] = self.grid[i][j]
        self.entry.place(x=j * self.grid.box_size + 2,
                         y=i * self.grid.box_size + 2,
                         width=self.grid.box_size + 1,
                         height=self.grid.box_size + 1)
        self.entry.focus_set()

    def event_return(self, event):
        """
        Manages the return key event.
        """

        x = self.entry.winfo_x()
        y = self.entry.winfo_y()
        i, j = self.grid.get_position(x, y)
        self.grid.set_value(i, j, self.entry.get())
        self.entry.place_forget()

        win = self.grid.check_grid()
        if win:
            tk.messagebox.showinfo(title="Gagné",
                                   message="Félicitation ! Vous avez réussi !")
예제 #4
0
class Application(tk.Tk):
    def __init__(self):
        super().__init__()

        # Field size
        self.grid_size = (16, 16)

        self.state = "pause"
        self.state_id = None

        self.var_ms = tk.IntVar(self, 300)

        self.withdraw()
        self.title("Life Game")
        # self.set_menu()
        self.set_ui()
        self.set_events()
        self.resizable(False, False)
        self.init_new_game()
        self.deiconify()

    def set_ui(self):
        """
        Sets the elements in the UI.
        """

        f = tk.Frame(self)
        f.pack(fill="both", expand=True)

        self.b_start = ttk.Button(f,
                                  text="Démarrer",
                                  command=self.btn_run_pause)
        self.b_start.pack(side="left", padx=5, pady=5)
        self.b_reset = ttk.Button(f,
                                  text="Recommencer",
                                  command=self.btn_reset)
        self.b_reset.pack(side="left", padx=5, pady=5)
        self.s_ms = tk.Scale(f,
                             from_=10,
                             to=1000,
                             resolution=10,
                             variable=self.var_ms,
                             orient="horizontal")
        self.s_ms.pack(side="left", padx=5, pady=5)
        # self.l_ms = ttk.Label()

        self.canvas = tk.Canvas()
        self.canvas.pack()
        self.field = Grid(self.canvas)

    def set_events(self):
        """
        Sets the events in the UI.
        """

        self.bind("<space>", self.event_space)
        self.canvas.bind("<Button-1>", self.event_left_click)

    def init_new_game(self):
        """
        Initializes a new field
        """

        # Clear previous game
        self.field.init(self.grid_size)
        self.focus_set()

    def loop(self):
        """
        Loops
        """

        for box in self.field.gen_boxes():
            self.field.process_box(box)

        self.field.apply()

        self.state_id = self.after(self.var_ms.get(), self.loop)

    # Boutons

    def btn_run_pause(self):
        """
        Runs or pauses the loop.
        """

        self.event_space()

    def btn_reset(self):
        """
        Resets all boxes.
        """

        if self.state == "pause":
            self.init_new_game()

    # Events

    def event_space(self, event=None):
        """
        Manages the event when pressing space bar
        """

        if self.state == "pause":
            self.state = "run"
            self.b_start.configure(text="Pause")
            self.focus_set()
            self.loop()
        elif self.state == "run":
            self.state = "pause"
            self.b_start.configure(text="Démarrer")
            self.focus_set()
            self.after_cancel(self.state_id)

    def event_left_click(self, event=None):
        """
        Manages tje event when pressing left click
        """

        # Can modify field only when paused
        if self.state == "pause":
            i = event.y // self.field.box_size
            j = event.x // self.field.box_size
            self.field.toggle(i, j)