Game Programming © Wiley Publishing. 2006. All Rights Reserved. Writing Your Fir

Game Programming © Wiley Publishing. 2006. All Rights Reserved. Writing Your Fir

Game Programming © Wiley Publishing. 2006. All Rights Reserved. Writing Your Fir

Lazarus, Anthony, Copy Editor has reference to this Academic Journal, PHwiki organized this Journal Game Programming © Wiley Publishing. 2006. All Rights Reserved. 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 © Wiley Publishing. 2006. All Rights Reserved. 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 as long as complex output Stations Along the Way © Wiley Publishing. 2006. All Rights Reserved.

Knoxville Institute of Hair Design TN

This Particular University is Related to this Particular Journal

St in addition to ard outline Why Use Python Freely available Plat as long as m-indendent Easy to learn Powerful Extensible Transferable Installing Python Download binaries from Book examples tested with Python 2.4 Not all libraries yet available as long as 2.5 Run installer with default parameters Mac / Linux users follow Python website instructions Starting the Engine Run Python in comm in addition to line console Windows: Start menu – run – cmd Mac / Linux: Start terminal console Move to Python directory if necessary Type python to begin session

Interacting with the Console The >>> symbol is Python’s prompt Type print “Hello, there!” View immediate results 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 5/2 is incorrect! We’ll return to this. Storing In as long as mation 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 in as long as mation. answer = 5 + 3 print answer

Using IDLE IDLE is an Integrated Development Environment as long as Python It comes st in addition to ard with most versions of Python It’s a text editor specialized as long as creating in addition to testing Python programs IDLE’s two modes If you type idle into the comm in addition to 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 Storing code in a file Open a new IDLE window Note the different menus Continue writing code (nothing happens immediately) Save your file with a .py extension Run the program (F5)

Your first Interactive Game Type this code into IDLE: “””Cheese Shop demonstrate comments, raw input, in addition to string variables from Game Programming – L-line, Andy Harris 4/10/06 “”” tell the user something print “Welcome to the cheese shop!” get in as long as mation from the user cheeseType = raw-input(“What kind of cheese would you like “) we don’t have that kind print “Sorry, We’re all out of” print cheeseType Using docstrings The triple-quoted string contains special comments about the program: Program name Author Date Filename Printing output print prints output to the screen Values in quotes are printed exactly The value of a variable is printed

Getting Input from the User raw-input gets data from the screen It prints a prompt It retrieves text data It expects a variable to store result into Variable Naming Conventions Descriptive No spaces Case-sensitive Manageable length 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

Building a Basic String Type a 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 playerName Princess OoogieBoogie 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 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.

Exploring String Methods “”” 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 Selected String Methods Making the Cartoon Version The “cartoon voice” required a couple of steps Convert the string to upper-case Replace “R” with “W” Convert back to title case Program catches uppercase in addition to lowercase “R”

Lazarus, Anthony MarketWatch Broadband Copy Editor

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 salami Please view String Slicing Example Guide 0 1 2 3 4 5 6 salami >>> meat = “salami” >>> print meat[2:5] ‘lam’ >>> print meat[0: 3] ‘sal’ Print meat[4:6] ‘mi’ More String Slicing 0 1 2 3 4 5 6 salami >>> 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’

String Interpolation Sometimes you want to combine variables in addition to literal values Python has a nice technique called string interpolation %s indicates a string placeholder Second % indicates variable to stuff into string >>> userName = “Benjamin” >>> print “Hi there, %s!” % userName Hi there, Benjamin! Interpolating numbers Use %s to embed a string value Use %d to embed an integer (number without decimal values) Use %f to embed a real number (with decimal values) Use %.2f to embed a real number to two places (more on numbers in chapter 2) Interpolating multiple values A string interpolation can include multiple values. Use a placeholder as long as each value Use parentheses to make a list of variables Please view Print “%s is %d years old today.” % (name, age)

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 in addition to output be useful in more advanced games When might a program require string slicing

Lazarus, Anthony Copy Editor

Lazarus, Anthony is from United States and they belong to MarketWatch Broadband and they are from  San Francisco, United States got related to this Particular Journal. and Lazarus, Anthony deal with the subjects like Stock Market and Wall Street; Stocks and Equities

Journal Ratings by Knoxville Institute of Hair Design

This Particular Journal got reviewed and rated by Knoxville Institute of Hair Design and short form of this particular Institution is TN and gave this Journal an Excellent Rating.