Download Python for Kids: A Playful Introduction to Programming PDF

TitlePython for Kids: A Playful Introduction to Programming
TagsPython
LanguageEnglish
File Size6.5 MB
Total Pages366
Table of Contents
                            Python for Kids
About the Author
About the Illustrator
About the technical Reviewers
Acknowledgments
Introduction
	Why Python?
	How to Learn to Code
	Who Should Read This Book
	What’s in This Book
	The Companion Website
	Have Fun!
I. Learning to Program
	1. Not All Snakes Slither
		A Few Words About Language
		Installing Python
			Installing Python on Windows 7
			Installing Python on Mac OS X
			Installing Python on Ubuntu
		Once You’ve Installed Python
		Saving Your Python Programs
		What You Learned
	2. Calculations and Variables
		Calculating with Python
			Python Operators
			The Order of Operations
		Variables Are Like Labels
		Using Variables
		What You Learned
	3. Strings, Lists, Tuples, and Maps
		Strings
			Creating Strings
			Handling Problems with Strings
			Embedding Values in Strings
			Multiplying Strings
		Lists Are More Powerful Than Strings
			Adding Items to a List
			Removing Items from a List
			List Arithmetic
		Tuples
		Python Maps Won’t Help You Find Your Way
		What You Learned
		Programming Puzzles
			#1: Favorites
			#2: Counting Combatants
			#3: Greetings!
	4. Drawing with Turtles
		Using Python’s turtle Module
			Creating a Canvas
			Moving the Turtle
		What You Learned
		Programming Puzzles
			#1: A Rectangle
			#2: A Triangle
			#3: A Box Without Corners
	5. Asking Questions with if and else
		if Statements
			A Block Is a Group of Programming Statements
			Conditions Help Us Compare Things
		if-then-else Statements
		if and elif Statements
		Combining Conditions
		Variables with No Value—None
		The Difference Between Strings and Numbers
		What You Learned
		Programming Puzzles
			#1: Are You Rich?
			#2: Twinkies!
			#3: Just the Right Number
			#4: I Can Fight Those Ninjas
	6. Going Loopy
		Using for Loops
		While We’re Talking About Looping . . .
		What You Learned
		Programming Puzzles
			#1: The Hello Loop
			#2: Even Numbers
			#3: My Five Favorite Ingredients
			#4: Your Weight on the Moon
	7. Recycling Your Code with Functions and Modules
		Using Functions
			Parts of a Function
			Variables and Scope
		Using Modules
		What You Learned
		Programming Puzzles
			#1: Basic Moon Weight Function
			#2: Moon Weight Function and Years
			#3: Moon Weight Program
	8. How to Use Classes and Objects
		Breaking Things into Classes
			Children and Parents
			Adding Objects to Classes
			Defining Functions of Classes
			Adding Class Characteristics as Functions
			Why Use Classes and Objects?
			Objects and Classes in Pictures
		Other Useful Features of Objects and Classes
			Inherited Functions
			Functions Calling Other Functions
		Initializing an Object
		What You Learned
		Programming Puzzles
			#1: The Giraffe Shuffle
			#2: Turtle Pitchfork
	9. Python’s Built-in Functions
		Using Built-in Functions
			The abs Function
			The bool Function
			The dir Function
			The eval Function
			The exec Function
			The float Function
			The int Function
			The len Function
			The max and min Functions
			The range Function
			The sum Function
		Working with Files
			Creating a Test File
				Creating a New File in Windows
				Creating a New File in Mac OS X
				Creating a New File in Ubuntu
			Opening a File in Python
				Opening a Windows File
				Opening a Mac OS X File
				Opening an Ubuntu File
			Writing to Files
		What You Learned
		Programming Puzzles
			#1: Mystery Code
			#2: A Hidden Message
			#3: Copying a File
	10. Useful Python Modules
		Making Copies with the copy Module
		Keeping Track of Keywords with the keyword Module
		Getting Random Numbers with the random Module
			Using randint to Pick a Random Number
			Using choice to Pick a Random Item from a List
			Using shuffle to Shuffle a List
		Controlling the Shell with the sys Module
			Exiting the Shell with the exit function
			Reading with the stdin Object
			Writing with the stdout Object
			Which Version of Python Am I Using?
		Doing Time with the time Module
			Converting a Date with asctime
			Getting the Date and Time with localtime
			Taking Some Time Off with sleep
		Using the pickle Module to Save Information
		What You Learned
		Programming Puzzles
			#1: Copied Cars
			#2: Pickled Favorites
	11. More Turtle Graphics
		Starting with the Basic Square
		Drawing Stars
		Drawing a Car
		Coloring Things In
			A Function to Draw a Filled Circle
			Creating Pure Black and White
		A Square-Drawing Function
		Drawing Filled Squares
		Drawing Filled Stars
		What You Learned
		Programming Puzzles
			#1: Drawing an Octagon
			#2: Drawing a Filled Octagon
			#3: Another Star-Drawing Function
	12. Using tkinter for Better Graphics
		Creating a Clickable Button
		Using Named Parameters
		Creating a Canvas for Drawing
		Drawing Lines
		Drawing Boxes
			Drawing a Lot of Rectangles
			Setting the Color
		Drawing Arcs
		Drawing Polygons
		Displaying Text
		Displaying Images
		Creating Basic Animation
		Making an Object React to Something
		More Ways to Use the Identifier
		What You Learned
		Programming Puzzles
			#1: Fill the Screen with Triangles
			#2: The Moving Triangle
			#3: The Moving Photo
II. Bounce!
	13. Beginning Your First Game: Bounce!
		Whack the Bouncing Ball
		Creating the Game Canvas
		Creating the Ball Class
		Adding Some Action
			Making the Ball Move
			Making the Ball Bounce
			Changing the Ball’s Starting Direction
		What You Learned
	14. Finishing Your First Game: Bounce!
		Adding the Paddle
			Making the Paddle Move
			Finding Out When the Ball Hits the Paddle
		Adding an Element of Chance
		What You Learned
		Programming Puzzles
			#1: Delay the Game Start
			#2: A Proper “Game Over”
			#3: Accelerate the Ball
			#4: Record the Player’s Score
III. Mr. Stick Man Races for the Exit
	15. Creating Graphics for the Mr. Stick Man Game
		Mr. Stick Man Game Plan
		Getting GIMP
		Creating the Game Elements
			Preparing a Transparent Image
			Drawing Mr. Stick Man
				Mr. Stick Man Running to the Right
				Mr. Stick Man Running to the Left
			Drawing the Platforms
			Drawing the Door
			Drawing the Background
			Transparency
		What You Learned
	16. Developing the Mr. Stick Man Game
		Creating the Game Class
			Setting the Window Title and Creating the Canvas
			Finishing the _init_ Function
			Creating the mainloop Function
		Creating the Coords Class
		Checking for Collisions
			Sprites Colliding Horizontally
			Sprites Colliding Vertically
			Putting It All Together: Our Final Collision-Detection Code
				The collided_left Function
				The collided_right Function
				The collided_top Function
				The collided_bottom Function
		Creating the Sprite Class
		Adding the Platforms
			Adding a Platform Object
			Adding a Bunch of Platforms
		What You Learned
		Programming Puzzles
			#1: Checkerboard
			#2: Two-Image Checkerboard
			#3: Bookshelf and Lamp
	17. Creating Mr. Stick Man
		Initializing the Stick Figure
			Loading the Stick Figure Images
			Setting Up Variables
			Binding to Keys
		Turning the Stick Figure Left and Right
		Making the Stick Figure Jump
		What We Have So Far
		What You Learned
	18. Completing the Mr. Stick Man Game
		Animating the Stick Figure
			Creating the Animate Function
				Checking for Movement
				Changing the Image
			Getting the Stick Figure’s Position
			Making the Stick Figure Move
				Starting the move Function
				Has the stick Figure Hit the Bottom or Top of the Canvas?
				Has the Stick Figure Hit the Side of the Canvas?
				Colliding with Other Sprites
				Colliding at the Bottom
				Checking Left and Right
		Testing Our Stick Figure Sprite
		The Door!
			Creating the DoorSprite Class
			Detecting the Door
			Adding the Door Object
		The Final Game
		What You Learned
		Programming Puzzles
			#1: “You Win!”
			#2: Animating the Door
			#3: Moving Platforms
A. Where to Go from Here
	Games and Graphics Programming
		PyGame
	Programming Languages
		Java
		C/C++
		C#
		PHP
		Objective-C
		Perl
		Ruby
		JavaScript
	Final Words
B. Python Keywords
	and
	as
	assert
	break
	class
	continue
	def
	del
	elif
	else
	except
	finally
	for
	from
	global
	if
	import
	in
	is
	lambda
	not
	or
	pass
	raise
	return
	try
	while
	with
	yield
Glossary
Index
About the Author
Updates
Copyright
                        
Document Text Contents
Page 2

Python for Kids

Jason R. Briggs

Page 183

itself in the correct position within the window. If that function isn’t called,
nothing will display properly.

Drawing Lines
To draw a line on the canvas, we use pixel coordinates. Coordinates determine
the positions of pixels on a surface. On a tkinter canvas, coordinates describe
how far across the canvas (from left to right) and how far down the canvas (top
to bottom) to place the pixel.
For example, since our canvas is 500 pixels wide by 500 pixels high, the
coordinates of the bottom-right corner of the screen are (500, 500). To draw the
line shown in the following image, we would use the starting coordinates (0, 0)
and ending coordinates (500, 500).



We specify the coordinates using the create_line function, as shown here:


>>> from tkinter import *

>>> tk = Tk()

Page 184

>>> canvas = Canvas(tk, width=500, height=500)

>>> canvas.pack()

>>> canvas.create_line(0, 0, 500, 500)

1

The create_line function returns 1, which is an identifier—we’ll learn more
about that later. If we had done the same thing with the turtle module, we
would have needed the following code:


>>> import turtle

>>> turtle.setup(width=500, height=500)

>>> t = turtle.Pen()

>>> t.up()

>>> t.goto(−250, 250)

>>> t.down()

>>> t.goto(500, −500)

So the tkinter code is already an improvement. It’s slightly shorter and a bit
simpler.
Now let’s look at some of the functions available on the canvas object that we
can use for some more interesting drawings.

Drawing Boxes
With the turtle module, we drew a box by moving forward, turning, moving
forward, turning again, and so on. Eventually, we were able to draw a
rectangular or square box by changing how far we moved forward.



The tkinter module makes it a lot easier to draw a square or rectangle. All you
need to know are the coordinates for the corners. Here’s an example (you can
close the other windows now):


>>> from tkinter import *

>>> tk = Tk()

>>> canvas = Canvas(tk, width=400, height=400)

>>> canvas.pack()

>>> canvas.create_rectangle(10, 10, 50, 50)

In this code, we use tkinter to create a canvas that is 400 pixels wide by 400
pixels high, and we then draw a square in the top-left corner of the window, like

Page 365

the Side of the Canvas?
Colliding with Other
Sprites
Colliding at the Bottom
Checking Left and Right

Testing Our Stick Figure Sprite
The Door!

Creating the DoorSprite Class
Detecting the Door
Adding the Door Object

The Final Game
What You Learned
Programming Puzzles

#1: “You Win!”
#2: Animating the Door
#3: Moving Platforms

A. Where to Go from Here
Games and Graphics Programming

PyGame
Programming Languages

Java
C/C++
C#
PHP
Objective-C
Perl
Ruby
JavaScript

Final Words
B. Python Keywords

and
as
assert
break
class
continue
def
del
elif

Page 366

else
except
finally
for
from
global
if
import
in
is
lambda
not
or
pass
raise
return
try
while
with
yield

Glossary
Index
About the Author
Updates
Copyright

Similer Documents