Advertisement
here2share

# Tk_demo_board.py

Sep 5th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. # Tk_demo_board.py
  2.  
  3. import random
  4. from Tkinter import *
  5.  
  6. def mousePressed(event):
  7.     canvas = event.widget.canvas
  8.     board = canvas.data["board"]
  9.     cell = getCell(canvas, event.x, event.y)
  10.     if (cell != None):
  11.         (row, col) = cell
  12.         if (board[row][col] != 0):
  13.             print "*** already occupied ***"
  14.         else:
  15.             board[row][col] = canvas.data["currentPlayer"]
  16.             switchPlayers()
  17.     redrawAll()
  18.  
  19. def getCell(canvas, x, y):
  20.     board = canvas.data["board"]
  21.     rows = len(board)
  22.     cols = len(board[0])
  23.     for row in range(rows):
  24.         for col in range(cols):
  25.             (left, top, right, bottom) = cellBounds(canvas, row, col)  
  26.             if ((left <= x) and (x <= right) and
  27.                 (top <= y) and (y <= bottom)):
  28.                 return (row, col)
  29.     return None
  30.  
  31. def keyPressed(event):
  32.     canvas = event.widget.canvas
  33.     redrawAll()
  34.  
  35. def gameOver():
  36.     canvas.data["isGameOver"] = True
  37.  
  38. def timerFired():
  39.     pass
  40.  
  41. def redrawAll():
  42.     canvas.delete(ALL)
  43.     drawBoard()
  44.     if (canvas.data["isGameOver"] == True):
  45.         cx = canvas.data["canvasWidth"]/2
  46.         cy = canvas.data["canvasHeight"]/2
  47.         canvas.create_text(cx, cy, text="Game Over!", font=("Helvetica", 32, "bold"))
  48.  
  49. def drawBoard():
  50.     board = canvas.data["board"]
  51.     rows = len(board)
  52.     cols = len(board[0])
  53.     for row in range(rows):
  54.         for col in range(cols):
  55.             drawCell(canvas, board, row, col)
  56.  
  57. def cellBounds(canvas, row, col):
  58.     margin = canvas.data["margin"]
  59.     cellSize = canvas.data["cellSize"]
  60.     left = margin + col * cellSize
  61.     right = left + cellSize
  62.     top = margin + row * cellSize
  63.     bottom = top + cellSize
  64.     return (left, top, right, bottom)  
  65.  
  66. def drawCell(canvas, board, row, col):
  67.     (left, top, right, bottom) = cellBounds(canvas, row, col)
  68.     canvas.create_rectangle(left, top, right, bottom, fill="white")
  69.     if (board[row][col] > 0):
  70.         if (board[row][col] == 1):
  71.             color = "red"
  72.         else:
  73.             color = "green"
  74.         canvas.create_oval(left+3, top+3, right-3, bottom-3, width=0, fill=color)
  75.  
  76. def loadBoard():
  77.     rows = canvas.data["rows"]
  78.     cols = canvas.data["cols"]
  79.     board = [ ]
  80.     for row in range(rows): board += [[0] * cols]
  81.     canvas.data["board"] = board
  82.  
  83. def printInstructions():
  84.     print "Demo Started..."
  85.  
  86. def switchPlayers():
  87.     player = canvas.data["currentPlayer"]
  88.     if (player == 1):
  89.         canvas.data["currentPlayer"] = 2
  90.     else:
  91.         canvas.data["currentPlayer"] = 1
  92.  
  93. def init():
  94.     printInstructions()
  95.     loadBoard()
  96.     canvas.data["inDebugMode"] = False
  97.     canvas.data["isGameOver"] = False
  98.     canvas.data["currentPlayer"] = 1
  99.     redrawAll()
  100.  
  101. ########### copy-paste below here ###########
  102.  
  103. def run(rows, cols):
  104.     canvas.data["rows"] = rows
  105.     canvas.data["cols"] = cols
  106.     # Set up canvas data and call init
  107.    
  108.     init()
  109.     # set up events
  110.     root.bind("<Button-1>", mousePressed)
  111.     root.bind("<Key>", keyPressed)
  112.     timerFired()
  113.     # and launch the app
  114.     root.mainloop()  # This call BLOCKS (so your program waits until you close the window!)
  115.  
  116. # create the root and the canvas
  117. root = Tk()
  118. root.resizable(width=0, height=0)
  119. # Store canvas in root and in canvas itself for callbacks
  120. margin = 5
  121. cellSize = 30
  122. rows, cols = 8, 16
  123. canvasWidth = 2*margin + cols*cellSize
  124. canvasHeight = 2*margin + rows*cellSize
  125. canvas = Canvas(root, width=canvasWidth, height=canvasHeight)
  126. root.canvas = canvas.canvas = canvas
  127. canvas.data = {}
  128. canvas.data["margin"] = margin
  129. canvas.data["cellSize"] = cellSize
  130. canvas.data["canvasWidth"] = canvasWidth
  131. canvas.data["canvasHeight"] = canvasHeight
  132. canvas.data["rows"] = rows
  133. canvas.data["cols"] = cols
  134. canvas.pack()
  135. run(8, 16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement