Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_2048_Game.py -- a slightly edited copy
- from Tkinter import *
- from random import randint, choice
- from time import sleep
- main_back = 'papaya whip'
- root = Tk()
- root['bg'] = main_back
- backs = {0: 'chocolate2', None: 'chocolate2', 2: 'orange', 4: 'dark orange', 8: 'tomato', 16: 'orange red', 32: 'red', 64: 'firebrick2', 128: 'firebrick3', 256: 'gold2', 512: 'goldenrod2', 1024: 'DarkGoldenrod2', 2048: 'yellow2'}
- root.geometry("480x480+0+0")
- xX = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4]
- field = [[None] * 4 for i in range(4)]
- for i in range(4):
- for j in range(4):
- Label(root, height=2, width=4, font="Arial 25", bg=backs[0]).place(y=i * 100, x=j * 100)
- i, j = randint(0, 3), randint(0, 3)
- t = choice(xX)
- field[i][j] = Label(text=t, font="Arial 25", height=2, width=4, bg=backs[t])
- field[i][j].place(y=i*100, x=j*100)
- summ = Label(text=t, width=4, height=2, font="Arial 20", bg='chocolate2')
- summ.place(x=400, y=250)
- alph = 0.0030
- def add_tile():
- bind_on()
- i, j = randint(0, 3), randint(0, 3)
- t = choice(xX)
- if not field[i][j]:
- field[i][j] = Label(root, text=t, height=2, width=4, font="Arial 25", bg=backs[t])
- field[i][j].place(y=i*100, x=j*100)
- summ['text'] += t
- root.update()
- else:
- add_tile()
- def left(event):
- bind_off()
- allow=0
- for i in range(4):
- free = 0
- for j in range(4):
- if not field[i][j]:
- free += 1
- else:
- if free > 0:
- allow = 1
- field[i][j - free] = field[i][j]
- for d in range(1, 101):
- field[i][j - free].place(y=i*100, x=j*100-free*d)
- root.update()
- sleep(alph)
- field[i][j] = None
- if j - free > 0 and field[i][j - free]["text"] == field[i][j - free - 1]["text"]:
- allow = 1
- for d in range(1, 101):
- field[i][j - free].place(y=i*100, x=(j-free)*100-d)
- root.update()
- sleep(alph)
- field[i][j - free].destroy()
- field[i][j - free - 1]["text"] *= 2
- field[i][j - free - 1]["bg"] = backs[field[i][j - free - 1]["text"]]
- field[i][j - free] = None
- free += 1
- root.update()
- if allow: add_tile()
- else: bind_on()
- def top(event):
- bind_off()
- allow=0
- for j in range(4):
- free = 0
- for i in range(4):
- if not field[i][j]:
- free += 1
- else:
- if free > 0:
- allow = 1
- field[i - free][j] = field[i][j]
- for d in range(1, 101):
- field[i - free][j].place(y=i*100-free*d, x=j*100)
- root.update()
- sleep(alph)
- field[i][j] = None
- if i - free > 0 and field[i - free][j]["text"] == field[i - free - 1][j]["text"]:
- allow = 1
- for d in range(1, 101):
- field[i-free][j].place(y=(i-free)*100-d, x=j*100)
- root.update()
- sleep(alph)
- field[i - free - 1][j]["text"] *= 2
- field[i - free][j].destroy()
- field[i - free - 1][j]["bg"] = backs[field[i - free - 1][j]["text"]]
- field[i - free][j] = None
- free += 1
- root.update()
- if allow: add_tile()
- else: bind_on()
- def right(event):
- bind_off()
- allow=0
- for i in range(4):
- free = 0
- for j in range(3, -1, -1):
- if not field[i][j]:
- free += 1
- else:
- if free > 0:
- allow = 1
- field[i][j + free] = field[i][j]
- for d in range(1, 101):
- field[i][j + free].place(y=i*100, x=j*100+free*d)
- root.update()
- sleep(alph)
- field[i][j] = None
- if j + free < 3 and field[i][j + free]["text"] == field[i][j + free + 1]["text"]:
- allow = 1
- for d in range(1, 101):
- field[i][j + free].place(y=i*100, x=(j+free)*100+d)
- root.update()
- sleep(alph)
- field[i][j + free + 1]["text"] *= 2
- field[i][j + free].destroy()
- field[i][j + free + 1]["bg"] = backs[field[i][j + free + 1]["text"]]
- field[i][j + free] = None
- free += 1
- root.update()
- if allow: add_tile()
- else: bind_on()
- def down(event):
- bind_off()
- allow=0
- for j in range(4):
- free = 0
- for i in range(3, -1, -1):
- if not field[i][j]:
- free += 1
- else:
- if free > 0:
- allow = 1
- field[i + free][j] = field[i][j]
- for d in range(1, 101):
- field[i+free][j].place(y=i*100+free*d, x=j*100)
- root.update()
- sleep(alph)
- field[i][j] = None
- if i + free < 3 and field[i + free][j]["text"] == field[i + free + 1][j]["text"]:
- allow = 1
- for d in range(1, 101):
- field[i + free][j].place(y=(i+free)*100+d, x=j*100)
- root.update()
- sleep(alph)
- field[i + free + 1][j]["text"] *= 2
- field[i + free][j].destroy()
- field[i + free][j] = None
- field[i + free + 1][j]["bg"] = backs[field[i + free + 1][j]["text"]]
- free += 1
- root.update()
- if allow: add_tile()
- else: bind_on()
- def bind_on():
- root.bind("<Down>", down)
- root.bind("<Up>", top)
- root.bind("<Left>", left)
- root.bind("<Right>", right)
- def bind_off():
- root.unbind("<Down>")
- root.unbind("<Up>")
- root.unbind("<Left>")
- root.unbind("<Right>")
- bind_on()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement