Just like the title says. I need help on clicking on a box in a grid and filling it in with a color. Right now each block is 10x10 so I would have to use a 9x9 rectangle so it doesn't overlap.

#!/usr/bin/python

import pygame,os,sys
from pygame.locals import *


xres = 640
yres = 480
screen = pygame.display.set_mode((xres, yres))
screen.fill((255,255,255))




def main():
    x = 10
    y = 10

#  pygame.draw.rect(screen,(0,0,255),(50,50,50,50),1)

#vertical lines
    pygame.draw.line(screen,(255,0,0),(x,0),(x,yres),1)
    
#horizontal lines
    pygame.draw.line(screen,(255,0,0),(0,10),(xres,y),1)
    
    pygame.display.flip()
    done = 0
    
    for x in range(0,640,10):
        x = x + 10
        pygame.draw.line(screen,(255,0,0),(x,0),(x,yres),1)

        
        
    for y in range(0,480,10):
        y = y + 10
        pygame.draw.line(screen,(255,0,0),(0,y), (xres,y),1)
        pygame.display.flip()
        done = 0
        
    while 1:
        for e in pygame.event.get():

            if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
                done = 1
                break
            if e.type == MOUSEBUTTONDOWN:
                 
                 if e.button == 1:
                     print "left button clicked"
                 elif e.button == 2:
                     print "middle button clicked"
                 elif e.button == 3:
                     print "right button clicked"
                 elif e.button == 4:
                     print "scrolling forward"
                 elif e.button == 5:
                     print "scrolling backward"
                 else:
                     print "some cool button"
                 print e.pos
        if done:
            break


if __name__ == "__main__":
   main()

Also is there a better way to draw the grid?

okay I'm a little further. I added in the following

xp = (e.pos[0]/10)*10  #0 is the x position
        yp = (e.pos[1]/10)*10  #1 is the y position
        rectange = (xp,yp,10,10)
        pygame.draw.rect(screen, (0, 255, 0), (xp, yp, 9, 9))
        pygame.display.flip()

which fills in the rectangle however I'm trying to figure out how to do it upon a mouse click. Also whenever it leaves the area it crashes. :\

hah I'm an idiot, I just put it under the left click :P

Thanks for letting us know!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.