不使用numpy。
如果我知道元素的大小和网格大小,是否可以找出网格左上角的坐标?
rect的大小:62x50 WxH(g_height,g_width)
网格:12x12
"""self.row/column is triggered by mouse position at a click coordinate"""
def draw_grid(self):
for self.row in range(12):
for self.column in range(12):
color = Game.BLUE
if self.grid[self.row][self.column] == 1:
color = Game.RED
pygame.draw.rect(self.screen,color,\
[(self.margin+self.g_width)*self.column+self.margin,\
(self.margin+self.g_height)*self.row+self.margin, \
self.g_width,self.g_height])
我可以找出我制作并放在屏幕上的矩形的确切坐标吗?
最终,我想用表面图像替换这些矩形。
答案 0 :(得分:1)
是粗略的......
[(self.margin+self.g_width)*self.column+self.margin,\
(self.margin+self.g_height)*self.row+self.margin, \
self.g_width,self.g_height]
是你的矩形......
它可以重写为[x,y,width,height]
...只需将它们保存到以后的二维数组中......
def draw_grid(self):
my_rects = []
for self.row in range(12):
my_rects.append([])
for self.column in range(12):
color = Game.BLUE
if self.grid[self.row][self.column] == 1:
color = Game.RED
x,y = (self.margin+self.g_width)*self.column+self.margin,\
(self.margin+self.g_height)*self.row+self.margin
width,height = self.g_width,self.g_height
my_rect = Rect(x,y,w,h)
my_rects[-1].append(my_rect)
pygame.draw.rect(self.screen,color,\
my_rect)
print my_rects[0][0]