~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to docs/tut/ImportInit.html

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
TUTORIAL:Import and Initialize
 
3
--><html><head>
 
4
 
 
5
<title>Pygame Tutorials - Import and Initialize</title>
 
6
</head><body>
 
7
 
 
8
<h1 align=center><font size=-1>Pygame Tutorials</font><br>Import and Initialize</h1>
 
9
<h2 align=center>by Pete Shinners<br><font size=-1>pete@shinners.org</font></h2>
 
10
<h3 align=center>Revision 1.0, January 28th, 2002</h3>
 
11
<br><br>
 
12
 
 
13
 
 
14
Getting pygame imported and initialized is a very simple process. It is also
 
15
flexible enough to give you control over what is happening. Pygame is a
 
16
collection of different modules in a single python package. Some of the
 
17
modules are written in C, and some are written in python. Some modules
 
18
are also optional, and might not always be present.
 
19
<br>&nbsp;<br>
 
20
This is just a quick introduction on what is going on when you import pygame.
 
21
For a clearer explanation definitely see the pygame examples.
 
22
 
 
23
 
 
24
<br>&nbsp;<br>&nbsp;<br>
 
25
<h2>Import</h2>
 
26
First we must import the pygame package. Since pygame version 1.4 this
 
27
has been updated to be much easier. Most games will import all of pygame like this.
 
28
<ul><pre>import pygame
 
29
from pygame.locals import *</pre></ul>
 
30
The first line here is the only necessary one. It imports all the available pygame
 
31
modules into the pygame package. The second line is optional, and puts a limited
 
32
set of constants and functions into the global namespace of your script.
 
33
<br>&nbsp;<br>
 
34
An important thing to keep in mind is that several pygame modules are optional.
 
35
For example, one of these is the font module. When  you "import pygame", pygame
 
36
will check to see if the font module is available. If the font module is available
 
37
it will be imported as "pygame.font". If the module is not available, "pygame.font"
 
38
will be set to None. This makes it fairly easy to later on test if the font module is available.
 
39
 
 
40
<br>&nbsp;<br>&nbsp;<br>
 
41
<h2>Init</h2>
 
42
Before you can do much with pygame, you will need to initialize it. The most common
 
43
way to do this is just make one call.
 
44
<ul><pre>pygame.init()</pre></ul>
 
45
This will attempt to initialize all the pygame modules for you. Not all pygame modules
 
46
need to be initialized, but this will automatically initialize the ones that do. You can
 
47
also easily initialize each pygame module by hand. For example to only initialize the
 
48
font module you would just call.
 
49
<ul><pre>pygame.font.init()</pre></ul>
 
50
Note that if there is an error when you initialize with "pygame.init()", it will silently fail.
 
51
When hand initializing modules like this, any errors will raise an exception. Any
 
52
modules that must be initialized also have a "get_init()" function, which will return true
 
53
if the module has been initialized.
 
54
<br>&nbsp;<br>
 
55
It is safe to call the init() function for any module more than once.
 
56
 
 
57
<br>&nbsp;<br>&nbsp;<br>
 
58
<h2>Quit</h2>
 
59
Modules that are initialized also usually have a quit() function that will clean up.
 
60
There is no need to explicitly call these, as pygame will cleanly quit all the
 
61
initilized modules when python finishes.
 
62
 
 
63
</body></html>