How do i draw a line and change colors in pygame?
<p>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:</p>

<pre><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]))
</code></pre>

<p>What I would do is</p>

<pre><code>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()
</code></pre>

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

python python-2.7 pygame pygame-surface