Do All Steps in Order

Total Page:16

File Type:pdf, Size:1020Kb

Do All Steps in Order

Exercise on Programming Turtle Graphics in Python

Purpose: The purpose of this exercise is to get a little introduction to the process of computer programming in a high- level language - the process that software professionals use to write a program. In particular, you will be exposed to the syntax of the Python programming language and use a set of tools called Turtle Graphics.

DO ALL STEPS IN ORDER

Step 0) a) I suggest you create a separate folder to download the files into. b) Download the 4 .py files from the course calendar. (For each, right-click and choose "Save Link As…" or “Download Linked Page …”) Store each of these files in the folder you created in part a. c) Open the IDLE software.  MAC: Go to the Applications Folder. There should be a Python folder. Look inside this folder for IDLE  Windows: Do a search on IDLE.

You should see something like this (although the exact numbers will be different).

This window is called the Shell or the Interpreter.

Step 1) Within IDLE, open the file "LabProg1.py" via the "File" menu, and the "Open" command.

You should see something like this image. This window is the “program” window or the “code” window.

Now, run the program. HOW? Under the "Run" menu, choose "Run Module". The program draws a box. Try to understand why the program does what it did. If you do not understand, ask questions.

Question 1-1: How many pixels wide is the box? How many pixels high is the box?

Change the program to have the turtle draw a box that is much wider (at least 300 pixels wide), but the same height. Run the program again.

Question 1-2: Describe the changes you made to achieve this.

You may now close LabProg1.py.

*************************************************************************** Step 2) Open the file "LabProg2.py" via the "File" menu, and the "Open" command.

Run this program. See that it draws an old style camera that is black, with a white lens and a red “viewport”.

Notice three lines of code that say turtle.color From these lines, we see that Python knows the colors "Black", "white" and "red" by name. Make changes to the program and run it to determine the answer to the following:

Question 2-1: Which of the following colors does Python know about by name:

Yellow Pink Cyan Magenta LightBlue Grey Rose DarkPink Chartreuse

------

Now, change the turtle.color commands to use RGB. Create a blue "camera", a black "lens", and a white "viewport".

Question 2-2: Using RGB, what command did you give to make each of the following colors: a) blue b) black c) white

------

Create a purple "camera", a yellow "lens", and a pink "viewport".

Question 2-3: Using RGB, what command did you give to make each of the following colors: a) purple b) yellow c) pink

------

Near the top of the program, we see lines for "variables" named x, y, width, and height. These allow the code below to be more flexible. Change the x = -200 line to say x = -300

Question 2-4: Run the program now. What happens?

Spend some time and play with the values of x, y, width, and height. See what the program does differently for each one.

Set values for x, y, width, and height to have the camera appear at the lower right corner of the window, with “reasonable” dimensions and scale.

Question 2-5: What values did you set those variables to?

You may now close LabProg2py.

*************************************************************************** Step 3) Open the file "LabProg3.py" via the "File" menu, and the "Open" command.

Run the program. Discover that it does NOTHING.

This may seem surprising, given that there is a lot of code in the window. This is because almost all of the code is in functions, which are sets of code that CAN do things, but do not unless they are called.

At the BOTTOM of the file, type drawGround() drawSky()

NOTE: do NOT indent those two lines. They should appear at the left hand side of the editor window.

Run the program and watch what happens. (THIS MAY TAKE A MINUTE, so be patient.) ------Now, change the code to say: drawSky() drawGround()

Question 3-1: What happens differently when you run this changed program?

------

At the BOTTOM of the file, add the code: drawFlower(0,50,20,"red")

Run the program and observe what happens.

a) Change the arguments to be 0, 50, 20, and “yellow”  Run the program and observe what changes. b) Change the arguments to be 200, 50, 20, and “yellow”  Run the program and observe what changes. c) Change the arguments to be 200, 100, 20, and “yellow”  Run the program and observe what changes. d) Change the arguments to be 200, 100, 40, and “yellow”  Run the program and observe what changes.

Now, modify the program so that it has four flowers.  Flower1: right hand side of the screen, 70 pixels high, red flower of size 40  Flower2: left hand side of the screen, 140 pixels high, yellow flower of size 70 (Use RGB for this one).  Flower3: center of the screen, 80 pixels high, purple flower of size 30  Flower4: between flower1 and flower3, taller than the other flowers, white blossom smaller than the other flowers

Question 3-2: What line of code did you use for each of the flowers?

You may now close LabProg3py.

*************************************************************************** Step 4) Open the file "LabProg4.py" via the "File" menu, and the "Open" command.

Run this program. See that it creates a spiral. At the bottom of the file is the “function call”, with four arguments. Change one of the arguments and run the program again. See what changes. Change another argument and run the program again. See what changes. Etc. In other words, futz with the arguments till you understand what each of them does.

Now, add a SECOND call to the spiral function (with different parameters). Observe how the spirals interact.

TASK: Create a pretty picture using the spirals. Your picture must include at least two spirals, but may include more.

Question 4-1: What code did you use to create the picture?

Recommended publications