Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats,...

Preview:

Citation preview

Life after CS106AP!

Day 1!

Life after CS106AP!

Day 1!

Life after CS106AP!

Day 1!

while True:

if stop_condition:

break

# ‘Animate’ object

pause(timestep)

while True:

if stop_condition:

break

# ‘Animate’ object

pause(timestep) Small movements help make the animation appear continuous!

while True:

if stop_condition:

break

# ‘Animate’ object

pause(timestep)

How long to wait in milliseconds

Definition

Definition

ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Definition

Classes allow us to define our own data types!

●○

Definition

●○

image = SimpleImage(width, height)

Creates an instance of the SimpleImage class (i.e. an object of the type SimpleImage)

●○

●○○○

●○

●○ oval.fill_color oval.width○○ Variables stored inside

the class

●○

●○ oval.fill_color oval.width○ oval.move()○

Functions you can call on the object

●○

●○ oval.fill_color oval.width○ oval.move()○ GOval(width, height)

How you create the object

Pynstagram.py

PynstaUser

PynstaUser

●○○○

●○○

PynstaUser

●○○○

●○○

● PynstaUser(name)

class PynstaUser:

def __init__(self, username): self.name = username self.friends = [] self.posts = []

def add_friend(self, user): …

def post(self, message): ...

Methods

Attributes (must start ith self. to be attributes!)

Class definition and name

Constructor

●○○

●○

●○○

●○

Today!

Encapsulation!

Definition

Definition

a class!

●●●●

vs.

●○○

●○

●○○

●○○

Getters and setters!

●○○

○more on this tomorro

Bubbles.py

Life after CS106AP!

Day 1!

Event-driven programming!

def main():

...

...

def your_mouse_listener():

...

def main():

...

...

def your_mouse_listener():

...

Definition

def main():

...

...

def your_mouse_listener():

...

Definition

clicking, moving, dragging

def main():

...

...

def your_mouse_listener():

...

def main():

...

...

def your_mouse_listener():

...

def main():

...

...

def your_mouse_listener():

...

MOUSE CLICK

def main():

...

...

def your_mouse_listener():

...

MOUSE CLICK

def main():

...

...

def your_mouse_listener():

...

MOUSE CLICK

The function happens immediately, no matter where you are in your program!

def mouse_listener_handler(event):

...

def mouse_listener_handler(event):

...

It must take in an event for campy to recognize it as a valid mouse listener.

def mouse_listener_handler(event):

...

event gives us access to information about the mouse event (e.g. x, y coordinates of the click).

def mouse_listener_handler(event):

...

onmouseevent()

onmouseclicked(mouse_listener_handler)

def mouse_listener_handler(event):

...

onmouseevent()

onmouseclicked(mouse_listener_handler)

Pass in your mouse listener function as the argument

def mouse_listener_handler(event):

...

onmouseevent()

onmouseclicked(mouse_listener_handler)

Don’t include parentheses after the function name!

Bubbles.py

Recommended