associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

Revisão 1e 2006-02-09 12:59:56

Excluir mensagem

ImportandoInicializando

Pygame Tutorials Import and Initialize by Pete Shinners pete@shinners.org Revision 1.0, January 28th, 2002

Ter o pygame importado e inicializado é um processo muito simples. Ele tambén é flexivel o bastante para dar a você controle sobre o que esta acontecendo. Pygame é uma coleção de módulos diferentes num único pacote python. Alguns desses módulos estão escritos em C e outros em python. Alguns também são opcionais e poderá nem sempre estar presentes.

Esta é apenas uma rápida introdução sobre o que está acontecendo quando você importa pygame. Para uma definitivamente clara explanação veja os exemplos do pygame.

== Import ==

Primeiro nós devemos importar o pacote pygame. Desde pygame versão 1.4 este processo se tornou muito fácil. Muitos jogos irão importar tudo de pygame como aqui:

import pygame from pygame.locals import *

A primeira linha aqui é a única realmente necessária. [continua]

The first line here is the only necessary one. It imports all the available pygame modules into the pygame package. The second line is optional, and puts a limited set of constants and functions into the global namespace of your script.

An important thing to keep in mind is that several pygame modules are optional. For example, one of these is the font module. When you "import pygame", pygame will check to see if the font module is available. If the font module is available it will be imported as "pygame.font". If the module is not available, "pygame.font" will be set to None. This makes it fairly easy to later on test if the font module is available.

Init Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.

  • pygame.init()

This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.

  • pygame.font.init()

Note that if there is an error when you initialize with "pygame.init()", it will silently fail. When hand initializing modules like this, any errors will raise an exception. Any modules that must be initialized also have a "get_init()" function, which will return true if the module has been initialized.

It is safe to call the init() function for any module more than once.

Quit Modules that are initialized also usually have a quit() function that will clean up. There is no need to explicitly call these, as pygame will cleanly quit all the initilized modules when python finishes.