Search code examples
pythonpygamegame-physics

PyGame Sprite conflicting collisions


So my sprites in pygame all have the same collision effects even thou i separated them into different defs do you know how i could fix this ?

def render(self,collisionrock):

    if (collisionrock==True):
        pygame.draw.rect(window,red,(150,150,100,100))   #for some reason this wont work
        window.blit(self.i1, (self.x,self.y))

def render(self,collisionguy):

    if (collisionguy==True):
        font = pygame.font.Font(None, 50)
        text = font.render("YOU WIN", 1, (10, 10, 10))   #they would all apply this line
        textpos = text.get_rect()                        #of code
        textpos.centerx = window.get_rect().centerx
        window.blit(text, textpos)

        window.blit(self.i1, (self.x,self.y))
    else:
        window.blit(self.i1, (self.x,self.y))

Solution

  • It seems that you have two methods with the same name "render" in your sprite class.

    In that case Python'll ignore the first method (that has collisionrock argument) and use only the second definition. That seems to be the case.

    You can rename first "render" to "render_with_rock_collision", second "render" to "render_with_guy_collision".

    And one other thing. Instead of

    if (collisionguy==True):
    

    you can write:

    if collisionguy:
    

    Makes Guido Van Rossum happy.