Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame Help - Pydraw
#1
Hello everyone,

I am taking an introductory Python class and have actually been doing pretty well so far. The assignment I have to turn in this week, however, is significantly more difficult than the ones I have done up to this point. For this assignment, I have to create a program that defines three colors in a list, sets the size of the pen size (cursor), and changes color based on which mouse button is being pressed. Up to this point, we have not done anything as interactive as this and I am really struggling to complete this. I will paste the code below along with what I have done. Any help would be GREATLY appreciated.

(Note: The only areas I have filled in are below the "Steps" so Step 1-6. I have gone ahead and bolded these areas. The rest of the code was already there when I downloaded the assignment. This does actually run but nothing happens when I click in the box. I am not sure how to indent within the post here but everything is indented correctly.)

# Drag to Draw Pygame program
# By C. Calongne 01/21/19
# Developed with tips from Pygame.com & Teach Your Kids to Code by Payne, 2015

# Six steps to complete in M4Lab2
# Define a list to store the colors
# Define statements for each mouse button
# Within the program suite, draw on screen with a different color
# When your program runs, a black box appears. 
# Draw with a different color using the left, middle, and right cursor buttons
# The middle cursor is the scrollwheel. You can configure a random color.
# Close the draw window. In the Python shell, type exit() to quit


import pygame
import random # optional for this program, but useful for random colors, such as
# randcolor = random.randrange(0,255)

pygame.init()

# Defines the display size with a width and height of 600, 600
size = width, height = 600, 600


# Step 1: Define a colors list to describe 3 colors.
# Hint, use their RGB number values
colors = ['red', 'green', 'blue']
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)


screen = pygame.display.set_mode(size)
# Draws the screen using the size

pygame.display.set_caption("Click and drag to draw")
# Displays the title at the top of the draw screen

keep_going = True
# The program continues unti you close the window and type exit() in the shell

# Step 2: define a radius variable for your pen at 5, 10 or 15
# Test them to see which radius you prefer and use it.
radius = radius + 20
pen_size = pen_size + 5


mousedown = False
# we have not started drawing yet

while keep_going != False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going  = False
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            mousedown = True
        if event.type == pygame.MOUSEBUTTONUP:
            mousedown = False
            
    if mousedown: # start drawing
        spot = pygame.mouse.get_pos() # locate the pen's position as the 1st spot
        if pygame.mouse.get_pressed()[0]: # Boolean for button 1
            button_color = colors[0] # selects the first color from the list
            
        # Step 3: write the elif for pressing button 2, similar to button 1

        elif pygame.mouse.get_pressed()[1]:
        
            # Step 4: write the statement for button 2's color

            button_color = colors[1]
        
        # Step 5: write the else statement for button 3

        elif pygame.mouse.get_pressed()[2]:
        
            # Step 6: choose a color from the list

            button_color = colors[2]
     
           
        pygame.draw.circle(screen, button_color, spot, radius)
        # The pen is in the shape of a circle that draws on the screen
        # using the button color, the position of the spot, and the pen's radius

    pygame.display.update()
    # You can draw until you close the Pygame window

pygame.quit()

# Does the program continue to run?
# Type print(keep_going) or another variable to test it. 
# The program ends after you type exit() in the Python shell.

# For fun, try defining a random color and calling it for button_color
# randcolor = random.randrange(0,255)
Reply
#2
There were some bugs in the code. I fixed and commented them. Hope it helps.

import pygame
import random

pygame.init()

# Defines the display size with a width and height of 600, 600
width, height = 600, 600
size = (width, height)

screen = pygame.display.set_mode(size)
# Draws the screen using the size

pygame.display.set_caption("Click and drag to draw")
# Displays the title at the top of the draw screen

#colors = ['red', 'green', 'blue']     # this is wrong, this is not a list with colors but with strings of color names
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
colors = [red, green, blue]


keep_going = True
# The program continues until you close the window and type exit() in the shell

#radius = radius + 20    #   This line throws NameError: name 'radius' is not defined
radius = 10

#pen_size = pen_size + 5   # NameError: name 'pen_size' is not defined
pen_size = 5  # This variable is not used

mousedown = False   # we have not started drawing yet
keep_going = True   # The program continues unti you close the window and type exit() in the shell

# while keep_going != False:   # this can be done more pythonic
while keep_going:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            mousedown = True
            
        if event.type == pygame.MOUSEBUTTONUP:
            mousedown = False
    
    if mousedown: # start drawing
        spot = pygame.mouse.get_pos()
        if pygame.mouse.get_pressed()[0]:
            button_color = colors[0]
 
        elif pygame.mouse.get_pressed()[1]:
            button_color = colors[1]

        elif pygame.mouse.get_pressed()[2]:
            button_color = colors[2]

        pygame.draw.circle(screen, button_color, spot, radius)

    pygame.display.update()

pygame.quit()
Reply
#3
Wow! Thank you so much Thomas! That got it working perfectly. You're a life saver!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020