Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders Assignment 2 due this Friday.

Similar presentations


Presentation on theme: "CISC101 Reminders Assignment 2 due this Friday."— Presentation transcript:

1 CISC101 Reminders Assignment 2 due this Friday.
Winter 2019 CISC101 4/14/2019 CISC101 Reminders Assignment 2 due this Friday. Quiz 2 this week, as well. See updated Quiz topics listed in Friday’s lecture. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Introduce the use of the turtle module.
Winter 2019 CISC101 4/14/2019 Today Introduce the use of the turtle module. Use the turtle to illustrate more examples of iteration. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

3 Turtle Graphics The “Turtle” is an easy way of drawing to the screen.
CISC101 Turtle Graphics The “Turtle” is an easy way of drawing to the screen. One use is to illustrate how loops work. See the first section in “Program Frameworks” in the Python Standard Library. To start you’ll need to import the turtle module: from turtle import * Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

4 Aside – Importing Modules
Normally, you would use a simpler import statement, like: import random To use a function from this module, you would write, for example: dice1 = random.randint(1, 6) So, you need to keep naming the module to obtain its members. Winter 2019 CISC101 - Prof. McLeod

5 Aside – Importing Modules, Cont.
A more involve import is, for example: from math import * Now, instead of having to say math.sqrt(), you can just say sqrt(). You don’t have to keep using the module name. Since we will be using so many turtle functions, it is easier to import as in: from turtle import * Winter 2019 CISC101 - Prof. McLeod

6 Aside – Importing Modules, Cont.
While this usage looks like it would be quite convenient, you need to be cautious and not use it with more than one or two imports at a time. What would happen if two imports contain one function or more with the same name? Which one would you get? Winter 2019 CISC101 - Prof. McLeod

7 A Few turtle Commands To make the turtle look like a “turtle”:
shape(“turtle”) To set the line thickness and line colour: pensize(5) pencolor(“green”) The turtle starts out at the “home” position, (0, 0), facing right or “east”. To draw, just issue commands to move the turtle. Winter 2019 CISC101 - Prof. McLeod

8 A Few More turtle Commands
Turning: right(num_degrees) # clockwise left(num_degrees) # counter-clockwise Moving in the direction he is pointing; forward(length) Changing speed: speed(5) # 0 is no animation, 1 slow 10 fastest Winter 2019 CISC101 - Prof. McLeod

9 turtle Demos Start with five demo programs that illustrate turtle commands. These five do not have any iteration. Start with TurtleDemo1.py Winter 2019 CISC101 - Prof. McLeod

10 Filling Commands Use begin_fill(), then draw, followed by end_fill()
to fill a closed, drawn area. See TurtleDemo2.py See TurtleDemo3.py to see how to draw a filled circle. Winter 2019 CISC101 - Prof. McLeod

11 Regular Polygons Modify how circle() is invoked to draw regular polygons instead of circles. See TurtleDemo4.py Draw text, too! See TurtleDemo5.py Fun! Winter 2019 CISC101 - Prof. McLeod

12 Turtle Loop Demos The turtle can use loops to draw many similar figures one after the other without a lot of extra code. These demos will show loops, nested loops and the use of conditionals inside loops. Winter 2019 CISC101 - Prof. McLeod

13 while Loop Demo Draw 10 squares with turtle, one inside the other.
CISC101 while Loop Demo Draw 10 squares with turtle, one inside the other. See TurtleLoopDemo1.py In essence: size = 40 while size <= 400 : # drawing stuff size = size + 40 Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

14 Nesting Loops, Example A loop can be useful in preventing repetitious coding practice. Can you see any part of the previous example that is repetitive? How would you fix it? See TurtleLoopDemo2.py Every time the outer loop iterates once, the inner loop iterates 3 times. Also note the use of the speed(0) setting… Winter 2019 CISC101 - Prof. McLeod

15 Nesting Loops, Example Cont.
A more compact version that takes advantage of two more turtle functions: TurtleLoopDemo2Alt.py Uses goto(x, y) to move to a corner of the square. (Think of the transporter in Star Trek…) Uses setheading(angle) to make the turtle point South. Inner loop iterates four times now. while loop is changed to a for loop. Winter 2019 CISC101 - Prof. McLeod

16 Turtle Squares Again Alternate red and blue squares.
See TurtleLoopDemo3.py Uses for loops. Shows an if statement inside the loop. Every time the outer loop iterates once, the if statement executes once. How can you change the code to have just the top side of the red square drawn in green? Draw the bottom side of the blue square in green? Winter 2019 CISC101 - Prof. McLeod

17 Random Walk See “Random Walk” in Wikipedia.
Think of a drunk turtle staggering through a regular grid of city streets. Used to model any sort of random population movement. Brownian Motion, for example. See RandomWalk.py Advanced features include the use of a tuple to store possible direction choices. Winter 2019 CISC101 - Prof. McLeod

18 Spiral of Theodorus Supposedly invented by the ancient Greek philosopher Theodorus as a geometric means of estimating square roots. It consists of a set of contiguous right angle triangles, drawn as shown: Winter 2019 CISC101 - Prof. McLeod

19 Spiral of Theodorus, Cont.
See FilledTheodorusSpiral.py Draws the spiral and fills the triangles with a gradation of colour. Uses a tkinter dialog to allow the user to choose colours for the start and end of the gradation. More tkinter towards the end of this course! Winter 2019 CISC101 - Prof. McLeod


Download ppt "CISC101 Reminders Assignment 2 due this Friday."

Similar presentations


Ads by Google