PYTHON 3.6.5 QUESTION Nature Art,tuetle questions 1. We must use Python 3 (the P
ID: 3912284 • Letter: P
Question
PYTHON 3.6.5 QUESTION Nature Art,tuetle questions
1. We must use Python 3 (the Python IDLE version used in our CSIL lab) to write our program
2. Our program is to execute as soon as one has pressed the F5 key or has selected the option Run -> Run Module from the Python IDLE menu. No user input is required in this assignment. This is to say that the user must not be prompted for any input data once our nature scene Python program has started executing. It executes all on its own.
3. We cannot make use of Python modules that must first be downloaded on a computer before they can be used. We can only use Python modules that are already available on the computer (available by simply using the Python statement import in our Python program).
4. Our drawing must be composed of at least 30 shapes (Each shape must be labelled in the code using comments: e.g. # Shape 31. A red football in the air.). we can count 9 shapes: · 2 rectangles, · 1 square, · 3 circles, · 2 semi circles (note that 2 semi circles drawn together count as 1 shape, but here, the 2 semi circles are not drawn together so they count as 2 shapes) and · 1 quarter of a circle.
5.Our drawing must include at least 10 colours. ,
Some of the shapes our program draws must be filled in.
6.Our program must include at least 6 for loops.
7.Our program must include at least 8 functions:
4 of them must be void functions: functions that do not return any “value” (possibly doing drawings and/or pen movements, or creating more complex shapes), · Note that these 4 functions may or may not require parameters. · 6 of them must require parameters (the number of parameters is up to us). · Note that these 6 functions may or may not be void.
we can count 3 void functions that require parameters and 3 incomplete functions. Of course, we cannot submit a program with incomplete functions.
8. We have applied the generalization guideline when we created all our functions. This is to say that our functions are as general as they can be. They are designed in such a way (with parameters and/or returned value) that they can solve several similar problems.
9.None of our code is repeated. This is to say that we encapsulated repeated code into functions and have called these functions whenever needed.
10.All our functions terminate with a return statement.
Explanation / Answer
Allow the user to create their own scene with houses, grass, sun, turtles, etc. This example illustrates how to listen to user input using the Screen's onclick and onkey commands.
import turtle
import math
import random
screen = turtle.Screen()
screen.bgcolor("lightblue")
fred = turtle.Turtle()
fred.shape("turtle")
fred.color("black")
fred.fillcolor("yellow")
fred.speed(0)
# Draws a rectangle
def drawRectangle(t, width, height):
t.begin_fill()
for i in range(2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.end_fill()
# Draws an equalateral triangle
def drawTriangle(t, length):
t.begin_fill()
t.forward(length)
t.left(135)
t.forward(length / math.sqrt(2))
t.left(90)
t.forward(length / math.sqrt(2))
t.left(135)
t.end_fill()
# Draws four rays of a sun
def drawFourRays(t, length, radius):
for i in range(4):
t.penup()
t.forward(radius)
t.pendown()
t.forward(length)
t.penup()
t.backward(length + radius)
t.left(90)
# Goes to the given x, y coordinates. This function is
# called on user's click.
def gotoCoords(x, y):
fred.penup()
fred.goto(x, y)
fred.pendown()
# Draws a house. Called when the user presses the h key.
def drawHouse():
fred.setheading(0)
drawRectangle(fred, 60, 60)
fred.left(90)
fred.forward(60)
fred.right(90)
drawTriangle(fred, 60)
fred.right(90)
fred.forward(60)
fred.left(90)
fred.forward(20)
drawRectangle(fred, 20, 30)
# Draws the sun. Called when the user presses the s key.
def drawSun():
fred.setheading(0)
fred.begin_fill()
fred.circle(24)
fred.end_fill()
fred.penup()
fred.left(90)
fred.forward(24)
drawFourRays(fred, 25, 24)
fred.right(45)
drawFourRays(fred, 15, 24)
fred.right(45)
# Draws a clump of grass. Called when the user presses
# the g key.
def drawGrass():
fred.setheading(0)
fred.begin_fill()
fred.left(120)
d = random.randint(20, 30)
fred.forward(d)
fred.right(170)
fred.forward(d)
for i in range(3):
fred.left(150)
d = random.randint(20, 30)
fred.forward(d)
fred.right(170)
fred.forward(d)
fred.end_fill()
# Turns left by 1 degree. Called on left arrow press.
def turnLeft():
fred.left(1)
# Turns right by 1 degree. Called on right arrow press.
def turnRight():
fred.right(1)
# The following several functions can be called to
# change the current color.
def colorBlue():
fred.fillcolor("blue")
def colorRed():
fred.fillcolor("red")
def colorGreen():
fred.fillcolor("green")
def colorLightGreen():
fred.fillcolor("lightgreen")
def colorPink():
fred.fillcolor("pink")
def colorYellow():
fred.fillcolor("yellow")
def colorOrange():
fred.fillcolor("orange")
# Tell the screen to listen to user clicks and key
# presses, and call the appropriate functions, which
# are defined above.
screen.listen()
screen.onclick(gotoCoords)
screen.onkey(drawHouse, "h")
screen.onkey(drawSun, "s")
screen.onkey(drawGrass, "g")
screen.onkey(turnLeft, "left")
screen.onkey(turnRight, "right")
screen.onkey(fred.stamp, "t") # leave a turtle stamp
screen.onkey(colorBlue, "b")
screen.onkey(colorRed, "r")
screen.onkey(colorGreen, "n")
screen.onkey(colorLightGreen, "l")
screen.onkey(colorPink, "p")
screen.onkey(colorYellow, "y")
screen.onkey(colorOrange, "o")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.