Question

In: Computer Science

How to make a python program that imports only turtle and math library where I can...

How to make a python program that imports only turtle and math library where I can click once on the screen to set the center of the square, move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point?

Solutions

Expert Solution

Comment if you have any issues or changes to be made.

Code:

import turtle

def get_mouse_click_coor(x, y):

    global centerClick,centerCoordinates

    if centerClick == 0:

        centerCoordinates = (x,y)

        centerClick = 1

    else:

        edgeCoordinates = (x,y)

        centerClick = 0

        drawSquare(centerCoordinates, edgeCoordinates)

def drawSquare(center, edge):

    #calculate difference

    x = abs(center[0] - edge[0])

    y = abs(center[1] - edge[1])

    #set edge length

    if y > x:

        sideLength = y * 2

    else:

        sideLength = x * 2

    #create turtle

    square = turtle.Turtle()

    square.penup()

    #go to center coordinates

    square.goto(center)

    for i in range(2):

        square.forward(sideLength/2)

        square.right(90)

    #draw square

    square.pendown()

    for i in range(4):

        square.forward(sideLength)

        square.right(90)



#start program

wn = turtle.Screen()

# wn.bgcolor("light blue")

centerClick = 0

wn.onscreenclick(get_mouse_click_coor)

turtle.done()

Code Screenshots:

Output


Related Solutions

how can i make this program in python? Captain Øks is on his way to conquer...
how can i make this program in python? Captain Øks is on his way to conquer Mosefjell. He has a map of the area and he is in his camp at the designated place on the map. The map is divided into cells. For each cell, the height is given. Captain Øks is not happy, the landscape is full of hills, and he hates to climb both up and down. He has hired you to write a program that will...
What would the Python code be to create the following working program using turtle: -Make 4...
What would the Python code be to create the following working program using turtle: -Make 4 turtles that are YELLOW, and 4 turtles that are GREEN. Create these turtles using one or more lists. -Make the yellow turtles start at a different randomized location somewhere on the left side of the screen within the range of (-100,100) and (0, -100) -Make the green turtles start at a different randomized location somewhere on the right side of the screen within the...
I want to make a python program that encodes/decodes inputted messages. There are only three options...
I want to make a python program that encodes/decodes inputted messages. There are only three options in the initial menu: 1. encode, 2. decode, 3. quit, and if 1, 2, or 3 are not the input the program says that it is not valid, try again. If encode is chosen, the user inputs their message, the message should only be alphabetical and spaces. Then, the user can pick a number in the range [-27,27] with space being the 27th character...
The coding for this program to run as described on Python: Your (turtle) program must include:...
The coding for this program to run as described on Python: Your (turtle) program must include: include import turtle on a line after the comments so you can use the various Turtle-related objects and methods. Create a turtle named after your favorite ice cream flavor. Make sure the name has no punctuation and consists only of letters, numbers and the underscore character. Write your python program so your turtle is constantly moving and can be turned left and right using...
In Python, I have a turtle that is already coded to move in correlation with the...
In Python, I have a turtle that is already coded to move in correlation with the keys I set (I can control the turtle). I need to put a second turtle in that moves WHILE the first turtle is moving and being controlled. The second turtle needs to do the following while the first turtle is still listening and being controlled: -It needs to begin facing a random direction -It needs to move at a speed of .5 the entire...
Write a program in Python where you can swap only two consecutive elements. You have to...
Write a program in Python where you can swap only two consecutive elements. You have to show all steps to convert a string into another string (both strings will be anagrams of each other). E.g. GUM to MUG GUM GMU MGU MUG
How can I check that if a string only make from specific character? For example, I...
How can I check that if a string only make from specific character? For example, I want to check if a string only make from ICDM characters. It pass the test if it makes from ICMD. It fail the test if it makes from any special character like @#$& or lower case. Example inputs: if string = CIM, it passed if string = AIM, it failed if string = MCDI, it passed if string = ICDF, it failed This can...
Please make a Python program where the computer chooses a number and the player guesses the...
Please make a Python program where the computer chooses a number and the player guesses the number. You need an input, a random number, if statement, and a while loop. Please reference these when doing the code: ((random reference)) •>>> import random •>>> random.randint(1, 100) •67 ((While & if referefence)) •>>> cnum = 3 •>>>while true •>>> if cnum == 3: •>>> print(‘correct’) •>>> break •>>> elif cnum == 2: •>>> print(‘incorrect’) Please post the Python code and the screen...
Please make a Python program where the computer chooses a number and the player guesses the...
Please make a Python program where the computer chooses a number and the player guesses the number. You need an input, a random number, if statement, and a while loop. Please reference these when doing the code: ((random reference)) •>>> import random •>>> random.randint(1, 100) •67 ((While & if referefence)) •>>> cnum = 3 •>>>while true •>>> if cnum == 3: •>>> print(‘correct’) •>>> break •>>> elif cnum == 2: •>>> print(‘incorrect’) Please prepare a Word document that has the...
For python: How do I make an y vs x graph where x is from 1...
For python: How do I make an y vs x graph where x is from 1 to 20 and y is x times 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT