def __init__(self, suit, value, canvas): """Card constructor. Arguments are the card's suit and value, and the canvas widget. The card is created at position (0, 0), with its face down (adding it to a stack will position it according to that stack's rules). """ self.suit = suit self.value = value self.color = COLOR[suit] self.face_shown = 0 self.x = self.y = 0 self.group = Group(canvas) text = "%s %s" % (VALNAMES[value], suit) self.__text = CanvasText(canvas, CARDWIDTH/2, 0, anchor=N, fill=self.color, text=text) self.group.addtag_withtag(self.__text) self.__rect = Rectangle(canvas, 0, 0, CARDWIDTH, CARDHEIGHT, outline='black', fill='white') self.group.addtag_withtag(self.__rect) self.__back = Rectangle(canvas, MARGIN, MARGIN, CARDWIDTH-MARGIN, CARDHEIGHT-MARGIN, outline='black', fill='blue') self.group.addtag_withtag(self.__back)
def __init__(self, array, index, value): self.array = array self.index = index self.value = value x1, y1, x2, y2 = self.position() self.item = Rectangle(array.canvas, x1, y1, x2, y2, fill='red', outline='black', width=1) self.item.bind('<Button-1>', self.mouse_down) self.item.bind('<Button1-Motion>', self.mouse_move) self.item.bind('<ButtonRelease-1>', self.mouse_up)
def __init__(self, board): """Initializes graphic display of a rectangular gameboard.""" # Properties of gameboard dw = self.dotwidth = 6 sw = self.squarewidth = 60 sk = self.skip = 4 fw = self.fieldwidth = dw + sw + 2 * sk ins = self.inset = sw / 2 self.barcolors = ['red', 'forest green'] self.squarecolors = ['orange red', 'lime green'] # Construct Canvas self.board = board width, height = board.width, board.height # compute size of canvas: w = width * fw h = height * fw self.root = Tk() cv = self.cv = Canvas(self.root, width=w, height=h, bg='white') cv.pack() # Put geometrical objects - dots, bars and squares - on canvas self.bars = {} self.squares = {} for dot in cartesian(range(width), range(height)): # dots. Never used again Rectangle(cv, ins + dot[0] * fw, ins + dot[1] * fw, ins + dot[0] * fw + dw, ins + dot[1] * fw + dw, fill='black', outline='') # horizontal bars if dot[0] < width - 1: x0 = ins + dot[0] * fw + dw + sk y0 = ins + dot[1] * fw self.bars[(dot,right(dot))] =\ Rectangle(cv,x0,y0,x0+sw,y0+dw,fill='lightgray',outline='') # vertical bars if dot[1] < height - 1: x0 = ins + dot[0] * fw y0 = ins + dot[1] * fw + dw + sk self.bars[(dot,upper(dot))] =\ Rectangle(cv,x0,y0,x0+dw,y0+sw,fill='lightgray',outline='') # squares if (dot[0] < width - 1) and (dot[1] < height - 1): x0 = ins + dot[0] * fw + dw + sk y0 = ins + dot[1] * fw + dw + sk self.squares[dot] = \ Rectangle(cv,x0,y0,x0+sw,y0+sw,fill='whitesmoke',outline='') cv.update() self._nextturn() self.root.mainloop()
def makebottom(self): bottom = Rectangle(self.game.canvas, self.x, self.y, self.x + CARDWIDTH, self.y + CARDHEIGHT, outline='black', fill='')
def plotting(xData,yData,symb,Ax,Bx,Ay,By): # Plots the data global cv for i in range(len(xData)-1): x1 = round(Ax + Bx*xData[i]) y1 = round(Ay + By*yData[i]) x2 = round(Ax + Bx*xData[i+1]) y2 = round(Ay + By*yData[i+1]) if symb[0] == 'l': Line(cv,x1,y1,x2,y2) if symb[1] == 'o': Oval(cv,x1-3,y1-3,x1+3,y1+3,fill="white") if symb[1] == 'b': Rectangle(cv,x1-3,y1-3,x1+3,y1+3,fill="white") if symb[1] == 'o': Oval(cv,x2-3,y2-3,x2+3,y2+3,fill="white") if symb[1] == 'b': Rectangle(cv,x2-3,y2-3,x2+3,y2+3,fill="white")
def makebottom(self): bottom = Rectangle(self.game.canvas, self.x, self.y, self.x + CARDWIDTH, self.y + CARDHEIGHT, outline='black', fill=BACKGROUND) self.group.addtag_withtag(bottom)
#! /usr/bin/env python
class ArrayItem: def __init__(self, array, index, value): self.array = array self.index = index self.value = value x1, y1, x2, y2 = self.position() self.item = Rectangle(array.canvas, x1, y1, x2, y2, fill='red', outline='black', width=1) self.item.bind('<Button-1>', self.mouse_down) self.item.bind('<Button1-Motion>', self.mouse_move) self.item.bind('<ButtonRelease-1>', self.mouse_up) def delete(self): item = self.item self.array = None self.item = None item.delete() def mouse_down(self, event): self.lastx = event.x self.lasty = event.y self.origx = event.x self.origy = event.y self.item.tkraise() def mouse_move(self, event): self.item.move(event.x - self.lastx, event.y - self.lasty) self.lastx = event.x self.lasty = event.y def mouse_up(self, event): i = self.nearestindex(event.x) if i >= self.array.getsize(): i = self.array.getsize() - 1 if i < 0: i = 0 other = self.array.items[i] here = self.index self.array.items[here], self.array.items[i] = other, self self.index = i x1, y1, x2, y2 = self.position() self.item.coords(((x1, y1), (x2, y2))) other.setindex(here) def setindex(self, index): nsteps = steps(self.index, index) if not nsteps: return if self.array.speed == "fastest": nsteps = 0 oldpts = self.position() self.index = index newpts = self.position() trajectory = interpolate(oldpts, newpts, nsteps) self.item.tkraise() for pts in trajectory: self.item.coords((pts[:2], pts[2:])) self.array.wait(50) def swapwith(self, other): nsteps = steps(self.index, other.index) if not nsteps: return if self.array.speed == "fastest": nsteps = 0 myoldpts = self.position() otheroldpts = other.position() self.index, other.index = other.index, self.index mynewpts = self.position() othernewpts = other.position() myfill = self.item['fill'] otherfill = other.item['fill'] self.item.config(fill='green') other.item.config(fill='yellow') self.array.master.update() if self.array.speed == "single-step": self.item.coords((mynewpts[:2], mynewpts[2:])) other.item.coords((othernewpts[:2], othernewpts[2:])) self.array.master.update() self.item.config(fill=myfill) other.item.config(fill=otherfill) self.array.wait(0) return mytrajectory = interpolate(myoldpts, mynewpts, nsteps) othertrajectory = interpolate(otheroldpts, othernewpts, nsteps) if self.value > other.value: self.item.tkraise() other.item.tkraise() else: other.item.tkraise() self.item.tkraise() try: for i in range(len(mytrajectory)): mypts = mytrajectory[i] otherpts = othertrajectory[i] self.item.coords((mypts[:2], mypts[2:])) other.item.coords((otherpts[:2], otherpts[2:])) self.array.wait(50) finally: mypts = mytrajectory[-1] otherpts = othertrajectory[-1] self.item.coords((mypts[:2], mypts[2:])) other.item.coords((otherpts[:2], otherpts[2:])) self.item.config(fill=myfill) other.item.config(fill=otherfill) def compareto(self, other): myfill = self.item['fill'] otherfill = other.item['fill'] outcome = cmp(self.value, other.value) if outcome < 0: myflash = 'white' otherflash = 'black' elif outcome > 0: myflash = 'black' otherflash = 'white' else: myflash = otherflash = 'grey' try: self.item.config(fill=myflash) other.item.config(fill=otherflash) self.array.wait(500) finally: self.item.config(fill=myfill) other.item.config(fill=otherfill) return outcome def position(self): x1 = (self.index+1)*XGRID - WIDTH/2 x2 = x1+WIDTH y2 = (self.array.maxvalue+1)*YGRID y1 = y2 - (self.value)*YGRID return x1, y1, x2, y2 def nearestindex(self, x): return int(round(float(x)/XGRID)) - 1
class Card: """A playing card. A card doesn't record to which stack it belongs; only the stack records this (it turns out that we always know this from the context, and this saves a ``double update'' with potential for inconsistencies). Public methods: moveto(x, y) -- move the card to an absolute position moveby(dx, dy) -- move the card by a relative offset tkraise() -- raise the card to the top of its stack showface(), showback() -- turn the card face up or down & raise it Public read-only instance variables: suit, value, color -- the card's suit, value and color face_shown -- true when the card is shown face up, else false Semi-public read-only instance variables (XXX should be made private): group -- the Canvas.Group representing the card x, y -- the position of the card's top left corner Private instance variables: __back, __rect, __text -- the canvas items making up the card (To show the card face up, the text item is placed in front of rect and the back is placed behind it. To show it face down, this is reversed. The card is created face down.) """ def __init__(self, suit, value, canvas): """Card constructor. Arguments are the card's suit and value, and the canvas widget. The card is created at position (0, 0), with its face down (adding it to a stack will position it according to that stack's rules). """ self.suit = suit self.value = value self.color = COLOR[suit] self.face_shown = 0 self.x = self.y = 0 self.group = Group(canvas) text = "%s %s" % (VALNAMES[value], suit) self.__text = CanvasText(canvas, CARDWIDTH/2, 0, anchor=N, fill=self.color, text=text) self.group.addtag_withtag(self.__text) self.__rect = Rectangle(canvas, 0, 0, CARDWIDTH, CARDHEIGHT, outline='black', fill='white') self.group.addtag_withtag(self.__rect) self.__back = Rectangle(canvas, MARGIN, MARGIN, CARDWIDTH-MARGIN, CARDHEIGHT-MARGIN, outline='black', fill='blue') self.group.addtag_withtag(self.__back) def __repr__(self): """Return a string for debug print statements.""" return "Card(%r, %r)" % (self.suit, self.value) def moveto(self, x, y): """Move the card to absolute position (x, y).""" self.moveby(x - self.x, y - self.y) def moveby(self, dx, dy): """Move the card by (dx, dy).""" self.x = self.x + dx self.y = self.y + dy self.group.move(dx, dy) def tkraise(self): """Raise the card above all other objects in its canvas.""" self.group.tkraise() def showface(self): """Turn the card's face up.""" self.tkraise() self.__rect.tkraise() self.__text.tkraise() self.face_shown = 1 def showback(self): """Turn the card's face down.""" self.tkraise() self.__rect.tkraise() self.__back.tkraise() self.face_shown = 0
class Card: """A playing card. A card doesn't record to which stack it belongs; only the stack records this (it turns out that we always know this from the context, and this saves a ``double update'' with potential for inconsistencies). Public methods: moveto(x, y) -- move the card to an absolute position moveby(dx, dy) -- move the card by a relative offset tkraise() -- raise the card to the top of its stack showface(), showback() -- turn the card face up or down & raise it Public read-only instance variables: suit, value, color -- the card's suit, value and color face_shown -- true when the card is shown face up, else false Semi-public read-only instance variables (XXX should be made private): group -- the Canvas.Group representing the card x, y -- the position of the card's top left corner Private instance variables: __back, __rect, __text -- the canvas items making up the card (To show the card face up, the text item is placed in front of rect and the back is placed behind it. To show it face down, this is reversed. The card is created face down.) """ def __init__(self, suit, value, canvas): """Card constructor. Arguments are the card's suit and value, and the canvas widget. The card is created at position (0, 0), with its face down (adding it to a stack will position it according to that stack's rules). """ self.suit = suit self.value = value self.color = COLOR[suit] self.face_shown = 0 self.x = self.y = 0 self.group = Group(canvas) text = "%s %s" % (VALNAMES[value], suit) self.__text = CanvasText(canvas, CARDWIDTH / 2, 0, anchor=N, fill=self.color, text=text) self.group.addtag_withtag(self.__text) self.__rect = Rectangle(canvas, 0, 0, CARDWIDTH, CARDHEIGHT, outline='black', fill='white') self.group.addtag_withtag(self.__rect) self.__back = Rectangle(canvas, MARGIN, MARGIN, CARDWIDTH - MARGIN, CARDHEIGHT - MARGIN, outline='black', fill='blue') self.group.addtag_withtag(self.__back) def __repr__(self): """Return a string for debug print statements.""" return "Card(%r, %r)" % (self.suit, self.value) def moveto(self, x, y): """Move the card to absolute position (x, y).""" self.moveby(x - self.x, y - self.y) def moveby(self, dx, dy): """Move the card by (dx, dy).""" self.x = self.x + dx self.y = self.y + dy self.group.move(dx, dy) def tkraise(self): """Raise the card above all other objects in its canvas.""" self.group.tkraise() def showface(self): """Turn the card's face up.""" self.tkraise() self.__rect.tkraise() self.__text.tkraise() self.face_shown = 1 def showback(self): """Turn the card's face down.""" self.tkraise() self.__rect.tkraise() self.__back.tkraise() self.face_shown = 0
def xyPlot(*data): def dataRange(data): # Finds minimum and maximum values of data xMin = 1.0e12 xMax = -1.0e12 yMin = 1.0e12 yMax = -1.0e12 n = len(data)/3 for i in range(n): xData = data[3*i-3] yData = data[3*i-2] x1 = min(xData) if x1 < xMin: xMin = x1 x2 = max(xData) if x2 > xMax: xMax = x2 y1 = min(yData) if y1 < yMin: yMin = y1 y2 = max(yData) if y2 > yMax: yMax = y2 if yMin == yMax: if yMin > 0.0: yMin = 0.0 elif yMin < 0.0: yMax = 0.0 else: yMax = 1.0 return xMin,xMax,yMin,yMax def viewport(aMin,aMax): # Computes the size of the viewport logE = 0.4342945 np = int(log10(abs(aMax - aMin))) - 1 p = 10**np aMin = round(aMin/p - 0.49)*p aMax = round(aMax/p + 0.49)*p return aMin,aMax,p def xMap(xMin,xMax): # Computes parameters used in mapping x to # screen coordinates: xPixel = Ax + Bx*x Ax = leftMargin - width*xMin/(xMax - xMin) Bx = width/(xMax - xMin) return Ax,Bx def yMap(yMin,yMax): # Computes parameters used in mapping y to # screen coordinates: yPixel = Ay + By*y Ay = topMargin + height*yMax/(yMax - yMin) By = -height/(yMax - yMin) return Ay,By def tickSpace(aMin,aMax,p): # Computes tick spacing for the axes n = (aMax - aMin)/p if n <= 10: da = 1.0 elif (n > 11) and (n <= 20): da = 2.0 elif (n >= 21) and (n <= 40): da = 5.0 elif (n >= 41) and (n <= 70): da = 10.0 else: da = 20.0 return da*p def xTicks(xMin,xMax,Ax,Bx): # Plot ticks and labels for x-axis dx = tickSpace(xMin,xMax,px) yp = topMargin + height if xMin < 0.0: x = 0.0 while x >= xMin: if x <= xMax: xp = Ax + Bx*x Line(cv,xp,yp,xp,yp - 8) label = str(round(x,4)) CanvasText(cv,xp,yp+20,text=label) x = x - dx if xMax > 0.0: x = 0.0 while x <= xMax: if x >= xMin: xp = Ax + Bx*x Line(cv,xp,yp,xp,yp - 8) label = str(round(x,4)) CanvasText(cv,xp,yp+20,text=label) x = x + dx def yTicks(yMin,yMax,Ay,By): # Plot ticks and labels for y-axis dy = tickSpace(yMin,yMax,py) xp = leftMargin if yMin < 0.0: y = 0.0 while y >= yMin: if y <= yMax: yp = Ay + By*y Line(cv,xp,yp,xp+8,yp) label = str(round(y,4)) xxp = xp-10-len(label)*3 CanvasText(cv,xxp,yp,text=label) y = y - dy if yMax > 0.0: y = 0.0 while y <= yMax: if y >= yMin: yp = Ay + By*y Line(cv,xp,yp,xp+8,yp) label = str(round(y,4)) xxp = xp-10-len(label)*3 CanvasText(cv,xxp,yp,text=label) y = y + dy def axes(xMin,xMax,yMin,yMax,Ax,Ay): # Draws the x = 0 and y = 0 lines if xMin*xMax < 0: Line(cv,Ax,topMargin,Ax,topMargin+height) if yMin*yMax < 0: Line(cv,leftMargin,Ay,leftMargin+width,Ay) def plotting(xData,yData,symb,Ax,Bx,Ay,By): # Plots the data global cv for i in range(len(xData)-1): x1 = round(Ax + Bx*xData[i]) y1 = round(Ay + By*yData[i]) x2 = round(Ax + Bx*xData[i+1]) y2 = round(Ay + By*yData[i+1]) if symb[0] == 'l': Line(cv,x1,y1,x2,y2) if symb[1] == 'o': Oval(cv,x1-3,y1-3,x1+3,y1+3,fill="white") if symb[1] == 'b': Rectangle(cv,x1-3,y1-3,x1+3,y1+3,fill="white") if symb[1] == 'o': Oval(cv,x2-3,y2-3,x2+3,y2+3,fill="white") if symb[1] == 'b': Rectangle(cv,x2-3,y2-3,x2+3,y2+3,fill="white") W = 640 # Width of plot window H = 480 # Height of plot window leftMargin = 100 topMargin = 25 width = W - 150 height = H - 75 global cv root = Tk() cv = Canvas(root,width=W,height=H,background="white") cv.pack() Rectangle(cv,leftMargin,topMargin,width+leftMargin,\ height+topMargin) [xMin,xMax,yMin,yMax] = dataRange(data) [xMin,xMax,px] = viewport(xMin,xMax) [yMin,yMax,py] = viewport(yMin,yMax) [Ax,Bx] = xMap(xMin,xMax) [Ay,By] = yMap(yMin,yMax) axes(xMin,xMax,yMin,yMax,Ax,Ay) xTicks(xMin,xMax,Ax,Bx) yTicks(yMin,yMax,Ay,By) n = len(data)/3 for i in range(n): xData = data[3*i-3] yData = data[3*i-2] symb = data[3*i-1] plotting(xData,yData,symb,Ax,Bx,Ay,By) root.mainloop() return