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

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.