MONT 105S, Spring 2009
Home | | Course Schedule | | Assignments | | Lecture NotesThe following topics have been covered since the midterm. Each of the following topics may appear on the exam.
1. Functions General form of a function definition Writing function definitions Calling functions Function execution Using paramters Scope: Local vs. global variables Return values for functions Mutable and immutable parameter types 2. Classes and Objects Class properties Class methods Writing a class definition Creating an object of a particular class type Accessing properties and methods of an object (the dot notation) Using self in a class method The docstring: Creating and accessing the docstring The constructor function (__init__( ) ) Writing a constructor function Purpose of the constructor function The __str__ function 3. Graphical User interfaces (GUIs) Elements (widgets) in a GUI Frame, Label, Button, Entry, Text, Checkbutton, Radiobutton Event driven programming--event loop and event handlers Creating a root window Adding GUI elements to root window Creating an event handler function Getting input from an entry field Writing to an entry field or text box Using a Checkbutton Setting up a boolean variable for the value Writing a function to respond to checked boxes Using a Radiobutton Setting up a string variable for the value Writing a function to respond to the selected radio button Using the grid( ) function to position elements Setting row, column, columnspan, padx, pady and sticky 4. Game Graphics Creating a game screen with a background image Loading an image Binding the image to the screen background Adding a sprite to the game screen Sprites: Properties: x, y, dx, dy, etc. Methods: get_x( ), set_x(new_x), update( ), etc. Creating a subclass from Sprites (e.g. Pizza class) Overriding the update( ) function The mouse class: Properties: x, y, is_visible Methods: get_x( ), set_x(new_x), etc. Creating a mouse-controlled sprite. Setting the is_visible property. Using games.screen.event_grab Detecting Collisions Adding Text to the screen Avoiding problems with collisions Updating the text value Adding a message to the screen Creating an animation 5. Class inheritance Parent (Super) class and Child (sub) class Creating a class that inherits from another class
Practice Problems
The following problems are intended to help you study for the exam.
1) Given the function definition: def Mystery( someVal): answer = 0 if someVal > 2.0: answer = 3.0 * someVal else: answer = 0.0 return answerWhat is the value is assigned to x after execution of the following statement?
x = Mystery(2.5)2) Consider the following program:
def Demo( theList, answer): theList[0] = theList[0]*2 answer = theList[0] + 3.5 myList = [20] myResult = 4.8 Demo(myList, myResult) print myList print myResultWhat is printed out?
3) a)Write a function,
def TenToThePower( n ):that returns 10 raised to the integer power specified by n.
b) Write a Python statement to compute 10 to the power of 3 using the above function and store the answer in the variable, tenCubed.
4) a) Write a class, named Account, to keep track of a user's bank account. The class should have four properties: name, totalDeposits, totalWithdrawals and totalFunds. The class should have three methods:
b) Write a main program that makes use of the above class. It should do the following:
c) What is the constructor function and what is its purpose? When is it called?
5) a) Write the code to create the GUI shown below.
The relative positioning of the elements should be as shown. When the user types their name in the entry field and clicks on the "Say Hello" button, the appropriate message should be typed in the text box as shown. For example, if the user enters "Mary", the message should read "Hello Mary!"
The following are the dimensions of the GUI and elements:
from Tkinter import * #Write your event handler for the button here #Write the main program here.b) What is meant by "Event driven programming?" What is the purpose of the mainloop in the above program?
6. a) Write a class, called FallingRock, that inherits from the Sprite class in the livewires games module. The class should have an update function that checks if the object hits the bottom of the screen. If it does, it should reset the position so the rock is at the top of the screen.
b) Write code for the main program that would create an object of type FallingRock. The code should first load in an image named "rock.bmp". It should then create the FallingRock object that has "rock.bmp" as its image. Its initial position should be at the top of the screen and in the middle horizontally. It should be moving directly downward. The code should add this object to the gamescreen.
c) Explain what is meant by class inheritance. Use the example from parts a and b to illustrate your answer.