Question

Not using numpy.

If I know the size of my elements and the grid size, is it possible to find out what coordinate is on the top left corner of my grid?

Size of rect: 62x50 WxH (g_height, g_width)

Grid: 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])

Can I figure out the exact coordinates of the rectangles I have made and put on the screen?

Ultimately, I want to replace these rectangles I drew with a surface image.

Was it helpful?

Solution

yes of coarse ...

[(self.margin+self.g_width)*self.column+self.margin,\
                        (self.margin+self.g_height)*self.row+self.margin, \
                            self.g_width,self.g_height]

is your rectangle ....

it can be rewritten as [x,y,width,height] ... just save them for later in a 2d array ...

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top