Games Development 2 Further Python Integrating with C - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Games Development 2 Further Python Integrating with C

Description:

Wide range of string support: # Basic operations. print 'Hello ' 'World' # Hello World ... Argument unpacking. def VectorLength( x, y ): vector = (10, 15) ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 14
Provided by: lauren80
Category:

less

Transcript and Presenter's Notes

Title: Games Development 2 Further Python Integrating with C


1
Games Development 2Further Python / Integrating
with C
  • CO3301
  • Week 13

2
Contents
  • Further Python Examples
  • Strings
  • Further Conditions
  • Working with Lists
  • Function Techniques
  • Libraries
  • Extending / Embedding Python
  • Calling Python from C

3
Python Strings
  • Wide range of string support
  • Basic operations
  • print 'Hello ' 'World' Hello World
  • print "Spam "4 Spam Spam Spam Spam
  • Strings cant be written to easy to get round
  • str "Hello World"
  • newstr 'M' str2 Mello World
  • Example of other methods
  • print len( "Piece of String" ) Output 15
  • print "london".capitalize() Output London
  • print "too times too".replace( "too", "two" )
  • Output two times two
  • folderlist "C\Work\Python".split("\\")
  • folderlist "C", "Work", "Python

4
Further Conditions
  • More Conditions
  • Set membership
  • primes 1,2,3,5,7,11,13,17,19,23,29
  • if x in primes
  • print "x is prime"
  • if year not in range(2000, 2100, 4) 2000-gt2100
    step 4
  • print "Not a leap year"
  • String / List comparison
  • "one" lt "two" true lexicographical comparison
  • 1,2,3 lt 1,2,4 Compare elements
  • 1,3 lt 1,2,3 Shorter list always less than

5
Working with Lists
  • List operations
  • Working with this list
  • a 66.25, 333, 333, 1, 1234.5
  • Counting instances of a value
  • print a.count(333), a.count(66.25), a.count('x')
  • Output 2 1 0
  • Insertion (anywhere), appending (at the end)
  • a.insert(2, -1) Location, value
  • a.append(333)
  • print a
  • Output 66.25, 333, -1, 333, 1, 1234.5, 333

6
Working with Lists
  • More list operations
  • Working with this list
  • a 66.25, 333, -1, 333, 1, 1234.5, 333
  • Removal (by location)
  • x a.pop() Remove last element
  • y a.pop(2) Remove specific element
  • print a, x, y
  • Output 66.25, 333, 333, 1, 1234.5 333 -1
  • Removal (by value)
  • a.remove(333) Remove first instance of 333
  • print a
  • Output66.25, 333, 1, 1234.5

7
Working with Lists
  • List reordering
  • Working with this list
  • a 66.25, 333, 1, 1234.5
  • Reversal
  • a.reverse()
  • print a
  • Output 1234.5, 1, 333, 66.25
  • Sorting
  • a.sort()
  • print a
  • Output1, 66.25, 333, 1234.5

8
Functional List Creation
  • List creation using functions
  • map applies function to list elts to create
    new list
  • def square(x) return xx
  • print map(square, range(1, 11))
  • Output 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
  • filter uses boolean function to filter elts
    in a list
  • def NotDiv3(x) return x 3 ! 0
  • filter(f, range(1, 15))
  • Output 1, 2, 4, 5, 7, 8, 10, 11, 13, 14
  • List comprehension powerful list creation
  • def square(x) return xx
  • primes 1,2,3,5,7,11,13,17,19,23,29
  • print square(x) for x in primes if x10 lt 5
  • Output 1,4,9,121,169,529

9
Function Techniques
  • More Function Techniques
  • Argument passing by name
  • def AddRecord( name, ID 0, job "Unemployed
    )
  • ...
  • AddRecord( "Bill", 5 ) Normal style
  • AddRecord( job"Builder", name"Bob" ) By name
  • Argument unpacking
  • def VectorLength( x, y )
  • ...
  • vector (10, 15)
  • print VectorLength( vector ) unpacks 1
    argument to 2
  • Documentation first line can be string
    literal
  • def Square(x)
  • "Returns the square of the argument"
  • return xx

10
Libraries
  • Can import support libraries
  • Mathematics as in C
  • import math
  • print math.cos( math.pi / 3 ) Output 0.5
  • Random numbers
  • import random
  • print random.choice('apple', 'pear', 'banana')
  • print random.random() random float 0-1
  • print random.randrange(6) random integer from
    0-5
  • 5 random samples from list, e.g. 30, 83, 16,
    4, 8
  • print random.sample(range(100), 5)

11
Integrating Python C
  • Must include the Python headers and libraries
  • Initialise Python at program start-up
  • Simple scripts can be run directly
  • PyRun_SimpleString( "print 'Hello World'" )
  • But no interaction between C code / data and
    script
  • Scripts can be run from a file equally easily
  • Again no interaction
  • For scripts to be useful in a game, need to
  • Pass parameters from C to scripts, e.g. entity
    UIDs
  • Get return values back
  • Call back to C from the script to access/update
    data

12
Integrating Python C
  • Basic process is intricate
  • Have Python open the script, and find function to
    call
  • Convert parameters from C types to Python
    objects
  • Call script function
  • Convert return value back to a C type
  • All Python variables are reference counted
  • Increase count when we first get them
  • Decrease when finished
  • Needed for Pythons garbage collection

13
Integrating Python C
  • We end up with Python function calls like
  • result CallPythonFn( "ScriptName", "MyFn", 1
    )
  • where 1 is a parameter passed to the Python
    function
  • To allow Python to call back into C
  • Define a set of functions that will become a
    Python import library
  • Identify import library to Python at start-up
    time
  • Each C function must convert parameters and
    return values as before
  • C functions can then be used in Python
  • import game
  • def PickUpHealthPack()
  • game.AdjustHP( 20 ) AdjustHP is a C fn
Write a Comment
User Comments (0)
About PowerShow.com