@IMPORTS: from tkinter import *
@CODE
-
Instantiate
TK()
as root -> Creates the window -
Set the window size (root.geometry("WxH"))
-
Give the title (root.title)
-
Block window maximize (root.resizable(False,False))
-
Create Frames i.e divide the
window
into frames/segments a. InstantiateFrame()
-> This is the top frame *Args: root, width, heightb. Place the frame -> x-axis=0, y_axis=0 (top_frame.place()) *Args: x, y
c. Instantiate
Frame()
-> This is the left framed. Place the frame -> Place after a+x_axis pixels
e Instantiate
Frame()
-> This is the center frame -
File: Cell.py a. Create a class Cell *Args: is_mine=False **Prog: -Create
self.cell_btn_obj = None
so that button objects can be stored in it later. This will help us modify its properties later on!
METHOD:
a. create_btn_object()
**Args: frame
**Prog:
-Instantiate Button()
as btn
and pass frame
to it | Pass
width and height
-Assign the obj to self.cell_btn_obj
|
-use the bind
method of the btn
and pass '<Button-1>'
, (i.e.
Left Mouse Click) self.left_click_action
(NOTE: Dont call the method
instead just pass its ref)
-Do the same for '<Button-3>'
(i.e. Right Mouse Click)
b. left_clikc_action()
*Args: event (Not of any use, its meta data that tkinter passes when
bind
is called)
c right_click_action() *Args: event
root.mainloop() --> Make the Window
-
Create a nested for loop to iterate over the
grid_size
(fromsettings.py
) | InstantiateCell()
| Call thecreate_btn_obj
| use theself.cell_btn_obj
attr to customize the location of the button (i.e. cell) -> Use.grid()
methodgrid()
*Args: rows, columns -
Modify the
Cell
init and pass 2 more args --> x, y (i.e. the position of the cell) and save them -
In Main.py pass row, columns from the nested for loop to the instantiated Cell()
-
Create a static method in
Cell
--Name: randomize_mines() -
Create a class attribute -- all = [] -> store all cell attrs
-
Append all the instans to
all
-
Cell:
- In randommize_mines();
--- select random cell from
Cell.all
--- Use for loop to changeself.is_mine
to True
-
In
left_mouse_clikc
method --Check is cell is a mine | ture: Callshow_mines
| Fales: callshow_cell
-
Create a method to show mines --> The player has lost the game
-
Create a method to surrounding cells NOTE: The code within should be unmutable. thus use @property decorator NOTE: @property converts a method into an attribute
--> Create list to store all the surrounding cells of a given cell
NOTE: Edges, Corners, Others will have 5,3,8 surrounders respectivly
--> Add the formula to make fetch the surrounding cells
--> pass the formula to get_cell_by_axis
- Create a method to return the called cell:
**Name: get_cell_by_axis
**Args: row, column
**For Loop:
--> loop
all
| Compare the row and column args with the attrs ofall
i.e. cell.row = row nd cell.col == col **Returns: The cell instance
@property 18. Create a mehtod surrounded cell mines length **For loop --> Itreate over surrounded cells --> Check if its a mine --> Incremetn a counter **Retuns: Counter
- Create a method show cells: Docs: Reveal the cell (basically its surrounding cell count if its not a mine) **Prog: --> Configure the cell's text and pass text as the surrounded cell mines length attr
20 if surrounding mine count is 0, reveal them: **Prog: IF condition:
- Check if surroungind mine count is 0
- Ture -> get surrounding cells
- Pass those cells to
show_cell
-
Create a method
create cell cout label
**Args: frame **Prog: -InstantiateLabel
as lbl @Args: frame, text -Assing lbl to the class attrcell_count
-
Create a class attr
cell_count
-
In method
show_cell
**Prog:
- Check if cell is not open,
- True| Decrease the cell_count
- If the
cell_count
is not None - Ture | Congif the label
- Change is_open to True
-
In init: -> Create an atttr is_open | set it to False
-
Make Mine Candidate: **Docs: Mark a cell as to be a mine candidate/ unmark it -- Make a is_mine_candidate (instance attr) (Type -> Bool) -- In the
right_click
method --IF: Check if is_mine_candidate is false: -- True | Congif cell to desired color & update is_mine_candidate --ELSE: Config cell colo toSystemButtonFace
& update is_mine_candidate -- In inshow_cell
method -- Config cell instant toSystemButtomFace
EXTRA:
-- Cancel all events for cells that have been revealed
**Prog: unbind
the cell instances that have been revealed
-
Game Lost: *Import ctypes , sys **Prog: -- In game_lost method, `ctypes.windll.user32.MessageBoxW(0, Body, Title, 0) -- sys.exit()
-
Game Won: -- In show cell method: **Prog: --IF : cell.cell_count == settings.mine_count -- True: ctypes.windll.user32.MessageBoxW(0,Body, Title, 0) & sys.exit()
-
Make the Title -- Use
Label
and place it
@FILES
-
Main.py
-
Settings.py -- Will contain all the Constantes a. Widht b. Height c. Grid_size d. Mine_Count e. Total_cells
-
Utils.py -- Will contain all functions to calculate different values
-
Cell.py -- Will contain all the cell for the game