CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation Python – Introducing Turtle
This week, we will have … Weekly Exercise 6 Lab – Functions and Turtle Posted beside our Weekly Exercise 6 To be done during Wednesday’s open lab in CSIL or on your own time Not submitted, not marked – but an opportunity for you to practice developing software using Python Solutions will be posted on Friday – June 14 Assignment 1 Will be posted on our course web site this week
Last week, we looked at … What are these comments? Why are we returning plain? Should we not be returning cipher instead? Why are we returning cipher? Should we not be returning plain instead? https://repl.it/repls/TimelyJovialAssociate
Software Development Process Incremental Software development Part of the software development process is the idea of developing a program (software) incrementally – one piece at a time Once this piece has been tested and shown to work, we move on to the next piece Example of incremental software development using our Encryption/Decryption program (with functions) Piece 1 -> Main part of program + functions encrypt() and decrypt() without body -> called STUB Piece 2 -> Implement encrypt() function -> change return value from plain to cipher Piece 3 -> Implement decrypt() function -> change return value from cipher to plain
Solution: Different kinds of functions Activity: Fill this table using functions we have used so far! Function takes parameters Function does not take parameters Function returns a result (i.e., a returned value) upper( … ) <- does not take parameters strip( … ) len( … ) input( … ) upper( ) strip( ) input( ) isalpha( ) Function does not return a result (i.e., a returned value) print( … ) print( )
Are there “functions” in the real world? Mom says to Louise “Please, can you go make your bed?“ Specific task: Make bed Parameter(s): None required Returned value: None returned Mom says to Louise “Here’s $5, please, can you go buy a bag of apples?“ Specific task: Parameter(s): Returned value:
Are there “functions” in the real world? Mom says to Louise “Please, can you find my cell phone?“ Specific task: Parameter(s): Returned value: Mom says to Louise “Please, can you put these bags on the kitchen counter?“ Specific task: Put bags on kitchen counter
While loop using a sentinel – From our Weekly Exercise 5 https://repl.it/repls/IdenticalInterestingScans
Unit 3 - Graphics and Animation Pixar movies and your favourite 3D animated films these days are built with code Indeed, animated movies and video games today rely heavily on 2D and 3D graphics and animation technology In this unit, we’ll make basic animations using code, and learn how to build complex and beautiful graphics
Unit 3 - Graphics and Animation APPLICATIONS In this unit, we’ll learn about the computing science field of graphics and animation ALGORITHMS One of the algorithms we shall learn is called recursion Python We’ll learn about Turtle graphics, functions and loops
2. Choose this one, not regular Python Python (with Turtle) 1. Click on … 2. Choose this one, not regular Python
Let’s try it! Problem Statement: Write a program that draws a blue square with our Turtle We may need to know the Turtle coordinate system: ? (100,100) (0,0) ? (-100, -100)
How about this! Problem statement: Write a program that draws a chocolate chip cookie with our Turtle
But what if I want many cookies? Solution 1 Could I use a for loop?
But what if I want many cookies? Solution 2: Could I copy and modify the code many times, each instance drawing one cookie? Hum… This solution would lead to a lot of repeating code, which is not a good idea! See GPS! Why is that?
But what if I want many cookies? Solution 3: Best solution would be to use a __________________
But what if I want many cookies? But our function draws many cookies in the same spot! We have not solve our problem! Solution 4: So, the ultimate solution would be to design our function such that it takes __________________ !
Review - For next lecture How do we create a Turtle object, given the turtle module? What is the keyword necessary to make a while loop? What is the keyword necessary to create a function? What is the difference between function parameters and function arguments? What are the commands necessary to create an animation with Turtle? Why do we place our functions at the top of our program?