2

I am trying to make a draw program in pygame for a school project. In this module, i am intending for the user to press down on the mouse and draw a line on the surface. If a person presses down on a rect, the color that the person selected is the color that the drawn line will be. For some reason, the variable can change but even if i press down the mouse, no line is drawn. Here's the code:

def painting_module():
     running = True
     while running:
        #clock for timingn processes
     Clock.tick(FPS)
     # Process input (event)
    for event in pygame.event.get():
        Mouse_location = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        displacement_val = pygame.mouse.get_rel()
        color_to_paint_with = (0,0,0)
        if event.type == pygame.QUIT:
            running = False
        if click[0] == 1:
            # Intended to select colors if pressed, draw a line if the mouse is not on the rect
            if red_rect.collidepoint(Mouse_location):
                color_to_paint_with = RED
                print "Red"
                print color_to_paint_with
            if green_rect.collidepoint(Mouse_location):
                color_to_paint_with = GREEN
                print "red"
                print color_to_paint_with
            if blue_rect.collidepoint(Mouse_location):
                color_to_paint_with = BLUE
                print "red"
                print color_to_paint_with
            if gray_rect.collidepoint(Mouse_location):
                color_to_paint_with = GRAY
                print "red"
                print color_to_paint_with
            if eraser_ivory_rect.collidepoint(Mouse_location):
                color_to_paint_with = IVORY
                print "red"
                print color_to_paint_with
                  #draws the line
            pygame.draw.line(Screen, color_to_paint_with, Mouse_location, (Mouse_location[0] + displacement_val[0], Mouse_location[1] + displacement_val[1]))
programmer
  • 21
  • 3

1 Answers1

0

What I would do is

painting_module()

def painting_module():
 running = True
 while running:
    #clock for timingn processes
 Clock.tick(FPS)
 # Process input (event)
for event in pygame.event.get():
    Mouse_location = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    displacement_val = pygame.mouse.get_rel()
    color_to_paint_with = (0,0,0)
    if event.type == pygame.QUIT:
        running = False
    if click[0] == 1:
        # Intended to select colors if pressed, draw a line if the mouse is not on the rect
        if red_rect.collidepoint(Mouse_location):
            color_to_paint_with = RED
            print "Red"
            print color_to_paint_with
            // Call drawloop here
        if green_rect.collidepoint(Mouse_location):
            color_to_paint_with = GREEN
            print "red"
            print color_to_paint_with
        if blue_rect.collidepoint(Mouse_location):
            color_to_paint_with = BLUE
            print "red"
            print color_to_paint_with
        if gray_rect.collidepoint(Mouse_location):
            color_to_paint_with = GRAY
            print "red"
            print color_to_paint_with
        if eraser_ivory_rect.collidepoint(Mouse_location):
            color_to_paint_with = IVORY
            print "red"
            print color_to_paint_with

def drawline(color_to_paint_with, Mouse_location, displacement_val):
    for event in pygame.event.get():
        while pygame.mouse.get_pressed()[0] == 1:
            pygame.draw.line(Screen, color_to_paint_with, Mouse_location, (Mouse_location[0] + displacement_val[0], Mouse_location[1] + displacement_val[1]))
            pygame.display.flip()

I would replace the "if pygame.mouse.get_pressed():" code block with this.

Akinni
  • 67
  • 1
  • 14
  • Well, i still got to select colors. When i press down a rect, i select a color. This variable is then passed to the pygame.draw.line. I like how you used the while loop, but how can i select colors. – programmer Sep 17 '16 at 15:24
  • I guess before the loop, I would use the code you did and have it select it the colors the same way. Then I would put this while loop in a function that takes a tuple(color_to_paint_with) as the parameter and call this function when a color is selected. – Akinni Sep 17 '16 at 16:14
  • I hope this helps – Akinni Sep 17 '16 at 16:18
  • You mean the function drawline(color_to_paint_with, Mouse_location, displacement_val) – programmer Sep 17 '16 at 16:37
  • Yes. I would call it where I said "// Call drawloophere" – Akinni Sep 18 '16 at 00:35