Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.

Similar presentations


Presentation on theme: "ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse."— Presentation transcript:

1 ENGINEERING 1D04 Tutorial 4

2 What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse clicks Python example Practice with Graphics (and basically everything else learned in this tutorial)

3 Focus Today’s tutorial does not have very much teaching as we are slowing down with the course and going over topics already covered to ensure everyone understands them There is only one more concept that will be covered before the exam (graphics) which is included in the tutorial The final tutorial for the course (the next one) will be exam review I will go over functions as well as an introduction to graphics Instead, I have a longer Python example with graphics code and then a very long practice problem that will encompass everything that you have learnt so far in the course as well as the material I cover in today’s tutorial The practice and the example should cover anything covered in the upcoming minor and major labs

4 FUNCTIONS Easy way to repeat code

5 Functions We use functions to easily repeat code For example, we can create a function to calculate averages for different courses and just keep calling the function giving it a different list of marks as the parameter Eliminates needing to rerun programs [instead we can type the functionName(parameter)] in the shell Eliminates rewriting ode Take the form def functionName(parameter/argument):

6 Scope of Variables Variables that are defined in a function can only be used within that same function This means that even if there are variables containing the exact same name outside of the function (or in a different function) this particular function can’t see, modify nor use them This is called variables being local to a function How do we use these variables in another function? We pass them in as parameters (the words inside of the brackets)

7 Returning values In some cases we do not want to print anything out onto the shell upon execution of a function, instead we wish to return a value that the main function (or another function in the program) can use To do this we use the return statement Every python function returns a value whether we have the return statement written or not, these functions return an object none It is also possible to return more than one object, to do so you simply write return x,y (x and y being the two values) Note: When you return more than one value, you must use multiple assignment to get all of the values at once. For example, if our function will simply return True or False we can save it in one variable Result = ourFunction() If, it returns True or False and a number we need to use two variables ResultA,ResultB = ourFunction()

8 OBJECTS AND GUI A brief introduction to graphics

9 Objects Combine data and operations (ie. they know stuff and can do stuff) They can also interact with each other For example, we can have a car object Each car object then has data such as: Colour Model Make Horsepower Etc.

10 Graphics.py To deal with graphics in Python in this course you will need to download the graphics library This is easy! Just type in graphics python zelle into google, find the first result, download it and save it into your python library directory! Then you can use it just as you would use the other libraries (ie. math library) By typing import graphics OR you can type from graphics import *  this method will allow you to skip writing graphics.function() before every function you use (note function would be any function name found within the graphics library)

11 Import graphics functions/objects win = GraphWin()  creates a graphics window (default size 200 pixels by 200 pixels) P = Point(x,y)  represents a point on the graphics window (0,0) starts at the top left corner, x-values increase R to L y-values increase T to B P.getX()  returns x value P.getY()  returns y value P.draw(win)  draws the point on the window

12 Aliasing When two variables refer to the same object For example, if we P=Point(30,20) Then we say P2=P Both P2 and P will refer to the same point This means that anytime one of the two variables is altered it alters the other one as well How do we avoid this? We have two options: 1. Create another Point object, so say P2=Point(30,20) 2. Use the clone() method, so P2=P.clone()

13 Events Usually, programs with graphics will be interactive This interaction with the user is based on event-driven programming (this means that something will be drawn on the screen and then it will wait for the user to do something) Anytime a user clicks on the mouse, types something, or moves the mouse an event will be generated This event will then be processed by the program and an appropriate action will be taken

14 Getting Mouse Clicks We can use the getMouse() method from the GraphWin class When this method is invoked, the program will pause until a user clicks somewhere and then the point where they click will be saved and returned as a Point

15 PYTHON EXAMPLE Drawing a circle and redrawing it with the center being where the user clicks, as well as having an exit button

16 Practice with Graphics (and basically everything covered in this tutorial) 1. Implement a function distanceBetweenPoints(p1, p2) that takes two Point objects as input and returns the distance between the Point objects as output. 2. Implement a function drawCircle(w,c,r,s) that: 1. Takes an object w of type GraphWin, a Point c, a number r and a string s, as input 2. Creates an object C of type Circle whose center is c and radius is r 3. Fills the interior of C with the colour represented by s 4. Draws the circle C on the window w 5. And returns C as output 3. Implement a function circleGame() that does the following: 1. Creates a pixel window with convenient coordinate system 2. Draws a red-filled circle in the center of the window 3. When the user clicks on the window with the mouse, three possible things happen according to where the mouse click was located: 1. If the mouse click was located inside the circle but less than one quarter of the radius from the center of the circle, the window is closed and the program terminates. 2. If the mouse click was located inside the circle but greater than one quarter of the radius of the circle from the center of the circle, the circle’s radius is reduced by 20%. 3. If the mouse click was located outside the circle, the circle’s radius is increased by 10% 4. The center and color of the circle does not change. 5. Uses the functions distanceBetweenPoints(p1,p2) and drawCircle(w,c,r,s)


Download ppt "ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse."

Similar presentations


Ads by Google