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.


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

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

def main():
    x = 10
    y = 10

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

#vertical lines
#horizontal lines
    done = 0
    for x in range(0,640,10):
        x = x + 10

    for y in range(0,480,10):
        y = y + 10
        pygame.draw.line(screen,(255,0,0),(0,y), (xres,y),1)
        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
            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"
                     print "some cool button"
                 print e.pos
        if done:

if __name__ == "__main__":

Also is there a better way to draw the grid?

Recommended Answers

Thanks for letting us know!

Jump to Post

All 3 Replies

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))

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 learning and sharing knowledge.