Skip to content

Commit 134c83d

Browse files
committed
Improved LyricsSearcher
1 parent b3e4c8c commit 134c83d

File tree

2 files changed

+100
-17
lines changed

2 files changed

+100
-17
lines changed

Python Files/BackupHelper.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,27 @@ def __init__(self, src: str, dst: str, copy: bool = True, update: bool = True):
1212
self.__filesRemoved = []
1313
self.__foldersToRemove = []
1414
self.__foldersRemoved = []
15+
self.__src = src
16+
self.__dst = dst
17+
self.__copy = copy
18+
self.__update = update
19+
self.again(src, dst, copy, update)
20+
21+
def again(self, src: str = None, dst: str = None, copy: bool = None, update: bool = None):
22+
if src != None: self.__src = src
23+
if dst != None: self.__dst = dst
24+
if copy != None: self.__copy = copy
25+
if update != None: self.__update = update
1526
self.__check(src, dst, copy, update)
27+
if not copy:
28+
print(f'待复制文件:{len(self.__filesToCopy)}项')
29+
if not update:
30+
print(f'待更新文件:{len(self.__filesToUpdate)}项')
1631
if self.__filesToRemove:
1732
print(f'待移除文件:{len(self.__filesToRemove)}项')
1833
if self.__foldersToRemove:
1934
print(f'待移除文件夹:{len(self.__foldersToRemove)}项')
35+
2036

2137
def __check(self, src, dst, copy, update):
2238
src_list = os.listdir(src)
@@ -193,5 +209,7 @@ def foldersRemoved(self):
193209
self.__print(self.__foldersRemoved, '已移除文件夹:{0}项', '没有已移除的文件夹')
194210

195211
if __name__ == '__main__':
196-
c = Checker(r'C:\Users\Seaky\Desktop\Docs',
197-
r'E:\Docs')
212+
c = Checker(r'C:\Users\Seaky\Desktop\Pics',
213+
r'E:\Pics',
214+
False,
215+
False)

Python Files/LifeGame.py

+80-15
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ class LifeGame(Frame):
99
# 2. A life dies when the number of surrounding lives is greater than 3
1010
# 3. A dead revives when the number of surrounding lives is 3
1111
# 4. A life stays the same when the number of surrounding lives is 2 or 3
12-
def __init__(self, parent, grid: list = None, interval: float = 1, cell_size: int = 5):
12+
def __init__(self, parent, grid: list = None, interval: float = 0.05, cell_size: int = 5):
1313
Frame.__init__(self, parent)
14-
self.grid = [list(map(bool, row)) for row in grid]
14+
self.grid = [list(map(bool, row)) for row in grid] if grid else LifeGame.randomGrid()
1515
self.interval = interval
1616
self.cell_size = cell_size
1717
self.hcells = len(self.grid[0])
1818
self.vcells = len(self.grid)
19+
self.cells = {}
1920
self.canvas = Canvas(self,
2021
height = self.vcells * self.cell_size,
2122
width = self.hcells * self.cell_size)
@@ -25,12 +26,19 @@ def __init__(self, parent, grid: list = None, interval: float = 1, cell_size: in
2526
def draw_cell(self, x, y, alive: bool):
2627
loc_x = x * self.cell_size
2728
loc_y = y * self.cell_size
28-
self.canvas.create_rectangle(loc_x, loc_y,
29-
loc_x + self.cell_size,
30-
loc_y + self.cell_size,
31-
fill = [None, 'black'][alive])
29+
if alive:
30+
cell = self.canvas.create_rectangle(loc_x, loc_y,
31+
loc_x + self.cell_size,
32+
loc_y + self.cell_size,
33+
fill = 'black')
34+
self.cells[(x, y)] = cell
35+
else:
36+
self.canvas.delete(self.cells[(x, y)])
37+
del self.cells[(x, y)]
3238

3339
def start(self):
40+
self.cells.clear()
41+
self.pause = False
3442
self.running = True
3543
def func():
3644
for i, row in enumerate(self.grid):
@@ -40,15 +48,17 @@ def func():
4048
print('Done Initing')
4149
while self.running:
4250
time.sleep(self.interval)
43-
if self.running:
51+
if self.pause:
52+
pass
53+
elif self.running:
4454
self.endOfTurn()
4555
print('End of turn')
4656
print('Stopped.')
4757
self.thread = threading.Thread(target=func)
4858
self.thread.start()
4959

5060
def endOfTurn(self):
51-
newCells = []
61+
newGrid = []
5262
for i, row in enumerate(self.grid):
5363
newRow = []
5464
for j, cell in enumerate(row):
@@ -75,20 +85,75 @@ def endOfTurn(self):
7585
surrounding.append(self.grid[i + 1][j - 1])
7686
alive = surrounding.count(True)
7787
new = alive == 3 or (alive == 2 and cell)
78-
if new != cell:
79-
self.draw_cell(i, j, new)
88+
if new != cell: self.draw_cell(i, j, new)
8089
newRow.append(new)
81-
newCells.append(newRow)
82-
self.grid = newCells
90+
newGrid.append(newRow)
91+
self.grid = newGrid
92+
93+
def resume(self):
94+
self.pause = False
95+
96+
def pause(self):
97+
self.pause = True
8398

8499
def stop(self):
85100
self.running = False
86101

102+
def getNewGrid(grid):
103+
hmax = len(grid[0])
104+
vmax = len(grid)
105+
newGrid = []
106+
for i, row in enumerate(grid):
107+
newRow = []
108+
for j, cell in enumerate(row):
109+
surrounding = []
110+
up = i > 0
111+
left = j > 0
112+
down = i + 1 < hmax
113+
right = j + 1 < vmax
114+
if left:
115+
surrounding.append(row[j - 1])
116+
if up:
117+
surrounding.append(grid[i - 1][j - 1])
118+
if up:
119+
surrounding.append(grid[i - 1][j])
120+
if right:
121+
surrounding.append(grid[i - 1][j + 1])
122+
if right:
123+
surrounding.append(row[j + 1])
124+
if down:
125+
surrounding.append(grid[i + 1][j + 1])
126+
if down:
127+
surrounding.append(grid[i + 1][j])
128+
if left:
129+
surrounding.append(grid[i + 1][j - 1])
130+
alive = surrounding.count(True)
131+
new = alive == 3 or (alive == 2 and cell)
132+
newRow.append(new)
133+
newGrid.append(newRow)
134+
return newGrid
135+
136+
def print(grid):
137+
for row in grid:
138+
print(list(map(int, row)))
139+
140+
def randomGrid():
141+
grid = [[0] * 300 for _ in range(300)]
142+
rand = random.randint(0, 2)
143+
num = 2 # rand
144+
if num == 0:
145+
grid[50][50] = grid[51][51] = grid[49][52] = grid[50][52] = grid[51][52] = 1
146+
elif num == 1:
147+
grid[30][30] = grid[31][30] = grid[32][30] = grid[36][30] = grid[37][30] = grid[38][30] = 1
148+
grid[34][26] = grid[34][27] = grid[34][28] = grid[34][32] = grid[34][33] = grid[34][34] = 1
149+
elif num == 2:
150+
for i in range(10):
151+
grid[30][30 + i] = 1
152+
return grid
153+
87154
if __name__=='__main__':
88-
grid = [[0] * 300 for _ in range(300)]
89-
grid[50][50] = grid[50][51] = grid[51][51] = 1
90155
root = Tk()
91-
game = LifeGame(root, grid)
156+
game = LifeGame(root)
92157
game.pack()
93158
root.mainloop()
94159
game.stop()

0 commit comments

Comments
 (0)