Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to...
-
Upload
duane-atkinson -
Category
Documents
-
view
220 -
download
3
Transcript of Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to...
![Page 1: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/1.jpg)
Game Game ProgrammingProgramming
© Wiley Publishing. 2007. All Rights Reserved.
The L LineThe L LineThe Express Line to LearningThe Express Line to Learning
L Line
L
![Page 2: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/2.jpg)
Writing Your First Program 1
•Installing Python•Interacting with the Python console•Building a program that greets the user•Getting basic text input from the user
Stations Along the Way
![Page 3: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/3.jpg)
Writing Your First Program(continued)
1
•Building a string variable with an appropriate name•Outputting the value of a string variable to the user•Creating subsets of a string with slicing•Using string interpolation for complex output
Stations Along the Way
![Page 4: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/4.jpg)
Why Use Python?
Freely availablePlatform-independentEasy to learnPowerfulExtensibleTransferable
![Page 5: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/5.jpg)
Installing Python
Download binaries from www.python.org.The book examples were tested with Python
2.4.Not all libraries are yet available for 2.5.Run the installer with default parameters.Mac or Linux users, follow the Python Web
site instructions.
![Page 6: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/6.jpg)
Starting the Engine
Run Python in the command-line console.
Windows:• StartRuncmd
Mac/Linux:• Start terminal console
Move to the Python directory, if necessary.
Type python to begin your session.
![Page 7: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/7.jpg)
Interacting with the Console
The >>> symbol is Python’s prompt.Type print "Hello, there!"View immediate results.
![Page 8: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/8.jpg)
Using Python as a Simple Calculator
Type a simple math problem (4 + 3) at the >>> prompt.
View the immediate response.Try other operations:
• Multiply = *• Divide = /• Try parentheses• Note that 5/2 is incorrect! I'll return to this.
![Page 9: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/9.jpg)
Storing Information in Variables
Type the following on the console:
Retrieve the answer with this code:
Variables are locations in memory designated to hold a piece of information.
answer = 5 + 3
print answer
![Page 10: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/10.jpg)
Using IDLE
IDLE is an Integrated Development Environment for Python.
It comes standard with most versions of Python.
It's a text editor specialized for creating and testing Python programs.
![Page 11: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/11.jpg)
IDLE's Two Modes
If you type idle into the command line, the IDLE window shows the >>> prompt.
This is interactive mode. You can type instructions directly.
File-new calls up a new IDLE window that acts more like a text editor.
Note the menus are slightly different in the two modes.
![Page 12: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/12.jpg)
Storing Code in a File
1. Open a new IDLE window.
2. Note the different menus.
3. Continue writing code (nothing happens immediately).
4. Save your file with a .py extension.
5. Run the program (F5).
![Page 13: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/13.jpg)
Your First Interactive Game
Type this code into IDLE:
"""Cheese Shop cheeseShop.py demonstrate comments, raw input, and string variables from Game Programming - L-line, Andy Harris 4/10/06 """#tell the user somethingprint "Welcome to the cheese shop!"#get information from the usercheeseType = raw_input("What kind of cheese would you like? ")#we don't have that kind...print "Sorry, We're all out of"print cheeseType
![Page 14: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/14.jpg)
Using docstrings
The triple-quoted string contains special comments about the program:• Program name• Author• Date• Filename
![Page 15: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/15.jpg)
Printing Output
print prints output to the screen.Values in quotes are printed exactly.The value of a variable is printed.
![Page 16: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/16.jpg)
Getting Input from the User
raw_input gets data from the screen.It prints a prompt.It retrieves text data.It expects a variable in which to store
the result.
![Page 17: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/17.jpg)
Variable Naming Conventions
DescriptiveNo spacesCase-sensitiveManageable length
![Page 18: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/18.jpg)
Introducing Strings
Programmers call text strings.The storage mechanism uses a
sequence of memory cells.This reminded early programmers of
beads on a string.Variables that contain text are called
string variables.
![Page 19: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/19.jpg)
Building a Basic String
Type string assignment in the console (the >>> will already be there):
Output the value of the string:
Storing a string value into a variable automatically creates a string variable.
>>> playerName = "Princess Oogieboogie"
>>> print playerNamePrincess OoogieBoogie
![Page 20: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/20.jpg)
Introducing Methods
Python uses Object-Oriented Programming (OOP).
All entities are objects.Objects have methods (things they
can do).Strings have a bunch of interesting
methods.
![Page 21: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/21.jpg)
Discovering String Methods
Python has a very rich built-in help system you can use to learn about objects.
Type help("str") at the console to learn about the str (string) object.
Note: help("string") produces results too, but these are older functions that have been replaced by str.
![Page 22: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/22.jpg)
Exploring String Methods""" nameGame.py illustrate basic string functions Andy Harris 3/15/06"""userName = raw_input("Please tell me your name: ")print "I will shout your name: ", userName.upper()print "Now all in lowercase: ", userName.lower()print "How about inverting the case? ", userName.swapcase()numChars = len(userName)print "Your name has", numChars, "characters"print "Now I'll pronounce your name like a cartoon character:"userName = userName.upper()userName = userName.replace("R", "W")userName = userName.title()print userName
![Page 23: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/23.jpg)
Selected String Methods
String Method Description
stringName.upper() Converts stringName into all uppercase
stringName.lower() Converts stringName into all lowercase
stringName.swapcase() Converts uppercase to lowercase, lowercase to uppercase
stringName.replace(old, new) Looks in the string for the value old and replaces it with the value new
stringName.title() Capitalizes each word in the string
len(string) Returns the length of the string
![Page 24: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/24.jpg)
Making the Cartoon Version
The "cartoon voice" requires a couple of steps.
Convert the string to uppercase.Replace "R" with "W."Convert back to title case.Program catches uppercase and
lowercase "R."
![Page 25: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/25.jpg)
Slicing Strings
You can extract parts of a string.This technique is called slicing.String has positions between
characters:• 0 1 2 3 4 5 6• |s|a|l|a|m|i|
Please view salamiSlice.py.
![Page 26: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/26.jpg)
String Slicing Example
Guide:• 0 1 2 3 4 5 6• |s|a|l|a|m|i|
>>> meat = "salami">>> print meat[2:5]'lam'>>> print meat[0: 3]'sal'Print meat[4:6]'mi'
![Page 27: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/27.jpg)
More String Slicing
• 0 1 2 3 4 5 6• |s|a|l|a|m|i|>>> meat = "salami">>> print meat[0:3]'sal'print meat[:3]'sal'print meat[4:6]'mi'print meat[4:]'mi'print meat[-3:]'ami'Print meat[4]'m'
![Page 28: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/28.jpg)
String Interpolation
Sometimes, you want to combine variables and literal values.
Python has a nice technique called string interpolation:
%s indicates a string placeholder.The second % indicates variable to stuff into
string.
>>> userName = "Benjamin">>> print "Hi there, %s!" % userNameHi there, Benjamin!
![Page 29: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/29.jpg)
Interpolating Numbers
Use %s to embed a string value.Use %d to embed an integer (a
number without decimal values).Use %f to embed a real number (with
decimal values).Use %.2f to embed a real number to
two places.You can find more on numbers in
Chapter 2.
![Page 30: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/30.jpg)
Interpolating Multiple Values
A string interpolation can include multiple values:
Use a placeholder for each value.Use parentheses to make a list of
variables.Please view interpolation.py.
Print "%s is %d years old today." % (name, age)
![Page 31: Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.](https://reader035.fdocuments.in/reader035/viewer/2022062516/56649ddb5503460f94ad17d6/html5/thumbnails/31.jpg)
Discussion Topics
How does Python compare to any other language you might know?
Why is it important to work with string values?
How will basic input and output be useful in more advanced games?
When might a program require string slicing?