Game Programming © Wiley Publishing. 2007. All Rights Reserved. The L Line The Express Line to Learning L Line L.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Python November 14, Unit 7. Python Hello world, in class.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Introduction to C Programming
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L.
Python Programming Fundamentals
An Introduction to Textual Programming
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 9, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Computer Science 101 Introduction to Programming.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
Variables, Input, and Output. Challenge: ● Ask the user his or her name, and repeat that name to the user ● Pause video and try.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
GCSE Computing: Programming GCSE Programming Remembering Python.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
A Sample Program #include using namespace std; int main(void) { cout
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introducing Python 3 Introduction to Python. Introduction to Python L1 Introducing Python 3 Learning Objectives Know what Python is and some of the applications.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
Fundamentals of Programming I Overview of Programming
Development Environment
A Playful Introduction to Programming by Jason R. Briggs
Using the Console.
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Python
Variables, Expressions, and IO
Fill the screen challenge!
Learning Outcomes –Lesson 4
Python Lessons 13 & 14 Mr. Kalmes.
Introduction to Programming with Python
Introduction In today’s lesson we will look at: why Python?
Introducing JavaScript
Input and Output Python3 Beginner #3.
General Computer Science for Engineers CISC 106 Lecture 03
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L

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

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

Why Use Python? Freely available Platform-independent Easy to learn Powerful Extensible Transferable

Installing Python Download binaries from 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.

Starting the Engine Run Python in the command-line console. Windows: Start  Run  cmd Mac/Linux: Start terminal console Move to the Python directory, if necessary. Type python to begin your 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 ( ) 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.

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 = print answer

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.

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.

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).

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 something print "Welcome to the cheese shop!" #get information 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 in which to store the result.

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 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 """ 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

Selected String Methods String MethodDescription 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

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."

Slicing Strings You can extract parts of a string. This technique is called slicing. String has positions between characters: |s|a|l|a|m|i| Please view salamiSlice.py.

String Slicing Example Guide: |s|a|l|a|m|i| >>> meat = "salami" >>> print meat[2:5] 'lam' >>> print meat[0: 3] 'sal' Print meat[4:6] 'mi'

More String Slicing |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'

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!" % userName Hi there, Benjamin!

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.

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)

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?