[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] newb: importing pygame



Luke Paireepinart wrote:

"pygame.locals" isnt it just supposed to be "pygame" and no dot something. And how am I supposed
to be able to tell if in his game tutorial if that function is already available or if its coming
from the pygame module, since now some of his code doesnt have to specify the first module name
since he did a "from...import"
Hehe.
Yeah, that's what's called namespace pollution.
It's a scary thing.
I generally try to import stuff straight as pygame and call the functions like "pygame.display.init()" etc.
but I'm pretty sure most people use the "from pygame.locals import *" line.
You had a question about the dot...
That works for any kind of import statement.
If, for example, you only wanted to use the randint function from the random module,
you just type "from random import randint"
or if you had some class that you wanted to import,
you could do
"import pymedia.audio.acodec as acodec"
assuming you have the pymedia library.


back to the "from pygame.locals import *"
the reason people do this is because the pygame.locals
is just a bunch of variables that are convenient to use.
They're generally named in such a way as the average programmer
won't accidentally overwrite one of their values.

say you had a display loop:

import pygame
from pygame.locals import *
from random import randint
pygame.display.init()
screen = pygame.display.set_mode((640,480))

time_to_quit = False
while not time_to_quit:
xval = randint(0,600)
yval = randint(0,440)
screen.fill((randint(0,255),randint(0,255),randint(0,255)),(xval,yval,40,40))


   pygame.display.update((xval,yval,40,40))
   for event in pygame.event.get():
      if event.type == KEYDOWN:#keydown is a pygame.locals variable
         if event.key == K_ESCAPE:#so is k_escape.
            time_to_quit = True
  #---------------------------------
if you didn't have pygame local variables
you'd have to say
if event.type == 0:
   if event.key == 27:
      time_to_quit = True

or something.
It's harder to understand that way.
That's why people import the local variables.

I think if testing for a lot ofpossibilities like say

for e in pygame.event.get():
	if e.type is pygame.locals.QUIT:
	elif e.type is pygame.locals.KEYUP:
	elif e.type is pygame.locals.MOUSEBUTTONDOWN:
	# and maaany others ...

the code would be much nicer to read like this :

for e in pygame.event.get():
	if e.type is QUIT:
	elif e.type is KEYUP:
	elif e.type is MOUSEBUTTONDOWN:
	# ...

also looks like python takes more time to deal with pygame.locals.MOUSEBUTTONDOWN than just MOUSEBUTTONDOWN
because it has to resolve the dots everytime it does each line of code. If you are programming a game that requires very quick response to user input you *might* (i do NOT know really) find faster the second solution despite of being more ugly.
Maybe someone how knows more about optimising python wants to comment on this? it is interesting issue.


try the example, it's trippy.
-Luke