Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to use turtle graphics to create an interactive forest drawing application. At the program start, you should disable all turtle animations and change the

How to use turtle graphics to create an interactive forest drawing application.

At the program start, you should disable all turtle animations and change the turtle's icon to a bird shape (see sample code). Then turtle should be hidden from view. Next your program should implement the following requirements.

  • Create a large rectangle to represent drawing boundary.
  • On top of the rectangle, draw two small circles and write text to let the user choose between a bird and tree drawing. When the program starts, tree must be chosen by default.
  • Handle mouse clicks in the turtle window. Clicks within the drawing rectangle will draw a tree or bird depending on user's selection. Mouse clicks outside the drawing rectangle are ignored, except for those within the selection circles which switch the drawing mode from bird to tree.
  • Drawing a tree consists of drawing one vertical rectangle (stem) and three overlapped triangles (branches and leaves).
  • The bird on the other hand should be drawn using turtle stamping at the click point.
  • Tree size should change randomly. First you will choose a reference size for tree components (stem rectangle and leave triangles). Then at the drawing time, generate a random value between 0.7 and 1.3 which will act as a scale factor so that individual trees can be shorter, equal to or bigger than the reference size.
  • When the bird mode is selected, the (bird shaped) turtle becomes visible in the top left corner. When tree mode is selected, turtle hides itself again.
  • While the bird mode is selected, your program should respond to left (?) and right (?) keyboard buttons. User will press these buttons in order to change the turtle's tilt angle. By changing the title angle, birds can be drawn in many different orientations.

Suggested dimensions and locations of all elements

Window size - 800 800

Inner rectangle - 700 700

Tree/bird selector circles - radius 10, centre points (0, 370) and (100, 370)

Bird location in top left - (-340, 370)

Tree (unscaled) size - stem 15 80, leaves 100 50, 40% overlap of leaf triangles

Sample Codes

All the following turtle functions can be called after importing theturtlemodule.

(1) To change window size

turtle.setup(window_width, window_height)

(2) To handle mouse clicks

# first create a click handler function which receives (x,y) location of click point# Python call this function automatically when a left click is detected anywhere in the windowdef handle_click(x, y): print('detected a click at', x, y)# in main function, declare hande_click as the listener functionturtle.listen()turtle.onscreenclick(handle_click) # pass the function name as parameter (no function call)

(3) To handle keyboard buttons

# create a listener function which is called automatically on button pressdef left_keypress(): print('left key detected')# in main function, declare left_keypress as the listener functionturtle.listen()turtle.onkey(left_keypress, 'Left')

(4) To change turtle icon to a bird

turtle.register_shape('bird', ((-22,-39),(-20,-7),(-7,3),(-11,7),(-12,9),(-11,10),(-9,10),(-3,7), (10,24),(30,16),(13,18),(4,0),(14,-6),(6,-13),(0,-4),(-14,-13),(-22,-39)))turtle.shape('bird')

(5) To change turtle tilt angle (it is different from turtle heading)

turtle.tiltangle(new_tilt_value)

below picture shows further clarification of all these requirements:

image text in transcribed
Tree Bird . Tree O Bird . Tree O Bird At the start, turtle is hidden. Boundary rectangle and selection Animations are disabled. options are created immediately. Tree is selected by default. Python Turtle Graphics . Tree O Bird 1 Python Turtle Gro O Tree Bird Tree . Bird Clicking outside boundary has no effect. Clicking within the selection circles In bird mode, the turtle (bird shaped) switches between the tree/bird mode. becomes visible in top left. 4 / Python Turtle Graphics . Tree O Bird Python Turtle Graphics Tree O Bird Tree O Bird Turtle hides again when in tree mode. Clicking inside the rectangle Note the click point is top of tree stem creates a drawing. and base of tree leaves . Tree O Bird / Python Turtle Graphics O Tree .Bird Tree Bird X Each tree has different size, scaled up or down from the reference size. In bird mode, clicking within the rectangle stamps the turtle. Turtle returns to top left after each stamping. Python Turtle Graph O Tree Bird Tree .Bird X X Pressing LEFT key titls the turtle counter clockwise. Pressing right key titls it clockwise

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Measuring and managing return on marketing investment

Answered: 1 week ago