Download presentation
Presentation is loading. Please wait.
Published byBlaze Newman Modified over 9 years ago
1
I210 review (for final exam) Fall 2011, IUB
2
What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided A4 cheat-sheet is allowed; write your name and ID on the cheat-sheet; and turn in your cheat-sheet together with the final exam.
3
Basics Variables Data types –list –conversion Branching structures Loops (while & for loops) Functions –Function header –Return values –Parameters and arguments 3
4
Understanding OOP OOP allows representation of real-life objects as software objects (e.g., a dictionary as an object) Object: A single software unit that combines attributes and methods Attribute: A "characteristic" of an object; like a variable associated with a kind of object Method: A "behavior" of an object; like a function associated with a kind of object Class: Code that defines the attributes and methods of a kind of object (A class is a collection of variables and functions working with these variables) 4
5
Creating Classes for Objects class Puppy(object): def __init__(self, name, color): self.name = name self.color = color def bark(self): print "I am", self.color, self.name puppy1 = Puppy("Max", "brown") puppy1.bark() puppy2 = Puppy("Ruby", "black") puppy2.bark() Class: Code that defines the attributes and methods of a kind of object Instantiate: To create an object; A single object is called an Instance 5
6
Special Methods def __init__(self): print "A new critter has been born!” #Constructor, __init__() #Automatically called by new Puppy object def __str__(self): rep = "Critter object\n" rep += "name: " + self.name + "\n" return rep # __str__() # return a string 6
7
Private vs Public Attributes and Methods Public: Can be directly accessed by client code Private: Cannot be directly accessed (easily) by client code By default, all attributes and methods are public But, can define an attribute or method as private class Critter(object): def __init__(self, name, mood): self.name = name # public self.__mood = mood # private 7
8
Understanding Inheritance Inheritance: An element of OOP that allows a new class to be based on an existing one where the new automatically gets (or inherits) all of the methods and attributes of the existing class The children classes get all the capabilities (methods) and properties (attributes) the parent class has; the children classes are also called derived classes Get the code for free! (code-reuse) – inheritance allows a new class to re-use code which already existed in another class (the parent class) 8
9
Derived Classes are New Classes To create specializations of existing classes or objects by adding new attributes and methods! –often called subtyping when applied to classes. In specialization, the new class or object has data or behavior aspects that are not part of the inherited class. Overriding (e.g., over-ridding of the + operator, so + has different meaning, addition of two numbers, concatenation of two strings, etc) – the same method that does something different Guide to Programming with Python 9
10
Altering the Behavior of Inherited Methods: Overriding Override: To redefine how inherited method of base class works in derived class Two choices when overriding –Completely new functionality –Incorporate functionality of overridden method, add more How to invoke base class’ methods? 10
11
11 class Animal(object): def __init__(self, name): # Constructor self.name = name def get_name(self): return self.name class Cat(Animal): def talk(self): return 'Meow!' class Dog(Animal): def talk(self): return 'Woof! Woof!' animals = [Cat('Missy'), Cat('Mr. Bojangles'), Dog('Lassie')] for animal in animals: print animal.talk() + ' I am ' + animal.get_name() Inheritance Example: Animal Class Base class: A class upon which another is based; it is inherited from by a derived class Derived (child) class: A class that is based upon another class; it inherits from a base class
12
Understanding Event-Driven Programming Event-driven program: A program that responds to actions regardless of the order in which they occur (vs structured programming) Event: Something that happens involving a program's objects (mouse moves over; click of a button, etc) Event handler: Code that runs when a specific event occurs Bind: To associate an event with an event handler Event loop: A loop that checks for events and calls appropriate event handlers when they occur GUI programs traditionally event-driven: the users control the flow of the program 12
13
Buttons on Strike from Tkinter import * root = Tk() # a root window, upon which other GUI elements can be added root.title("Simple GUI Demo") #add title root.geometry("400x200") #change the size of the window app = Frame(root) # create a frame, upon which other Widgets can be added app.grid() #need to invoke grid() method button = Button(app, text = "I am button #1”) button.configure(text = "I am button #2”) button['text'] = "I am button #3” button["command"] = update_count button.grid() root.mainloop() #must start up the window's event loop!!! 13 (I) (II) (III) (IV)
14
Writing a Game Using Pygame & Livewires #(1) Import pygame or livewires from livewires import games #(2) Create a graphical window games.init(screen_width = 640, screen_height = 480, fps = 50) #(3) Start up window’s main loop games.screen.mainloop() 14
15
Understanding the Graphics Coordinate System Graphics screen made up of rows and columns of pixels Specify point on screen with coordinates: x and y ; Upper-leftmost pixel is (0,0) Can place graphics objects on screen using coordinate system 15 screen.width screen.height
16
Control a Sprite Sprite: A graphics object with an image class Pizza(games.Sprite): """ A bouncing pizza. """ def update(self): if games.keyboard.is_pressed(games.K_UP): self.x -= 1 if self.right > games.screen.width or self.left < 0: self.dx = -self.dx if self.bottom > games.screen.height or self.top < 0: self.dy = -self.dy 16
17
Sample Question 1 In Pygame, what is a sprite? What is a sprite collision? 17
18
Sample Question 2 Complete the following GUI code that creates a button… from ____________ import * class Application(Frame): def __init__(self, master): Frame.__init__(self, master) self.grid() ___________________________ self.create_widget() def create_widget(self): self.lbl = Label(self, text="Click to increment!") ________________________ self.bttn = Button(self)...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.