[pygame] Rect() not imported

2008-12-14 Thread Gonzalo Castro
Hello everybody! :)

I'm reading some code from pygame's web and I found something that I
don't really understand. I'll really apreciate your help ;)

###
import sys, os
import random

import pygame
from pygame.locals import *

class Player(pygame.sprite.Sprite):

def __init__(self):

pygame.sprite.Sprite.__init__(self, self.containers)
self.image = pygame.Surface((16, 64))
self.image.fill((255, 255, 255), (2, 0, 12, 64))
self.image.fill((255, 255, 255), (0, 2, 16, 60))
self.rect = self.image.get_rect(midleft = (16, 240))
self._rect = pygame.Rect(self.rect) ###HERE IS THE
PROBLEM

def update(self):
self._rect = Rect(self.rect)

key = pygame.key.get_pressed()
if key[K_UP]:
self.rect.move_ip(0, -5)
if key[K_DOWN]:
self.rect.move_ip(0, 5)

if self.rect.bottom  480:
self.rect.bottom = 480
if self.rect.top  0:
self.rect.top = 0


This is all the imports and a class from the game PyPong.pyw. It works
perfectly for me, but I don't _understand_ how the function Rect works
if here, the module pygame is imported with import pygame, not import
* from pygame.

I used python help and I only found help about:
pygame.Rect()
But I still don't understand why does it works. Cloud anyone explain it to me?

Thanks a lot.


Re: [pygame] Rect() not imported

2008-12-14 Thread Michael George
If you import a module, it introduces the name of that module (pygame in 
this case) into the local scope.  That means you can access things 
inside that module using that name (e.g. pygame.Rect).   On the other 
hand, the from module import name construct imports the name itself.  
For example, if you said from pygame import Rect, then you could use 
Rect instead of pygame.Rect.


Don't know if that's any clearer :)

--Mike

Gonzalo Castro wrote:

Hello everybody! :)

I'm reading some code from pygame's web and I found something that I
don't really understand. I'll really apreciate your help ;)

###
import sys, os
import random

import pygame
from pygame.locals import *

class Player(pygame.sprite.Sprite):

def __init__(self):

pygame.sprite.Sprite.__init__(self, self.containers)
self.image = pygame.Surface((16, 64))
self.image.fill((255, 255, 255), (2, 0, 12, 64))
self.image.fill((255, 255, 255), (0, 2, 16, 60))
self.rect = self.image.get_rect(midleft = (16, 240))
self._rect = pygame.Rect(self.rect) ###HERE IS THE
PROBLEM

def update(self):
self._rect = Rect(self.rect)

key = pygame.key.get_pressed()
if key[K_UP]:
self.rect.move_ip(0, -5)
if key[K_DOWN]:
self.rect.move_ip(0, 5)

if self.rect.bottom  480:
self.rect.bottom = 480
if self.rect.top  0:
self.rect.top = 0


This is all the imports and a class from the game PyPong.pyw. It works
perfectly for me, but I don't _understand_ how the function Rect works
if here, the module pygame is imported with import pygame, not import
* from pygame.

I used python help and I only found help about:
pygame.Rect()
But I still don't understand why does it works. Cloud anyone explain it to me?

Thanks a lot.
  




Re: [pygame] Rect() not imported

2008-12-14 Thread Michael
On Sunday 14 December 2008 18:02:18 Gonzalo Castro wrote:
 Yes, I already know that. That's why I think is strange this code
 works, because I import pygame with import pygame and I can use
 Rect() function instead of pygame.Rect() and it works!

No, you have Rect() available because you do this:

from pygame.locals import *

Michael.
-- 
http://yeoldeclue.com/blog
http://www.kamaelia.org/Home


Re: [pygame] Rect() not imported

2008-12-14 Thread Ian Mallett
It's the pygame.locals line:

 import pygame
 pygame.Rect(0,0,5,6)
rect(0, 0, 5, 6)
 Rect(0,0,5,6)

Traceback (most recent call last):
  File pyshell#2, line 1, in module
Rect(0,0,5,6)
NameError: name 'Rect' is not defined
 from pygame.locals import *
 pygame.Rect(0,0,5,6)
rect(0, 0, 5, 6)
 Rect(0,0,5,6)
rect(0, 0, 5, 6)


Ian


Re: [pygame] Rect() not imported

2008-12-14 Thread Gonzalo Castro
2008/12/14 Ian Mallett geometr...@gmail.com:
 It's the pygame.locals line:

 import pygame
 pygame.Rect(0,0,5,6)
 rect(0, 0, 5, 6)
 Rect(0,0,5,6)

 Traceback (most recent call last):
   File pyshell#2, line 1, in module
 Rect(0,0,5,6)
 NameError: name 'Rect' is not defined
 from pygame.locals import *
 pygame.Rect(0,0,5,6)
 rect(0, 0, 5, 6)
 Rect(0,0,5,6)
 rect(0, 0, 5, 6)


Lots of thanks Ian

 Ian



Re: [pygame] Rect() not imported

2008-12-14 Thread Ian Mallett
Cheers :)


Re: [pygame] Rect() not imported

2008-12-14 Thread Lenard Lindstrom

Gonzalo Castro wrote:

2008/12/14 Michael zath...@thwackety.com:
  

On Sunday 14 December 2008 18:02:18 Gonzalo Castro wrote:


Yes, I already know that. That's why I think is strange this code
works, because I import pygame with import pygame and I can use
Rect() function instead of pygame.Rect() and it works!
  

No, you have Rect() available because you do this:

from pygame.locals import *



ammm, I thougt I imported Rect() with import pygame
  

pygame.locals is just a convenience module to limit what gets added into 
ones namespace when doing a from module import *. The contents of 
pygame.locals is also found in the top level pygame package.


--
Lenard Lindstrom
le...@telus.net