[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame]Unpunch



Pete Shinners wrote:
> hmm, it shouldn't be the 'hitbox', that gets rebuilt
> every time the 
> punch function is called. very strange. it should be
> all correct 
> then. i could probably spot the problem if i saw the
> entire code, 
> but it sounds like everything should be correct at
> this point?

Here are the two classes and the main function. I am
trying to make the program so that I can punch any one
of the three elves at any time. If you see the problem
please let me know. Thanks Max


class Aimer(pygame.sprite.Sprite):
    """moves a clenched fist on the screen, following
the mouse"""
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call
Sprite initializer
        self.image, self.rect = load_image('kill.gif',
-1)
        self.punching = 0

    def update(self):
        "move the fist based on the mouse position"
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos
        if self.punching:
            self.rect.move_ip(5, 5)

    def punch(self, elf):
        "returns true if the fist collides with the
target"
        if not self.punching:
            self.punching = 1
            hitbox = self.rect.inflate(-5, -5)
            return hitbox.colliderect(elf.rect)
                
                
    def unpunch(self):
        "called to pull the fist back"
        self.punching = 0

class Keebler(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call
Sprite intializer
        images = ['keeblerchoad.gif',
'keeblerkleman.gif', 'keebler.gif', 'keebler3.gif',
                  'keebler5.gif',
'gmama.gif','keebleregypt.gif', 'keeblerskippy.gif',
'keeblerookie.gif',]
        self.image, self.rect =
load_image(random.choice(images), -1)
        self.image2 = load_image('explosion1.gif')
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.rect.bottomright = self.area.center
        self.move = 7
        self.move2 = 4
        self.dizzy = 0

    def update(self):
        if self.dizzy:
            self.kill
        else:
            self.walk()

    def walk(self):
        movels=[self.move, -self.move]
        movels2=[self.move2, -self.move2]
        newpos = self.rect.move((self.move,
self.move2))
        if not self.area.contains(newpos):
            self.move = random.choice(movels)
            self.move2 = random.choice(movels2)
            newpos = self.rect.move((self.move,
self.move2))
            self.image =
pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos
def punched(self):
        if not self.dizzy:
            self.dizzy = 1            

def main():
    """this function is called when the program
starts.
       it initializes everything it needs, then runs
in
       a loop until the function returns."""
#Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption("Keeblers")
    pygame.mouse.set_visible(0)

#Create The Backgound
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((0, 200, 225 ))
    
#Put Text On The Background, Centered
    if pygame.font:
        font = pygame.font.Font(None, 36)
        text = font.render("Keebler Hunting", 1, (10,
10, 10))
        textpos = text.get_rect()
        textpos.centerx =
background.get_rect().centerx
        background.blit(text, textpos)

#Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()
    
#Prepare Game Objects
    clock = pygame.time.Clock()
##    whiff_sound = load_sound('whiff.wav')
    punch_sound = load_sound('punch.wav')
    elf1 = Keebler()
    elf2 = Keebler()
    elf3 = Keebler()
    elves = [elf1, elf2, elf3]
    crosshairs = Aimer()
    allsprites = pygame.sprite.RenderUpdates((elf1,
elf2, elf3, crosshairs))
        
#Main Loop
    while 1:
        clock.tick(60)
	
    #Handle Input Events
        for event in pygame.event.get():
            if event.type is QUIT:
                return
            elif event.type is KEYDOWN and event.key
is K_ESCAPE:
                return
            elif event.type is MOUSEBUTTONDOWN:
                for elf in elves:
                    if crosshairs.punch(elf):
                        punch_sound.play()
                        elf.punched()
                        crosshairs.unpunch()
                                              
                                           
                    else:
                        whiff_sound.play()
            elif event.type is MOUSEBUTTONUP:
                crosshairs.unpunch()
        allsprites.update()

    #Draw Everything
        screen.blit(background, (0, 0))
        allsprites.draw(screen)
        pygame.display.flip()


__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org