Slide 1

Slide 1 text

PLATINUM)SPONSORS) GOLD)SPONSORS) SILVER)SPONSORS) Saturday, May 4, 13

Slide 2

Slide 2 text

Juan Gomez Co-Founder of PyhtonKC Twitter: @_juandg Teach your kids how to program with Python and the Raspberry Pi Saturday, May 4, 13

Slide 3

Slide 3 text

May the 4th be with you! Saturday, May 4, 13

Slide 4

Slide 4 text

So what is this talk really about? Saturday, May 4, 13

Slide 5

Slide 5 text

Brief History of the RaspberryPi More at: https://www.youtube.com/watch?v=z-j4USHTLWM Saturday, May 4, 13

Slide 6

Slide 6 text

Saturday, May 4, 13

Slide 7

Slide 7 text

What you’ll need: • MicroUSB Power Supply • SD Card (>= 4GB) • USB Keyboard • USB Mouse • Ethernet Cable • HDMI Cable • Monitor • Case? • HDMI to VGA Adapter? • USB WiFi Adapter? • USB to TTL Cable? • RCA Cable? Saturday, May 4, 13

Slide 8

Slide 8 text

How do you teach kids? Saturday, May 4, 13

Slide 9

Slide 9 text

Make it fun! http://www.adafruit.com/products/975 Saturday, May 4, 13

Slide 10

Slide 10 text

Some tips • Seriously, Make it fun! • Start with Math • Be Patient • Explain the Basics • Use real life examples • Kids are slow typists! Saturday, May 4, 13

Slide 11

Slide 11 text

How do I set it up? Saturday, May 4, 13

Slide 12

Slide 12 text

Saturday, May 4, 13

Slide 13

Slide 13 text

Disclaimer • This an intro to Python for YOU, not your kids. • The kids version is at: https://github.com/ mechanicalgirl/young-coders-tutorial Saturday, May 4, 13

Slide 14

Slide 14 text

A Python Code Sample x = 34 - 23 # A comment. y = “Hello” # Another one. z = 3.45 if z == 3.45 or y == “Hello”: x = x + 1 y = y + “ World” # String concat. print x print y Saturday, May 4, 13

Slide 15

Slide 15 text

Enough to Understand the Code • First assignment to a variable creates it • Assignment is = and comparison is == • For numbers + - * / % are as expected • Special use: • + for string concatenation • % for string formatting (as in C’s printf) • Logical operators are words (and, or, not) not symbols (&&, ||, !). • The basic printing command is print Saturday, May 4, 13

Slide 16

Slide 16 text

Comments •Start comments with #, rest of line is ignored •Can include a “documentation string” as the first line of a new function or class you define •Development environments, debugger, and other tools use it: it’s good style to include one def my_function(x, y): “““This is the docstring. This function does blah blah blah.””” # The code would go here... Saturday, May 4, 13

Slide 17

Slide 17 text

Python and Types •Everything is an object! •“Dynamic Typing”-> Data types determined automatically. •“Strong Typing” -> Enforces them after it figures them out. x = “the answer is ” # Decides x is string. y = 23 # Decides y is integer. print x + y # Python will complain about this. Saturday, May 4, 13

Slide 18

Slide 18 text

Basic Datatypes • Integers (default for numbers) •z = 5 / 2 # Answer 2, integer division • Floats •x = 3.456 • Strings • Can use “” or ‘’ to specify with “abc” == ‘abc’ • Unmatched can occur within the string: “matt’s” • Use triple double-quotes for multi-line strings or strings that contain both ‘ and “ inside of them: “““a‘b“c””” Saturday, May 4, 13

Slide 19

Slide 19 text

Whitespace Whitespace is meaningful in Python: especially indentation and placement of newlines •Use a newline to end a line of code Use \ when must go to next line prematurely •No braces {} to mark blocks of code, use consistent indentation instead • First line with less indentation is outside of the block • First line with more indentation starts a nested block •Colons start of a new block in many constructs, e.g. function definitions, then clauses Saturday, May 4, 13

Slide 20

Slide 20 text

Assignment •You can assign to multiple names at the same time >>> x, y = 2, 3 >>> x 2 >>> y 3 This makes it easy to swap values >>> x, y = y, x •Assignments can be chained >>> a = b = x = 2 Saturday, May 4, 13

Slide 21

Slide 21 text

A Python Code Sample x = 34 - 23 # A comment. y = “Hello” # Another one. z = 3.45 if z == 3.45 or y == “Hello”: x = x + 1 y = y + “ World” # String concat. print x print y Saturday, May 4, 13

Slide 22

Slide 22 text

Side by Side with Java Java (C#) Python public class Employee { private String myEmployeeName; private int myTaxDeductions = 1; private String myMaritalStatus = "single"; public Employee(String EmployeName) { this(EmployeName, 1); } public Employee(String EmployeName, int taxDeductions) { this(EmployeName, taxDeductions, "single"); } public Employee(String EmployeName, int taxDeductions, String maritalStatus) { this.myEmployeeName = EmployeName; this.myTaxDeductions = taxDeductions; this.myMaritalStatus = maritalStatus; } } class Employee(): def __init__(self, employeeName , taxDeductions=1 , maritalStatus="single" ): self.employeeName = employeeName self.taxDeductions = taxDeductions self.maritalStatus = maritalStatus Saturday, May 4, 13

Slide 23

Slide 23 text

Life is Short (You Need Python) - Bruce Eckel (Thinking in C++) Saturday, May 4, 13

Slide 24

Slide 24 text

Useful books: Python for Kids http://oreil.ly/10boyUq The Quick Python Book, 2nd Ed http://amzn.to/lXKzH5 Google's Python Class https://developers.google.com/edu/python/ Saturday, May 4, 13

Slide 25

Slide 25 text

Let’s start by writing text based games Saturday, May 4, 13

Slide 26

Slide 26 text

Saturday, May 4, 13

Slide 27

Slide 27 text

A Skeleton • Let’s start with the most basic pygame program template.py template.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from pygame import * from pygame.sprite import * from random import * init() screen = display.set_mode((640, 480)) display.set_caption('Window name!') while True: e = event.poll() if e.type == QUIT: quit() break screen.fill(Color("white")) display.update() Saturday, May 4, 13

Slide 28

Slide 28 text

Surface • Most of the game elements you see are represented as Surface • display.set_mode((x, y)) creates your canvas – it returns a Surface object Useful surface methods: • fill("color") fills the surface object it's been called from • blit(surface, area) paints the source surface onto the rectangle bounded by the area tuple –Example: screen.blit(ball, (50,50)) Saturday, May 4, 13

Slide 29

Slide 29 text

Rect • Objects that store rectangular coordinates • Call .get_rect()on a surface to get its bounding box Rectangle methods/variables: • .center holds the object's center as a tuple • .colliderect(target) returns True if the parameter overlaps with the object • .collidepoint(target) returns True if the target point overlaps with the object Saturday, May 4, 13

Slide 30

Slide 30 text

Media • Loading an image: –img = image.load("file.gif").convert() • Getting a bounding rectangle: –img_rect = img.get_rect() • Loading and playing a sound file: –mixer.Sound("file.wav").play() Saturday, May 4, 13

Slide 31

Slide 31 text

Sprite • Simple base class visible game objects inherit from. Ball.py Ball.py 1 2 3 4 5 6 7 8 9 10 11 from pygame import * from pygame.sprite import * class Ball(Sprite): def __init__(self): Sprite.__init__(self) self.image = image.load("ball.png").convert() self.rect = self.image.get_rect() def update(self): self.rect.center = mouse.get_pos() Saturday, May 4, 13

Slide 32

Slide 32 text

Using Sprites • They're just objects: initialize them –ball = Ball() • Create a group of sprites in main –sprites = RenderPlain(sprite1, sprite2) • Groups know how to draw and update –sprites.update() –sprites.draw(surface) Saturday, May 4, 13

Slide 33

Slide 33 text

Events • User input such as clicking, moving mouse or key presses • Add more branches to test the result of event.poll() • Events to test for: –QUIT –MOUSEBUTTONDOWN –JOYBUTTONDOWN • Testing for the letter ‘d’ being pressed using KEYDOWN if e.type == KEYDOWN: if e.key == K_d: … Saturday, May 4, 13

Slide 34

Slide 34 text

Adding Text • f = font.Font(font, size) goes before your game loop – Example: f = font.Font(None, 25) – Usually, None is a good enough font! • text = Font.render(text, antialias, color) – Example: text = f.render("Hello!", True, Color("green")) – Returns a surface • Must be blit, just like any other surface – Example: screen.blit(t, (320, 0)) Saturday, May 4, 13

Slide 35

Slide 35 text

Let’s dissect a game! Saturday, May 4, 13

Slide 36

Slide 36 text

Join PythonKC -> http:// www.meetup.com/pythonkc/ Saturday, May 4, 13

Slide 37

Slide 37 text

Juan Gomez Co-Founder of PyhtonKC Twitter: @_juandg Thanks! Saturday, May 4, 13

Slide 38

Slide 38 text

PLATINUM)SPONSORS) GOLD)SPONSORS) SILVER)SPONSORS) Saturday, May 4, 13