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 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?
File Input Example
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 ”)
Console Input: Python ● Let's test it out: ● ● 1. Program that says hello ● ● 2. Program that multiplies two numbers
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
File Input: Python ● Let's try it out -->
Random Numbers ● Random numbers are VERY important for games ● ● Why?
Random Numbers ● Import random ● ● random.random() #seems redundant :) ● ● Returns a random number between 0 and 1 (1 not inclusive, but 0 is)
Random Numbers ● Let's try it out -->
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
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]
Lists ● Let's try it -->
Recommend
More recommend