Question & Answer: Purpose: Familiarize yourself with some basic list syntax and usage. Degree of Difficulty: Easy For this question, the theme is fun with polkadots! You'll w…..

Language: Python

Program required: Processing 3.0

Question 1(9 points): Purpose: Familiarize yourself with some basic list syntax and usage. Degree of Difficulty: Easy For this question, the theme is fun with polkadots! Youll write a program where the user can click the mouse to place circles on the canvas, and with the press of a key. all of the circles will randomly change color. Here is how your program should behave Start with an empty 400x400 canvas with a light grey background When the user clicks the mouse: A new white circle appears at the location where the mouse was clickd. . When the user presses the pkey: All of the circles on the canvas change color to a random color!its okay if, by random chance, a circle remains the same color as before) Figure 1: The canvas after creating 5 circles with mouse-clicks (left) and the same canvas again after hitting p (right). Here are some hints on how to design your program. Keep in mind that, as usual, sticking to the Model- View-Controller design style will make your life FAR easier! . Your model will need to include some list(s) for storing the (x, y) coordinates of all the circles (similar to the planet exercise from the Chapter 14 lecture slides) . Your model should also include some list(s) for storing the current color of every circle. You might want to use 3 separate lists for this-one list for each of the red, green and blue color values-or you might not, its up to you. . As a reminder. you can use the function call random(0, 256) to return a random floating point number from O to 255. Do this when you need to generate a random color value.

Purpose: Familiarize yourself with some basic list syntax and usage. Degree of Difficulty: Easy For this question, the theme is fun with polkadots! You’ll write a program where the user can click the mouse to place circles on the canvas, and with the press of a key, all of the circles will randomly change color. Here is how your program should behave: Start with an empty 400 times 400 canvas with a light grey background When the user clicks the mouse: A new white circle appears at the location where the mouse was clicked. When the user presses the ‘p’ key: All of the circles on the canvas change color to a random color!(it’s okay if, by random chance, a circle remains the same color as before) Here are some hints on how to design your program. Keep in mind that, as usual, sticking to the Model- View-Controller design style will make your life FAR easier! Your model will need to include some list(s) for storing the (x, y) coordinates of all the circles (similar to the planet exercise from the Chapter 14 lecture slides) Your model should also include some list(s) for storing the current color of every circle. You might want to use 3 separate lists for this – one list for each of the red, green and blue color values – or you might not. it’s up to you. As a reminder, you can use the function call random(0, 256) to return a random floating point number from 0 to 255. Do this when you need to generate a random color value.

Expert Answer

 

Explanation::

  • All information is provided in comment format in python code::

​Code in python::

"""
    Importing tkinter and random class to generate random numbers for colors.
"""
from tkinter import*
from random import randint

root = Tk()

"""
    Creating list named circleCoordinates to store tuples of (x,y) 
    And below canvas of size 400x400 is created with background of grey color
    I am using Tkinter to implement this program.

"""
circleCoordinates=[]
C = Canvas(root, bg="grey", height=400, width=400)

def mouseClick(event):
    """
        This function is called when mouse is clicked whether right,left or middle.
         x and y coordinates are stored in list.
    """
    #this statement set the focus of cursor on canvas.
    C.focus_set()
    x, y = event.x, event.y
    # coords store x and y in tuple and this tuple is appended to the list cricleCoordinates.
    coords=tuple([x,y])
    circleCoordinates.append(coords)
    """
        To create circles on canvas we need four parameter to pass in create_oval() function.
         We have center coordinates. Imagine circle in this square with center in middle.
          create_oval takes two coordinates i.e top left corner and bottom right corner coordinates.
        ------------
        |           |
        |           |
        |           |
        |           |
        ------------
        So below I have created four variables to get circle of radius 5 pixels
        
    """
    leftX=x-5
    leftY=y+5
    rightX=x+5
    rightY=y-5
    C.create_oval(leftX, leftY, rightX, rightY, fill='white')
def key(event):
    """
    Whenever key function is called it runs through list of circleCoordinates and
    generate three random numbers in range of 0 to 255

    """

    for x,y in circleCoordinates:
        leftX = x - 5
        leftY = y + 5
        rightX = x + 5
        rightY = y - 5
        """
        Below code generates random numbers
        and # is assigned to color string and is applied to fill in create_oval
        """
        red = ("%02x" % randint(0,255))
        green = ("%02x" % randint(0,255))
        blue = ("%02x" % randint(0,255))
        ge = "#"
        color = ge + red + green + blue
        C.create_oval(leftX, leftY, rightX, rightY, fill=color)


C.bind("<Key>",key)
C.bind("<Button-1>",mouseClick)
C.bind("<Button-2>",mouseClick)
C.bind("<Button-3>",mouseClick)
C.pack()
root.mainloop()

Output::

Question & Answer: Purpose: Familiarize yourself with some basic list syntax and usage. Degree of Difficulty: Easy For this question, the theme is fun with polkadots! You'll w..... 1

Question & Answer: Purpose: Familiarize yourself with some basic list syntax and usage. Degree of Difficulty: Easy For this question, the theme is fun with polkadots! You'll w..... 2

Pics of code identation problem::

Question & Answer: Purpose: Familiarize yourself with some basic list syntax and usage. Degree of Difficulty: Easy For this question, the theme is fun with polkadots! You'll w..... 3Question & Answer: Purpose: Familiarize yourself with some basic list syntax and usage. Degree of Difficulty: Easy For this question, the theme is fun with polkadots! You'll w..... 4

Still stressed from student homework?
Get quality assistance from academic writers!