使用python繪製棋盤
import mathimport matplotlib.patches as mpimport matplotlib.pyplot as pltfrom matplotlib.lines import Line2Dclass Gezi: def __init__(self, hang, lie, patch=None): self.Hang = hang self.Lie = lie self.Patch = patch def getwz(self): return self.Hang,self.Lie def __repr__(self): return "%02d,%02d"%(self.Hang,self.Lie)def HLtoXY(hangh, liel): y = 576 - 32 * hangh bc = 64 / 3 ** 0.5 x = 32 + bc * (liel - 1 + (hangh + 1) % 2 * 0.5) return x, yclass Qipan: def __init__(self): self.fig = plt.figure() self.ax = self.fig.add_subplot(111) self.fig.set_facecolor(#c0ffff) self.fig.set_dpi(120) self.color = [r, #00ff00, #ffffff, #ffff00, b, k] self.BigPoint = [] self.Clineadd = [] self.atthl={} self.顏色縮寫 = {gn: #00ff00, be: b, bk: k, rd: r, we: #ffffff, yw: #ffff00} for x in range(1, 18): h = 18 - x if x > 9 else x start = 7 - h // 2 if h < 5 else (h + 1) // 2 - 2 end = 7 + (h + 1) // 2 if h < 5 else 16 - h // 2 for y in range(start, end): patchtemp=mp.Circle(HLtoXY(x, y), 14, edgecolor=#6c6c6c,facecolor=#e0e0e0, linewidth=1.5, picker=True,fill=True, zorder=3) wangge[x][y] = Gezi(x,y,patchtemp) self.atthl[patchtemp]=wangge[x][y] self.ax.add_patch(patchtemp) self.BaseDraw() self.fig.canvas.mpl_connect(pick_event, self.pickcir) plt.show() def pickcir(self,event): print(mouse=%s,art=%s % (event.mouseevent, event.artist)) print(self.atthl[event.artist]) self.ax.patch def BaseDraw(self): self.fig.subplots_adjust(left=0, right=1, bottom=0, top=1) self.ax.set_facecolor(#00ffff) self.ax.set_xlim(0, 768) self.ax.set_ylim(0, 576) self.ax.axis(off) self.ax.add_patch(mp.Rectangle((0, 0), 768, 576, color=#c0ffff)) for x in range(6): center = TRtoXY((2 * x + 1) * math.pi / 6, 512 / 3) the = (x + 1) % 2 * math.pi self.ax.add_patch(mp.RegularPolygon(center, 3, 256 / 3, the, color=self.color[x])) for hang in range(1, 18): for lie in range(1, 14): for fx in ((0, 1), (1, 0), (1, 1 - hang % 2 * 2)): h0, l0 = hang + fx[0], lie + fx[1] try: if wangge[h0][l0] and wangge[hang][lie]: x1, y1 = HLtoXY(h0, l0) x0, y0 = HLtoXY(hang, lie) cl = #ffffff if lie > 14 - hang // 2 else k self.Clineadd.append( Line2D(xdata=[x0, x1], ydata=[y0, y1], linewidth=1, color=cl, zorder=1)) except: pass self.TrBiPo(1, 7, self.fig) self.ax.lines.extend(self.Clineadd) def TrBiPo(self, hang, lie, figr): self.BigPoint.append((hang, lie)) next_po = [] x0, y0 = HLtoXY(hang, lie) xiushi = [(4, -2), (4, 2), (0, -4), (0, 4)] for sub in xiushi: try: if wangge[hang + sub[0]][lie + sub[1]] and lie + sub[1] > 0: next_po.append((hang + sub[0], lie + sub[1])) except: None for BiPo in next_po: x1, y1 = HLtoXY(BiPo[0], BiPo[1]) self.Clineadd.append(Line2D(xdata=[x0, x1], ydata=[y0, y1], linewidth=3, color=#000000)) if not (BiPo[0], BiPo[1]) in self.BigPoint: self.TrBiPo(BiPo[0], BiPo[1], figr)def TRtoXY(thet, radiu, rad=True, x0=32 + 384 / 3 ** 0.5, y0=288): theta = thet if rad else thet * 180 / math.pi return radiu * math.cos(theta) + x0, radiu * math.sin(theta) + y0Qipan()
推薦閱讀:
※python爬蟲如何按序抓取一個頁面上的圖文?
※Python 代碼規範
※Python logging模塊入門及分線程輸出日誌文件的實現
※PYTHON黑帽編程 4.1 SNIFFER(嗅探器)之數據捕獲--補充
※PyQt5系列教程(26): 日曆(QCalendarWidget)