Advertisement
here2share

# Tk_Slider_Puzzle.py

Oct 20th, 2016
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 KB | None | 0 0
  1. # coding: utf-8
  2. # Tk_Slider_Puzzle.py
  3. #
  4. # Tkinter Slider Puzzle -- A classic game
  5. #
  6. #  1  2  3  4  5  6
  7. #  7  8  9 10 11 12
  8. # 13 14 15 16 17 18
  9. # 19 20 21 22 23 24
  10. # 25 26 27 28 29  .
  11. #
  12. #
  13. # Written by Thierry Bousch <bousch@topo.math.u-psud.fr> and placed
  14. # in the public domain.
  15.  
  16. from Tkinter import *
  17. from Canvas import *
  18. from random import *
  19.  
  20. BACKGROUND = 'orange'
  21. TSIZE = 90
  22. MARGIN = 5
  23. FONT = '-bitstream-charter-bold-r-normal--70-0-0-0-p-0-iso8859-1'
  24. LINES = 5
  25. COLUMNS = 6
  26.  
  27. def permute(array):
  28.   # Generate an even permutation, randomly
  29.   n = len(array)
  30.   signature = +1
  31.   for i in range(n-2):
  32.     j = randint(1, n)-1
  33.     if j <> i:
  34.       signature = -signature
  35.       array[i], array[j] = array[j], array[i]
  36.   # There's no choice for the last two elements, if we want
  37.   # an even permutation
  38.   if signature < 0:
  39.     array[n-2], array[n-1] = array[n-1], array[n-2]
  40.  
  41. #
  42. # Class definitions
  43. #
  44.  
  45. class Tile:
  46.   def __init__(self, text, canvas):
  47.     self.rect = Rectangle(canvas, 0, 0, TSIZE, TSIZE,
  48.     outline='black', fill='white')
  49.     self.text = CanvasText(canvas, TSIZE/2, TSIZE/2, anchor=CENTER,
  50.     text=text, font=FONT)
  51.     self.group = Group(canvas)
  52.     self.group.addtag_withtag(self.rect)
  53.     self.group.addtag_withtag(self.text)
  54.     self.group.move(MARGIN, MARGIN)
  55.     self.line = self.column = 0
  56.  
  57.   def moveto(self, line, column):
  58.     S = MARGIN+TSIZE
  59.     self.group.move(S*(column-self.column), S*(line-self.line))
  60.     self.line = line
  61.     self.column = column
  62.  
  63. class Counter(Label):
  64.   def __init__(self, parent, template):
  65.     Label.__init__(self, parent)
  66.     self.template = template
  67.     self.n = 0
  68.     self.redisplay()
  69.  
  70.   def reset(self):
  71.     self.n = 0
  72.     self.redisplay()
  73.  
  74.   def increment(self):
  75.     self.n = self.n + 1
  76.     self.redisplay()
  77.  
  78.   def redisplay(self):
  79.     self['text'] = self.template % self.n
  80.  
  81.  
  82. class Board:
  83.   def __init__(self, root, counter, lines, columns, bg):
  84.     S = MARGIN+TSIZE
  85.     self.lines = lines
  86.     self.columns = columns
  87.     self.counter = counter
  88.     self.canvas = Canvas(root, background=bg,
  89.       height=MARGIN+S*lines, width=MARGIN+S*columns)
  90.     self.tiles = []
  91.     for t in range(lines*columns-1):
  92.       tile = Tile(`t+1`, self.canvas)
  93.       self.tiles.append(tile)
  94.     self.shuffle()
  95.     self.canvas.bind('<1>', self.clickhandler)
  96.     self.canvas.pack()
  97.  
  98.   def shuffle(self):
  99.     permute(self.tiles)
  100.     for t in range(len(self.tiles)):
  101.       i,j = divmod(t, self.columns)
  102.       self.tiles[t].moveto(i,j)
  103.     self.hl = self.lines-1
  104.     self.hc = self.columns-1
  105.     self.counter.reset()
  106.  
  107.   def clickhandler(self, event):
  108.     S = MARGIN+TSIZE
  109.     line   = (event.y - MARGIN/2) / S
  110.     column = (event.x - MARGIN/2) / S
  111.     line = max(line, 0)
  112.     line = min(line, self.lines-1)
  113.     column = max(column, 0)
  114.     column = min(column, self.columns-1)
  115.     if line == self.hl:
  116.       if column <> self.hc:
  117.         self.move_line(line, column)
  118.     else:
  119.       if column == self.hc:
  120.         self.move_column(line, column)
  121.  
  122.   def move_line(self, l, c):
  123.     hc = self.hc
  124.     n = abs(hc - c)
  125.     if c < hc:
  126.       for i in range(n):
  127.         self.move_tile(l, hc-1-i, 0, 1)
  128.     else:
  129.       for i in range(n):
  130.         self.move_tile(l, hc+1+i, 0, -1)
  131.     self.counter.increment()
  132.  
  133.   def move_column(self, l, c):
  134.     hl = self.hl
  135.     n = abs(hl - l)
  136.     if l < hl:
  137.       for i in range(n):
  138.         self.move_tile(hl-1-i, c, 1, 0)
  139.     else:
  140.       for i in range(n):
  141.         self.move_tile(hl+1+i, c, -1, 0)
  142.     self.counter.increment()
  143.  
  144.   def move_tile(self, l, c, dl, dc):
  145.     for tile in self.tiles:
  146.       if tile.line == l and tile.column == c:
  147.         tile.moveto(l+dl, c+dc)
  148.         self.hl = l
  149.         self.hc = c
  150.  
  151.   def bg_color(self, color):
  152.     self.canvas['background'] = color
  153.  
  154.  
  155. class Game:
  156.   def __init__(self, lines, columns, bg):
  157.     self.win = Frame()
  158.     self.win.master.title('Taquin')
  159.     self.make_menubar()
  160.     self.board = Board(self.win, self.counter, lines, columns, bg)
  161.     self.make_menu_options()
  162.     self.make_menu_colors()
  163.     self.win.pack()
  164.     self.win.mainloop()
  165.  
  166.   def make_menubar(self):
  167.     self.menubar = Frame(self.win, relief=RAISED, bd=2)
  168.     self.menubutton1 = Menubutton(self.menubar, text='Options')
  169.     self.menubutton1.pack(side=LEFT)
  170.     self.menubutton2 = Menubutton(self.menubar, text='Colors')
  171.     self.menubutton2.pack(side=LEFT)
  172.     self.counter = Counter(self.menubar, 'Moves: %d')
  173.     self.counter.pack(side=RIGHT)
  174.     self.menubar.pack(side=TOP, fill=X)
  175.  
  176.   def make_menu_options(self):
  177.     menu = Menu(self.menubutton1)
  178.     menu.add_command(label='About this program...', command=self.about)
  179.     menu.add_command(label='New game', command=self.board.shuffle)
  180.     menu.add_command(label='Quit', command=self.win.quit)
  181.     self.menubutton1['menu'] = menu
  182.  
  183.   def make_menu_colors(self):
  184.     menu = Menu(self.menubutton2)
  185.     menu.add_command(label='Burlywood', command=self.bg_burlywood)
  186.     menu.add_command(label='Forest Green', command=self.bg_fgreen)
  187.     menu.add_command(label='Orange', command=self.bg_orange)
  188.     self.menubutton2['menu'] = menu
  189.  
  190.   def about(self):
  191.     from Dialog import Dialog
  192.     Dialog(self.win, title='Program Info', text="Tkinter Slider Puzzle!",
  193.       bitmap='info', default=0, strings=('OK',))
  194.  
  195.   def bg_burlywood(self):
  196.     self.board.bg_color('burlywood')
  197.  
  198.   def bg_fgreen(self):
  199.     self.board.bg_color('Forest Green')
  200.  
  201.   def bg_orange(self):
  202.     self.board.bg_color('orange')
  203.  
  204. Game(LINES, COLUMNS, BACKGROUND)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement