Skip to content

Commit

Permalink
Added Undo Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aahan0511 committed Jan 14, 2025
1 parent c47d77f commit ea035bb
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def __init__(root) -> None:
root.title("")
root.iconbitmap(PATHS["empty"])

root.prevMatrix = None
root.prevScore = None

root.scoreVar = IntVar(value=0)
root.highVar = IntVar(value=cursor.execute(f'''SELECT MAX(Score) FROM Scores''').fetchone()[0])
icon = ImageTk.PhotoImage(Image.open(PATHS["icon"]).resize((80, 80)))
Expand All @@ -85,6 +88,9 @@ def keyPress(root, event: Event) -> None:
movement = False
key = event.keysym

root.prevMatrix = matrix.copy()
root.prevScore = root.scoreVar.get()

if showingWin or showingLoss: return

for cell in grid:
Expand Down Expand Up @@ -196,9 +202,32 @@ def play(root, _: Event = None) -> None:
def increase(root, increament: int) -> None:
root.scoreVar.set(root.scoreVar.get()+increament)

def undo(screen) -> None:
def undo(root) -> None:
global grid, matrix

if not pause:
pass #TODO: add undo function
if root.prevMatrix:
for cell in grid:
if cell:
cell.destroy()

grid = [None]*16
matrix = [0]*16

for idx, cell in enumerate(root.prevMatrix):
if cell != 0:
temp = Block(root.game)
temp.power = cell
temp.x = (idx%4)*136.25+73.125
temp.y = (idx//4)*136.25+73.125
temp.pos = idx
temp.set()
temp.place()

root.scoreVar.set(root.prevScore)

root.prevMatrix = None
root.prevScore = None

else:
openWeb("https://github.com/aahan0511/the2048game")
Expand Down Expand Up @@ -904,6 +933,9 @@ def merge(block, direction: str) -> None:
entry_fg_color="#bdac97",
entry_text_color="#ffffff"
)
system("cls")

name.after(200, lambda: name.iconbitmap(PATHS["empty"]))

windll.dwmapi.DwmSetWindowAttribute(
windll.user32.GetParent(name.winfo_id()),
Expand All @@ -920,5 +952,5 @@ def merge(block, direction: str) -> None:

cursor.execute(f"INSERT INTO Scores VALUES ('{datetime.now().date()} {datetime.now().hour}:{datetime.now().minute}.{datetime.now().second}', {app.scoreVar.get()}, {2**max(matrix)}, '{name.get_input()}')")
conn.commit()
conn.close()

conn.close()

0 comments on commit ea035bb

Please sign in to comment.