Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

nature art python Nature Art In this Assignment 3, we are to write a Python prog

ID: 3911904 • Letter: N

Question

nature art python Nature Art In this Assignment 3, we are to write a Python program, called NatureArt.py, which is to create a drawing representing an outside nature scene using the Python Turtle Graphics module and its built- in functions What do we mean by "nature scene"? We mean that our Python program can be a drawing of anything as long as all the elements can be found outside, in nature (e.g. humans, buildings, parks, plants, trees, sun, animals, grass, mountains, etc). The drawings cannot be indoor scenes. This is to say that our nature scene cannot be a bunch of shapes drawn together with no theme like the drawing created by the program Turtle Lab_5.py seen in our Week 8 Lab. Our nature scene (program) is to also satisfy the following requirements. Requirements: 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.

Explanation / Answer

The program 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")