Re: [pygame] Rect() not imported
Gonzalo Castro wrote: 2008/12/14 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 * 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 import *". The contents of pygame.locals is also found in the top level pygame package. -- Lenard Lindstrom
Re: [pygame] Rect() not imported
Cheers :)
Re: [pygame] Rect() not imported
2008/12/14 Ian Mallett : > It's the pygame.locals line: > import pygame pygame.Rect(0,0,5,6) > Rect(0,0,5,6) > > Traceback (most recent call last): > File "", line 1, in > 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) > > Lots of thanks Ian > Ian >
Re: [pygame] Rect() not imported
2008/12/14 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 * > ammm, I thougt I imported Rect() with import pygame > Michael. > -- Lots of thanks > http://yeoldeclue.com/blog > http://www.kamaelia.org/Home >
Re: [pygame] Rect() not imported
It's the pygame.locals line: >>> import pygame >>> pygame.Rect(0,0,5,6) >>> Rect(0,0,5,6) Traceback (most recent call last): File "", line 1, in 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) >>> Ian
Re: [pygame] Rect() not imported
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 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. > 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! > Don't know if that's any clearer :) > Sorry, maybe I didn't explained my doubt well... I find some difficulties explaining things not in my mother tongue ;) Thanks a lot > --Mike > Gonzalo :)
Re: [pygame] Rect() not imported
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.
[pygame] Rect() not imported
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.