CS 111 - PROGRAM DESIGN I, Spring 2018

Lab 3

The default place for a turtle to "live" is in a world that was created by the makeWorld() function in the JES Library. This is done by the two statements:

     myWorld = makeWorld ()      
     myTurtle = makeTurtle (myWorld) 

However in a world, the background color is white. This can be rather boring. So we may want to access a picture that can be used for the background for the turtle. Once a picture has been created in Python, we can use that picture as the parameter for makeTurtle() function in the JES library. The JES Library has a makeEmptyPicture() function that can create a picture of any size what defaults to white (just like the makeWorld()) or can be set to some other color. This is done by the two statements:

     myPicture = makeEmptyPicture ( 640, 480, blue )      
     myTurtle = makeTurtle (myPicture) 


Accessing Pictures in JES

To have a turtle draw on an image/picture that already exists on your computer, you must first open the image. JES has a function to easily do this for you. This function takes the filename of the image as its parameter.

The easiest way to get the filename of an image is to use the pickAFile function in JES. The pickAFile function will display a dialog box that allows the user to change directories and select an existing file.

The code to prompt the user for a picture file, then open it, and the place a turtle on that picture is:

     filename = pickAFile ();      
     pict = makePicture ( filename ); 

myTurtle = makeTurtle ( pict );

Once the picture has been opened, that picture can then be used by a turtle for drawing. Check out the code in lect0202a.py for a program that does prompts the user for a file, creates the image and displays the picture.

JES Looping Statement: while loops

One way to repeat a section of code for a set number of times is to use a while loop. A while loop requires the use of condition. The code will be repeated while a condition is true. This normally requires the use of a loop counter variable. The loop counter variable is often initialized to 1 and incremented by 1 during the execution of the code in the loop body. The condition than uses a less than or equal relational operation to keep looping while the values is less than or equal to the number of times we want the loop to repeat.

The following code to will print Hello World 8 times. Don't forget the colon at the end of the for loop code and to indent to code that should be repeated.

     counter = 1         # initialize the loop counter to 1

# check that the loop counter is still less than or equal to 8
while ( counter <= 8 ) :
           print "Hello World" 
counter = counter + 1 # increment the loop counter by 1

Lab Assignment

Due: Wednesday, 2/7/2018 by 11:59 pm

For this lab assignment, you are to create a "spirograph" style design in a non-blank image. The lab is to contain the following operations:

  1. The user is to write a function called main() that must allow the user to select the non-blank image/picture as shown above in the code lect0202a.py. The function should create a turtle to draw on the image/picture selected.

  2. The program is to contain a function that draws a 5 pointed star. This function should be called something like "drawStar ( )". This function is to take a turtle as its parameter and will not return anything. Check out the code at lect0131d.py for an example of such functions used to draw using turtles. The shape your function is to draw is a 5 pointed star:
    Note that this shape is not a regular polygon, so you can't just use the same basic ideas we used in class. Fortunately, it is not all that different. For this shape, instead of just turning once around (360 degrees) as is done with a regular polygon, the figure turns around twice (720 degrees) when being created. The function used to draw a single star is to use a while loop statement to repeatly draw the 5 sides of the star.

  3. The "spiralgraph style design" will be done by repeatedly drawing shapes (i.e. calling the draw star function from the main( ) function) where the orientation of the starting point of the shape is modified between each time the shape is drawn. This simply means that the turtle should be at the some location but facing a different direction each time the function is called. In this manner the shape is to be redrawn enough times to sweep out a complete circle.

    For this lab, you are to draw the star shape and repeat it multiple times, with each star rotated slightly from the previous star. The repeated steps are to be done using a while loop statement. When completed, it should look something like:

    The star must be drawn at least 10 times. You can draw it more than 10 times if you wish.

  4. Be sure to include a comment containing the following:
    • your name
    • your NET-ID
    • day and time of your CS 111 lab section (i.e. Monday at 1:00)
    • A description of the project.
Submission of the Lab

Use the Lab 3 submission link in the Assignments Page of Blackboard for CS 111. You will only need to submit the python source code file. You are to name your file with both the lab number and your NetID so we can find it easier. For someone with the NetId of tsmith14 the file should be named:

     tsmith14lab3.py 

Just for Fun

I created the following picture based on the idea from this lab. I have the size and the color of the star change with respect to the loop counter. Also the shape is drawn in two circles instead of just one.

Edit | Attach | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2018-02-02 - 19:41:19 - Main.troy
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF