Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_demo_board.py
- import random
- from Tkinter import *
- def mousePressed(event):
- canvas = event.widget.canvas
- board = canvas.data["board"]
- cell = getCell(canvas, event.x, event.y)
- if (cell != None):
- (row, col) = cell
- if (board[row][col] != 0):
- print "*** already occupied ***"
- else:
- board[row][col] = canvas.data["currentPlayer"]
- switchPlayers()
- redrawAll()
- def getCell(canvas, x, y):
- board = canvas.data["board"]
- rows = len(board)
- cols = len(board[0])
- for row in range(rows):
- for col in range(cols):
- (left, top, right, bottom) = cellBounds(canvas, row, col)
- if ((left <= x) and (x <= right) and
- (top <= y) and (y <= bottom)):
- return (row, col)
- return None
- def keyPressed(event):
- canvas = event.widget.canvas
- redrawAll()
- def gameOver():
- canvas.data["isGameOver"] = True
- def timerFired():
- pass
- def redrawAll():
- canvas.delete(ALL)
- drawBoard()
- if (canvas.data["isGameOver"] == True):
- cx = canvas.data["canvasWidth"]/2
- cy = canvas.data["canvasHeight"]/2
- canvas.create_text(cx, cy, text="Game Over!", font=("Helvetica", 32, "bold"))
- def drawBoard():
- board = canvas.data["board"]
- rows = len(board)
- cols = len(board[0])
- for row in range(rows):
- for col in range(cols):
- drawCell(canvas, board, row, col)
- def cellBounds(canvas, row, col):
- margin = canvas.data["margin"]
- cellSize = canvas.data["cellSize"]
- left = margin + col * cellSize
- right = left + cellSize
- top = margin + row * cellSize
- bottom = top + cellSize
- return (left, top, right, bottom)
- def drawCell(canvas, board, row, col):
- (left, top, right, bottom) = cellBounds(canvas, row, col)
- canvas.create_rectangle(left, top, right, bottom, fill="white")
- if (board[row][col] > 0):
- if (board[row][col] == 1):
- color = "red"
- else:
- color = "green"
- canvas.create_oval(left+3, top+3, right-3, bottom-3, width=0, fill=color)
- def loadBoard():
- rows = canvas.data["rows"]
- cols = canvas.data["cols"]
- board = [ ]
- for row in range(rows): board += [[0] * cols]
- canvas.data["board"] = board
- def printInstructions():
- print "Demo Started..."
- def switchPlayers():
- player = canvas.data["currentPlayer"]
- if (player == 1):
- canvas.data["currentPlayer"] = 2
- else:
- canvas.data["currentPlayer"] = 1
- def init():
- printInstructions()
- loadBoard()
- canvas.data["inDebugMode"] = False
- canvas.data["isGameOver"] = False
- canvas.data["currentPlayer"] = 1
- redrawAll()
- ########### copy-paste below here ###########
- def run(rows, cols):
- canvas.data["rows"] = rows
- canvas.data["cols"] = cols
- # Set up canvas data and call init
- init()
- # set up events
- root.bind("<Button-1>", mousePressed)
- root.bind("<Key>", keyPressed)
- timerFired()
- # and launch the app
- root.mainloop() # This call BLOCKS (so your program waits until you close the window!)
- # create the root and the canvas
- root = Tk()
- root.resizable(width=0, height=0)
- # Store canvas in root and in canvas itself for callbacks
- margin = 5
- cellSize = 30
- rows, cols = 8, 16
- canvasWidth = 2*margin + cols*cellSize
- canvasHeight = 2*margin + rows*cellSize
- canvas = Canvas(root, width=canvasWidth, height=canvasHeight)
- root.canvas = canvas.canvas = canvas
- canvas.data = {}
- canvas.data["margin"] = margin
- canvas.data["cellSize"] = cellSize
- canvas.data["canvasWidth"] = canvasWidth
- canvas.data["canvasHeight"] = canvasHeight
- canvas.data["rows"] = rows
- canvas.data["cols"] = cols
- canvas.pack()
- run(8, 16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement