some python basics
play

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


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

  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?

  3. File Input Example

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

  5. Console Input: Python ● Let's test it out: ● ● 1. Program that says hello ● ● 2. Program that multiplies two numbers

  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

  7. File Input: Python ● Let's try it out -->

  8. Random Numbers ● Random numbers are VERY important for games ● ● Why?

  9. Random Numbers ● Import random ● ● random.random() #seems redundant :) ● ● Returns a random number between 0 and 1 (1 not inclusive, but 0 is)

  10. Random Numbers ● Let's try it out -->

  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

  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]

  13. Lists ● Let's try it -->

Recommend


More recommend