here2share

# TkBoxGame.py

Jun 21st, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. # TkBoxGame.py
  2.  
  3. # import the modules we need, for creating a GUI...
  4. import Tkinter as tk
  5. import tkMessageBox as messagebox
  6.  
  7.  
  8. board = [[None] * 10 for _ in range(10)]
  9.  
  10. counter = 0
  11. root = tk.Tk()
  12. root.geometry("300x300+100+300")
  13. root.title("Box Game")
  14.  
  15. # method for drawing the game
  16. def check_board():
  17.     freespaces = 0
  18.     redspaces = 0
  19.     greenspaces = 0
  20.     for i, row in enumerate(board):
  21.         for j, column in enumerate(row):
  22.             if board[i][j] == "red":
  23.                 redspaces += 1
  24.             elif board[i][j] == "green":
  25.                 greenspaces += 1
  26.             elif board[i][j] == None:
  27.                 freespaces += 1
  28.  
  29.     if freespaces == 0:
  30.         if greenspaces > redspaces:
  31.             winner = "green"
  32.         elif greenspaces < redspaces:
  33.             winner = "red"
  34.         else:
  35.             winner = "draw"
  36.  
  37.         if winner != "draw":
  38.             messagebox.showinfo("Game Over!", winner + " wins!")
  39.         else:
  40.             messagebox.showinfo("Game Over!", "The game was a draw!")
  41.  
  42.  
  43. # method to color the particular box
  44. def on_click(i, j, event):
  45.     global counter
  46.     if counter < 100:
  47.         if board[i][j] == None:
  48.             color = "green" if counter % 2 else "red"
  49.             enemycolor = "red" if counter % 2 else "green"
  50.             event.widget.config(bg=color)
  51.             board[i][j] = color
  52.             for k in range(-1, 2):
  53.                 for l in range(-1, 2):
  54.                     try:
  55.                         if board[i + k][j + l] == enemycolor:
  56.                             board[i + k][j + l] = color
  57.                     except IndexError:
  58.                         pass
  59.             counter += 1
  60.             global gameframe
  61.             gameframe.destroy()
  62.             redraw()
  63.             root.wm_title(enemycolor + "'s turn")
  64.         else:
  65.             messagebox.showinfo("Alert", "This square is already occupied!")
  66.         check_board()
  67.  
  68. # call the main game everytime after one_click method
  69. def redraw():
  70.     global gameframe
  71.     gameframe = tk.Frame(root)
  72.     gameframe.pack()
  73.  
  74.     for i, row in enumerate(board):
  75.  
  76.         for j, column in enumerate(row):
  77.             name = str(i) + str(j)
  78.             L = tk.Label(gameframe, text='    ',
  79.                          bg="grey" if board[i][j] == None else board[i][j])
  80.             L.grid(row=i, column=j, padx='3', pady='3')
  81.             L.bind('<Button-1>', lambda e, i=i, j=j: on_click(i, j, e))
  82.  
  83.  
  84. redraw()
  85. root.mainloop()
Add Comment
Please, Sign In to add comment