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

[pygame] Re: Very confused...



I haven't seen any replies yet, but I think I got my head arouind this one...

I think the thing I was missing is that the base sprite class (of which I was creating an instance) does not "remember" its own location.  The first two cases in the main program work because they use the list of rects that was saved when the sprites were created and placed.  The second two cases don't work because they are trying to retrieve rect information from the base sprite object - but it isn't there, so it returns [0,0].

Of course, in a real program, I would subclass the base sprite class and include the rect in my subclass.  With the code below, I was trying to port an example from KPL with as few changes to the structure of the code as possible, as an illustration.  But I think it might be more clear to just do things the "pygamey" way and accept that it's going to be a different way of doing things than KPL, which is not really object-oriented (at least in the current 1.1 release...).

(If my assumptions above are wrong, hopefully someone will steer me straight...)


Warren Sande


----- Original Message ----
From: Wsande <wsande@xxxxxxxxx>
To: pygame-users@xxxxxxxx
Sent: Monday, March 27, 2006 5:47:07 PM
Subject: Very confused...

I'm very confused about the way a simple bit of code is (not) working...
 
Here's some code for displaying 3 different sprites (of Christmas trees) at random x-locations at the bottom of a pygame window:
 
import sys, pygame
from random import *
size = width, height = 640, 480
white = 255, 255, 255
screen = pygame.display.set_mode(size)
trees = []
trees_rect = []
pygame.init()
tree_group = pygame.sprite.Group()
 
def loadTrees():
    for i in range (0, 3):
        trees.append(pygame.sprite.Sprite())
        trees[i].image = pygame.image.load("c:/local_documents/python/pygame_examples/data/tree" + str(i+1) + ".png")
        trees_rect.append(trees[i].image.get_rect())
        trees_rect[i].bottomleft = [randint(0, width-150), height-50]
        trees_rect[i].move_ip(0, 0)
        print str(trees_rect[i].left), str(trees_rect[i].top)
        tree_group.add(trees[i])
   
#--------------------------------
#Main Program
loadTrees()
screen.fill(white)
 
##for i in range (0, 3):
##    print str(trees_rect[i].left), str(trees_rect[i].top)
##    screen.blit(trees[i].image, trees_rect[i])
 
for i in range (0, 3):
    rect = trees_rect[i]
    print str(rect.left), str(rect.top)
    screen.blit(trees[i].image, rect)
 
##for i in range (0, 3):
##    rect = trees[i].image.get_rect()
##    print str(rect.left), str(rect.top)
##    screen.blit(trees[i].image, rect)
 
##for tree in tree_group:
##    rect = tree.image.get_rect()
##    print str(rect.left), str(rect.top)
##    screen.blit(tree.image, rect)
 
pygame.display.flip()
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

You can see the four different methods I'm trying for displaying the trees (and printing their locations).  Three of them are comented out, one is not.  The first two work fine, but I'm confused as to why the other two do not work.  If I uncomment either of the last two *for* blocks, the trees all get positioned at 0,0.  I don't understand why.  i'm sure this is something fundamental and simple, but I just can't get to the bottom of it.
 
Any help would be greatly appreciated.
 
Regards,
Warren Sande