Source

pygame / docs / tut / ImportInit.html

<!--
TUTORIAL:Import and Initialize
--><html><head>

<title>Pygame Tutorials - Import and Initialize</title>
</head><body>
 
<h1 align=center><font size=-1>Pygame Tutorials</font><br>Import and Initialize</h1>
<h2 align=center>by Pete Shinners<br><font size=-1>pete@shinners.org</font></h2>
<h3 align=center>Revision 1.0, January 28th, 2002</h3>
<br><br>


Getting pygame imported and initialized is a very simple process. It is also
flexible enough to give you control over what is happening. Pygame is a
collection of different modules in a single python package. Some of the
modules are written in C, and some are written in python. Some modules
are also optional, and might not always be present.
<br>&nbsp;<br>
This is just a quick introduction on what is going on when you import pygame.
For a clearer explanation definitely see the pygame examples.


<br>&nbsp;<br>&nbsp;<br>
<h2>Import</h2>
First we must import the pygame package. Since pygame version 1.4 this
has been updated to be much easier. Most games will import all of pygame like this.
<ul><pre>import pygame
from pygame.locals import *</pre></ul>
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.
<br>&nbsp;<br>
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.

<br>&nbsp;<br>&nbsp;<br>
<h2>Init</h2>
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.
<ul><pre>pygame.init()</pre></ul>
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.
<ul><pre>pygame.font.init()</pre></ul>
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.
<br>&nbsp;<br>
It is safe to call the init() function for any module more than once.

<br>&nbsp;<br>&nbsp;<br>
<h2>Quit</h2>
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.

</body></html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.