donRumata03

Untitled

Jun 29th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2. import sympy as sp
  3.  
  4.  
  5. # beginning
  6. def begin(s):
  7.     global started
  8.     started = True
  9.     change_status(0, 1)
  10.     print(s)
  11.  
  12.  
  13. # button function
  14. def give_next_token(token):
  15.     global turn, active_status
  16.     if not started:
  17.         return
  18.     if token == "del":
  19.         change_status(0, 1)
  20.         active_status = 1
  21.         turn = []
  22.     else:
  23.         turn.append(token)
  24.         active_status = 1 - active_status
  25.         change_status(0, active_status)
  26.         print(turn)
  27.         if len(turn) <= 1:
  28.             return
  29.         elif len(turn) == operators.get(turn[1]) + 1:
  30.             execute()
  31.             change_status(1, 1)
  32.             active_status = 1
  33.             turn = []
  34.     return
  35.  
  36.  
  37. # execution
  38. def execute():
  39.     global Buttons_f
  40.     op = turn[1]
  41.     if op == "+":
  42.         ex1 = players_f[int(turn[0][0])][int(turn[0][1])]
  43.         ex2 = players_f[int(turn[2][0])][int(turn[2][1])]
  44.         ex1 += ex2
  45.         players_f[int(turn[0][0])][int(turn[0][1])] = ex1
  46.         Buttons_f[int(turn[0][0])][int(turn[0][1])].config(text=str(ex1))
  47.     elif op == "*":
  48.         ex1 = players_f[int(turn[0][0])][int(turn[0][1])]
  49.         ex2 = players_f[int(turn[2][0])][int(turn[2][1])]
  50.         ex1 *= ex2
  51.         players_f[int(turn[0][0])][int(turn[0][1])] = ex1
  52.         Buttons_f[int(turn[0][0])][int(turn[0][1])].config(text=str(ex1))
  53.     elif op == "s":
  54.         ex1 = players_f[int(turn[0][0])][int(turn[0][1])]
  55.         ex2 = players_f[int(turn[2][0])][int(turn[2][1])]
  56.         ex1 = ex1.subs({x: ex2})
  57.         players_f[int(turn[0][0])][int(turn[0][1])] = ex1
  58.         Buttons_f[int(turn[0][0])][int(turn[0][1])].config(text=str(ex1))
  59.     elif op == "-":
  60.         ex1 = players_f[int(turn[0][0])][int(turn[0][1])]
  61.         ex1 *= -1
  62.         players_f[int(turn[0][0])][int(turn[0][1])] = ex1
  63.         Buttons_f[int(turn[0][0])][int(turn[0][1])].config(text=str(ex1))
  64.     elif op == "d":
  65.         ex1 = players_f[int(turn[0][0])][int(turn[0][1])]
  66.         ex1 = ex1.diff(x)
  67.         players_f[int(turn[0][0])][int(turn[0][1])] = ex1
  68.         Buttons_f[int(turn[0][0])][int(turn[0][1])].config(text=str(ex1))
  69.  
  70.  
  71. def change_status(change_player, mode):
  72.     global active_player, Buttons_f, Buttons_op
  73.     if change_player:
  74.         active_player = 1 - active_player
  75.         label1.config(text="ACTIVE: PLAYER " + str(active_player + 1))
  76.     if mode:
  77.         for i in range(n):
  78.             Buttons_f[active_player][i].config(state=tk.NORMAL, fg="green")
  79.             Buttons_f[1 - active_player][i].config(state=tk.ACTIVE, fg="black")
  80.         for j in range(m):
  81.             Buttons_op[active_player][j].config(state=tk.DISABLED)
  82.             Buttons_op[1 - active_player][j].config(state=tk.DISABLED)
  83.     else:
  84.         for i in range(n):
  85.             Buttons_f[active_player][i].config(state=tk.DISABLED)
  86.             Buttons_f[1 - active_player][i].config(state=tk.DISABLED)
  87.         for j in range(m):
  88.             Buttons_op[active_player][j].config(state=tk.NORMAL, fg="green")
  89.             Buttons_op[1 - active_player][j].config(state=tk.DISABLED, fg="black")
  90.  
  91.  
  92. # game parameters
  93. n = 5
  94. m = 5
  95.  
  96. # pre-window parameters
  97. lx = 300
  98. dx = 60
  99. delta = 160
  100.  
  101. # standard kit
  102. x = sp.symbols("x")
  103. functions = [x + 5, sp.exp(x), sp.sin(x), x**2 - 7 * x + 10, sp.asin(x)]
  104. operators = {"+": 2, "*": 2, "d": 1, "-": 1, "s": 2}
  105. texts_op = [["+", "-", "*", "d/dx", "sp"], ["+", "-", "*", "d", "s"]]
  106.  
  107. # active data
  108. started = 0
  109. turn = []
  110. players_f = [functions[:], functions[:]]
  111. active_player = 0
  112. active_status = 1
  113. turn_number = 0
  114.  
  115. # window
  116. window = tk.Tk()
  117. window.title("Zerocity")
  118. window.config(height=str(dx * (n + 2) + delta), width=str(lx + m * dx), bg="black")
  119. window.resizable(width=False, height=False)
  120.  
  121. # frames
  122. frame1 = tk.Frame(window)
  123. frame1.place(x=0, y=dx, height=dx * (n + 1) + delta, width=lx + m * dx)
  124. frame1.config(bg="#aaccff")
  125.  
  126. # labels
  127. label1 = tk.Label(window, text="ACTIVE: PLAYER " + str(active_player + 1), bg="black", fg="green", font="Arial 20")
  128. label1.place(x=0, y=0, height=dx, width=lx + m * dx)
  129.  
  130. # binding
  131. window.bind('<s>', lambda event: begin("started"))
  132.  
  133.  
  134. # buttons set
  135. Buttons_f = [[], []]
  136. Buttons_op = [[], []]
  137.  
  138.  
  139. for i in range(len(functions)):
  140.     button_1 = tk.Button(frame1, text=str(functions[i]), font="Arial 20")
  141.     button_2 = tk.Button(frame1, text=str(functions[i]), font="Arial 20")
  142.     button_1.place(x=0, y=i * dx, width=lx, height=dx)
  143.     button_2.place(x=m * dx, y=dx + delta + i * dx, width=lx, height=dx)
  144.     Buttons_f[0].append(button_1)
  145.     Buttons_f[1].append(button_2)
  146.  
  147. for j in range(len(texts_op[0])):
  148.     button_1 = tk.Button(frame1, text=str(texts_op[0][j]), font="Arial 20")
  149.     button_2 = tk.Button(frame1, text=str(texts_op[0][j]), font="Arial 20")
  150.  
  151.     button_1.place(x=lx + j * dx, y=0, width=dx, height=dx)
  152.     button_2.place(x=j * dx, y=dx * n + delta, width=dx, height=dx)
  153.     Buttons_op[0].append(button_1)
  154.     Buttons_op[1].append(button_2)
  155.  
  156. # button config
  157. for i in range(len(functions)):
  158.     Buttons_f[0][i].config(command=lambda i=i: give_next_token("0" + str(i)), state=tk.DISABLED)
  159.     Buttons_f[1][i].config(command=lambda i=i: give_next_token("1" + str(i)), state=tk.DISABLED)
  160.  
  161. for j in range(len(texts_op[0])):
  162.     Buttons_op[0][j].config(command=lambda j=j: give_next_token(texts_op[1][j]), state=tk.DISABLED)
  163.     Buttons_op[1][j].config(command=lambda j=j: give_next_token(texts_op[1][j]), state=tk.DISABLED)
  164.  
  165.  
  166. window.mainloop()
Add Comment
Please, Sign In to add comment