-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
87 lines (65 loc) · 2.59 KB
/
grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from Tkinter import *
from geometry import Point
from PIL import Image, ImageTk
#upgraded form of Canvas.
#Canvas that can be used to draw grid-locked sprites.
#(0,0) is the lower left of the grid.
def get_sprite(name):
"""
creates a TkImage instance from the given name.
each name should correspond to an png in the images directory, minus the file extension.
"""
return ImageTk.PhotoImage(Image.open("images/{}.png".format(name)))
class Grid(Canvas):
def __init__(self, root, **kargs):
self.length = kargs.pop("length", 16)
self.cols = kargs.pop("cols", 10)
self.rows = kargs.pop("rows", 22) #typically 16 to 24
self.margin = 10
width = self.cols * self.length + self.margin * 2
height = self.rows * self.length + self.margin * 2
bg = kargs.pop("background_color", (255,255,255))
kargs["width"] = width
kargs["height"] = height
Canvas.__init__(self, root, **kargs)
self.background = self.create_rectangle(self.margin, self.margin, width-self.margin, height-self.margin)
self.set_background(bg)
#maps sprite names to ImageTk instances.
#don't let these get garbage collected while the Canvas is alive!
self.sprites = {}
#maps points to canvas ids
self.tile_ids = {}
#maps points to sprite names
self.tile_names = {}
def set(self, p, name):
if name == None:
self.remove(p)
return
self.tile_names[p] = name
if name not in self.sprites:
self.sprites[name] = get_sprite(name)
if p not in self.tile_ids:
left = self.margin + self.length * p.x
top = self.margin + self.length * (self.rows - p.y - 1)
self.tile_ids[p] = self.create_image(left, top, image=self.sprites[name], anchor="nw")
else:
self.itemconfig(self.tile_ids[p], image=self.sprites[name])
def get(self, p):
return self.tile_names.get(p, None)
def set_background(self, color):
color = tuple(map(int, color))
self.background_color = color
bg_str = "#{:02X}{:02X}{:02X}".format(*color)
self.itemconfig(self.background, fill=bg_str)
def get_background(self):
return self.background_color
def remove(self, p):
if p not in self.tile_names:
return
self.delete(self.tile_ids[p])
del self.tile_ids[p]
del self.tile_names[p]
def clear(self):
for i in range(self.cols):
for j in range(self.rows):
self.set(Point(i,j), None)