[Tutor] affecting all classes if one class is affected by an event - pygame

Kent Johnson kent37 at tds.net
Fri Nov 16 13:36:31 CET 2007


ted b wrote:
> I am trying to figure out how to make a class instance
> respond the same way as another class instance if the
> other is affected by some event. I have been playing
> around with inheritance, and have tried other stuff,
> but i am somewhat of a newbie and I have been having
> difficulty (but you guys, particularly Kent, have
> really been helping me a lot :))))
> 
> For example, in the pygame code below, i have set it
> up so that the boxes will stop if they are above the
> barrier and hit it. Well, that's what i want to
> happen, but if one of the boxes hits the barrier, the
> other box keeps going. Is there a way i can have both
> boxes stop if either of them hit the barrier. I was
> hoping there was a way that i could have the info from
> one class get passed to the other classes so i could
> stop the other box (or stop all  or only some other
> boxes if i add lots more). Should i make another
> class? Another method? Globals?

I don't understand what in the code below makes the box stop - does it 
just hit the barrier and stick there? And you want the other box to stop 
wherever it is at the time?

Here are some suggestions:
You only need one class for the boxes, the only difference is the 
initialization parameters; these can be passed in as parameters. For 
example,

class Box(pygame.sprite.Sprite):
    def __init__(self, color, center):
       pygame.sprite.Sprite.__init__(self)
       self.image=pygame.Surface((25,25))
       self.image.fill(color)
       self.rect=self.image.get_rect()
       self.rect.center = center

box1=Box((255,0,0), (30,90))
box2=Box((0,0,255), (80,50))

To stop all the boxes, maybe you want a Box class attribute which is a flag:

class Box(...):
   moving = True

Then in the Box methods you can refer to Box.moving.

HTH,
Kent

> 
> Here's the code:
> 
> #/usr/bin/env python
> 
> import pygame
> 
> from pygame.locals import *
> 
> pygame.init()
> 
> class testBox1(pygame.sprite.Sprite):
>    def __init__(self):
>       pygame.sprite.Sprite.__init__(self)
>       self.image=pygame.Surface((25,25))
>       self.image.fill((255,0,0))
>       self.rect=self.image.get_rect()
>       self.rect.center = (30,90)
> 
>    def update(self):
>       # check for user input and move left, right, up
> or down
>       keys = pygame.key.get_pressed()
>       if keys[pygame.K_w]:
>          self.rect.center = (self.rect.centerx,
> self.rect.centery-4)
>       if keys[pygame.K_s]:
>          self.rect.center = (self.rect.centerx,
> self.rect.centery+4)
>       if keys[pygame.K_a]:
>          self.rect.center = (self.rect.centerx-4,
> self.rect.centery)
>       if keys[pygame.K_d]:
>          self.rect.center = (self.rect.centerx+4,
> self.rect.centery)
>       # see if the rect hit the barrier
>       self.checkPos()
> 
>    def checkPos(self):
>       # if box rect moves below barrier's rect, halt
> box's position
>       if ((self.rect.bottom > testBarrier().rect.top)
> and (self.rect.top < testBarrier().rect.top)):
>          if ((testBarrier().rect.right >
> self.rect.right > testBarrier().rect.left) or
> (testBarrier().rect.right > self.rect.left >
> testBarrier().rect.left)):
>             self.rect.bottom = testBarrier().rect.top
>      
> class testBox2(testBox1):
>    def __init__(self):
>       pygame.sprite.Sprite.__init__(self)
>       self.image=pygame.Surface((25,25))
>       self.image.fill((0,0,255))
>       self.rect=self.image.get_rect()
>       self.rect.center = (80,50)
> 
> class testBarrier(pygame.sprite.Sprite):
>    def __init__(self):
>       pygame.sprite.Sprite.__init__(self)
>       self.image=pygame.Surface((30,4))
>       self.image.fill((0,0,0))
>       self.rect=self.image.get_rect()
>       self.rect.center = (50,150)
> 
> def main():
>    screen = pygame.display.set_mode((100,300))
> 
>    pygame.display.set_caption("testing")
> 
>    background=pygame.Surface(screen.get_size())
>    background=background.convert()
>    background.fill((255,255,255))
>    screen.blit(background, (0,0))
> 
>    box1=testBox1()
>    box2=testBox2()
>    barrier=testBarrier()
> 
>    allSprites=pygame.sprite.Group(box1, box2, barrier)
>    
>    clock=pygame.time.Clock()
>    keepGoing=True
>    while keepGoing:
>       clock.tick(30)
>       for event in pygame.event.get():
>          if event.type==pygame.QUIT:
>             keepGoing=False
> 
>       allSprites.clear(screen, background)
>       allSprites = pygame.sprite.OrderedUpdates
> (barrier, box1, box2)
>       allSprites.update()
>       allSprites.draw(screen)
>       
>       pygame.display.flip()
> 
> if __name__ == "__main__":
>    main()
> 
> Thanks for all your help!!! :)))))
> -ted
> 
> 
>       ____________________________________________________________________________________
> Be a better sports nut!  Let your teams follow you 
> with Yahoo Mobile. Try it now.  http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list