PROGRAMAÇÃO EM JOGOS DIGITAIS Linguagem de Programação

Propaganda
PROGRAMAÇÃO EM
JOGOS DIGITAIS
Frutuoso Silva
Linguagem de Programação
Python
--- www.python.org
1
Modulos & Packages
Permitem organizar o código em python
Módulo
! ! ! Simply, a module is a file consisting of Python code.
A module can define functions, classes and
variables.
A module can also include runnable code.
2
Módulo
! Pode-se usar um qualquer ficheiro python como um
módulo executando a instrução import noutro
ficheiro python.
import calendar
# Calendar module
cal = calendar.month(2014, 10)
print "Here is the calendar:”
print cal;
Módulo
import calendar
# Calendar module
cal = calendar.month(2014, 10)
print "Here is the calendar:”
print cal;
3
Módulo
import time
# This is required to
# include time module.
localtime = time.localtime(time.time())
print "Local current time :", localtime
localtime =
time.asctime(time.localtime(time.time()))
print "Local current time :", localtime
Módulo
localtime = time.localtime(time.time())
print "Local current time :", localtime
localtime =
time.asctime(time.localtime(time.time()))
print "Local current time :", localtime
4
Módulo
import datetime
now =
print
print
print
print
print
datetime.datetime.now()
now.hour
now.minute
now.year
now.day
now.month
Módulo
! Podemos importar apenas uma “parte” de um
módulo;
from fib import fibonacci
! Podemos importar todo o conteúdo do módulo;
from modname import *
5
Localização dos módulos
! When you import a module, the Python interpreter
searches for the module in the following sequences:
! The
current directory.
! If the module isn't found, Python then searches each
directory in the shell variable PYTHONPATH.
! If all else fails, Python checks the default path.
On UNIX, this default path is normally
/usr/local/lib/python/
The module search path is stored in the system module sys as
the sys.path variable.
The sys.path variable contains the current directory,
PYTHONPATH, and the installation-dependent default.
Módulos
! The dir() function
import math
content = dir(math)
print content
6
Módulos
! The dir() function
import math
content = dir(math)
print content
Packages in Python
! A package is a hierarchical file directory structure
that defines a single Python application environment
that consists of modules and sub-subpackages, and
so on.
Phone
__init__.py
Pots.py
ISDN.py
G3.py
7
Packages in Python - Example
! Consider a file Pots.py available in Phone directory.
This file has following line of source code:
def Pots():
print "I'm Pots Phone"
Packages in Python - Example
! Consider a file Pots.py available in Phone directory.
! Phone/Pots.py
! file having function Pots()
Similar way, we have another two files having
different functions with the same name as above:
! Phone/Isdn.py
! Phone/G3.py
file having function Isdn()
file having function G3()
8
Packages in Python - Example
! Now, create one more file __init__.py in Phone
directory:
! Phone/__init__.py
! To make all of your functions available when you've
imported Phone, you need to put explicit import
statements in __init__.py as follows:
from Pots import Pots
from Isdn import Isdn
from G3 import G3
Packages in Python - Example
# Now import your Phone Package.
import Phone
Phone.Pots()
Phone.Isdn()
Phone.G3()
def Pots():
print "I'm Pots Phone”
#---def Isdn():
print "I'm ISDN Phone"
#---def G3():
print "I'm G3 Phone"
9
Packages in Python - Example
# Now import your Phone Package.
import Phone
Phone.Pots()
Phone.Isdn()
Phone.G3()
I'm Pots Phone
I'm ISDN Phone
I'm 3G Phone
Packages in Python - Example
Phone
__init__.py
Pots.py
Phone
package
ISDN.py
G3.py
10
Files I/O
Entrada e Saída em python
Standard Input/Output
! Printing to the Screen
! print
! Reading Keyboard Input
! raw_input()
! input()
11
Files I/O
! Opening and Closing Files
! The open Function:
! Before
you can read or write a file, you have to open it
using Python's built-in open() function.
file_object = open( file_name [, access_mode][,
buffering])
Files I/O
file_object = open( file_name [, access_mode][,
buffering])
12
Files I/O
file_object = open( file_name [, access_mode][,
buffering])
Files I/O
! The file object attributes
# Open a file
fo = open(”file.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
13
Files I/O
! Reading and Writing Files
# Open a file
fo = open(”file.txt", "wb")
fo.write( "Python is a great language.
\nYeah its great!!\n");
# Close opened file
fo.close()
Files I/O
! Reading and Writing Files
# Open a file
fo = open(”file.txt", "r+")
str = fo.read(10) #number of bytes
print "Read String is : ", str
# Close opened file
fo.close()
14
Files I/O
! Reading and Writing Files
file.txt
Python is a great language.
Yeah its great!!
# Open a file
fo = open(”file.txt", "r+")
str = fo.read(10) #number of bytes
print "Read String is : ", str
# Close opened file
fo.close()
Read String is : Python is
Files I/O
! File Positions
fo = open(”file.txt", "r+") # Open a file
str = fo.read(10);
print "Read String is : ", str
# Check current position
position = fo.tell();
print "Current file position : ", position
# Reposition pointer at the beginning once again
position = fo.seek(0, 0);
str = fo.read(10);
print "Again read String is : ", str
fo.close() # Close opened file
15
Download