Create Board Game-like Grid in Python -
i thinking of creating board game in python, 1 have grid of spaces, each different properties, , may or may not have pieces resting on them. these pieces should able move between spaces, though subject various rules. (chess or checkers examples of i'm thinking of, though game have different/more complicated rules, , grid may not square, if spaces are).
i wrote java implementation of similar data structures class, using modified version of linked lists. python, imagine there's better way (maybe library out there?)
drawing chessboard pretty trivial tkinter. here's simple example:
import tkinter tk class gameboard(tk.frame): def __init__(self, parent, rows=8, columns=8, size=32, color1="white", color2="blue"): '''size size of square, in pixels''' self.rows = rows self.columns = columns self.size = size self.color1 = color1 self.color2 = color2 self.pieces = {} canvas_width = columns * size canvas_height = rows * size tk.frame.__init__(self, parent) self.canvas = tk.canvas(self, borderwidth=0, highlightthickness=0, width=canvas_width, height=canvas_height, background="bisque") self.canvas.pack(side="top", fill="both", expand=true, padx=2, pady=2) # binding cause refresh if user interactively # changes window size self.canvas.bind("<configure>", self.refresh) def addpiece(self, name, image, row=0, column=0): '''add piece playing board''' self.canvas.create_image(0,0, image=image, tags=(name, "piece"), anchor="c") self.placepiece(name, row, column) def placepiece(self, name, row, column): '''place piece @ given row/column''' self.pieces[name] = (row, column) x0 = (column * self.size) + int(self.size/2) y0 = (row * self.size) + int(self.size/2) self.canvas.coords(name, x0, y0) def refresh(self, event): '''redraw board, possibly in response window being resized''' xsize = int((event.width-1) / self.columns) ysize = int((event.height-1) / self.rows) self.size = min(xsize, ysize) self.canvas.delete("square") color = self.color2 row in range(self.rows): color = self.color1 if color == self.color2 else self.color2 col in range(self.columns): x1 = (col * self.size) y1 = (row * self.size) x2 = x1 + self.size y2 = y1 + self.size self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=color, tags="square") color = self.color1 if color == self.color2 else self.color2 name in self.pieces: self.placepiece(name, self.pieces[name][0], self.pieces[name][1]) self.canvas.tag_raise("piece") self.canvas.tag_lower("square") # image comes silk icon set under creative commons # license. more information see http://www.famfamfam.com/lab/icons/silk/ imagedata = ''' r0lgodlheaaqaoesakx7fqx8f61/g62cilcjkriihm+halknmnciankkanomalurk7wovlwpv9er anisanuxan2zan6aan+baoccaokeancjkoshanknk+imaoyran6qsnaxpfcwaokyjokyjvkyanw0 r/s1apw2apw3apa4ape5apm7apm8apq8ao28ke29lo2/lo2/l+7bm+7bno6+re7cmu7boe7dnpha p+/foo/fo+jgs+/fqo/go/dhpojbdfdippdjqpdispdkqpdkrpdiuphlq/hlrermv/hmr/lnsovh fvlos/rnp/lptvlove/ldfprufpru/psu/lpapptvppuvftuvvlpe/lscptwwftxw/txxptxx/xy xu/skvxzypfvdfxay/tycfxazpxazvbwfvtye/xbbvhwl/bdapbeavvadffea/bebvffbfbdfpvb e/fgb/pam/fgcvfgeptbnfbcl/bfivfjdvfjepbemfjelpxeopjkepbfmvffnvbfofjlgffjkvfh nvjio/nnhvfjovjmlvzlmvrmpvrrmfzpp/zqq/vqr/zssvvvp/vvqfvvupvvuvvwvfzzwp////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////yh+funyzwf0zwqgd2l0acbu agugr0lnuaah+qqbcgd/acwaaaaaeaaqaaaizad/crxiskdbfydmlbhxcgaknikgptlupcpbjiua +vethswfpdqkokb0ye4amfiiopncj8pae20y6vntqmsubkwajkfyqacjrylcmofipymrhzv89kkg keskome8xhmcreiogc/2tbaowhgcaygkkbncwwkafncicashka4rahyk9maqwimmoq8edhbdkmun bqmefpigasorbqm1bgljriiogsxwbcmtoccmoxsw2hcbo8qwdqcvmmkzcncbhqga/qmgayidbqzu yxyyeaa7 ''' if __name__ == "__main__": root = tk.tk() board = gameboard(root) board.pack(side="top", fill="both", expand="true", padx=4, pady=4) player1 = tk.photoimage(data=imagedata) board.addpiece("player1", player1, 0,0) root.mainloop()
Comments
Post a Comment