Some Python Basics Getting user input, random numbers, review of - - PowerPoint PPT Presentation

some python basics
SMART_READER_LITE
LIVE PREVIEW

Some Python Basics Getting user input, random numbers, review of - - PowerPoint PPT Presentation

Some Python Basics Getting user input, random numbers, review of if-statements, and Lists... ...and how these things relate to games! Mark Floryan User Input: Types Clicks / Button Presses From game controller, mouse, etc. Won't


slide-1
SLIDE 1

Some Python Basics

Getting user input, random numbers, review of if-statements, and Lists... ...and how these things relate to games! Mark Floryan

slide-2
SLIDE 2

User Input: Types

  • Clicks / Button Presses

–From game controller, mouse, etc. –Won't talk much about this today

  • Console Input

–User directly types input in console –Not common, when might games use this?

  • File Input

–VERY common in games...why?

slide-3
SLIDE 3

File Input Example

slide-4
SLIDE 4

Console Input: Python

  • raw_input(<String>)
  • <String> is the prompt you want user to receive.
  • Need to store the result in a variable like this:
  • MyVariable = raw_input(“Please enter something ”)
slide-5
SLIDE 5

Console Input: Python

  • Let's test it out:
  • 1. Program that says hello
  • 2. Program that multiplies two numbers
slide-6
SLIDE 6

File Input: Python

file = open('newfile.txt', 'r') for line in file: print line,

  • http://www.pythonforbeginners.com/files/reading-and-writing-files-in-

python

slide-7
SLIDE 7

File Input: Python

  • Let's try it out -->
slide-8
SLIDE 8

Random Numbers

  • Random numbers are VERY important for games
  • Why?
slide-9
SLIDE 9

Random Numbers

  • Import random
  • random.random() #seems redundant :)
  • Returns a random number between 0 and 1 (1 not inclusive, but

0 is)

slide-10
SLIDE 10

Random Numbers

  • Let's try it out -->
slide-11
SLIDE 11

Lists

  • Allows you to store multiple variables in one
  • Can access individual variables from the list
  • What would games use this for?
  • http://www.tutorialspoint.com/python/python_lists.htm
  • https://docs.python.org/2/tutorial/datastructures.html
slide-12
SLIDE 12

Lists

#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]

slide-13
SLIDE 13

Lists

  • Let's try it -->