1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power

    Colliderect() not working?

    Hi all!, I am trying to add collision detection in my game and I am having a lot of trouble

    I have a sprite called player which has a player.rect attribute
    and I have a sprite called white_rect also with a rect attribute.

    I have tried different things like...

    if player.rect.colliderect(white_rect.rect):
    if pygame.sprite.collide_rect(player, white_rect):
    and quite a few of the main ways people detect collision but everytime I seem to just either get collision all the time or no collision even when the sprites are on top of each other...

    am I doing something wrong?
    thanks in advance

    ps I made another thread about this a about a week ago but no answers came up, is this ok?

    ok if you want here is my code, I have deleted a few of the functions not related to this so its not to long
    import pygame, sys
    def main():
        class rectangle(pygame.sprite.Sprite):
            def __init__(self, image_file, location):
                self.image = pygame.image.load(image_file)
                self.rect = self.image.get_rect()
                self.location = location
                self.currentX = location[0]
                self.currentY = location[1]
        class player(pygame.sprite.Sprite):
            def __init__(self, location, speed, image_file):
                self.image = pygame.image.load(image_file)
                self.speed = speed
                self.start_location = location
                self.currentX = location[0]
                self.currentY = location[1]
                self.keydownA = False
                self.keydownD = False
                self.keydownSPACE = False
                self.jumping = False
                self.jumpY = location[1]
                self.jump_direction = 1 # 1 = going up, 0 = going down
                self.jumpspeed = 20
                self.rect = self.image.get_rect()
            def detectMoving(self):
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_a:
                            self.keydownA = True
                        if event.key == pygame.K_d:
                            self.keydownD = True
                        if event.key == pygame.K_SPACE:
                            self.keydownSPACE = True
                    if event.type == pygame.KEYUP:
                        if event.key == pygame.K_a:
                            self.keydownA = False
                        if event.key == pygame.K_d:
                            self.keydownD = False
                        if event.key == pygame.K_SPACE:
                            self.keydownSPACE = False
            def move(self):
                if self.keydownA == True:
                    self.currentX -= self.speed
                if self.keydownD == True:
                    self.currentX += self.speed
            def jump(self):
                     if self.jumping == False:
                        self.jumpY = self.currentY
                     if self.keydownSPACE == True:
                        self.jumping = True
                     if self.jumping == True:
                            self.currentY -= self.jumpspeed
                            self.jumpspeed -= 1
                     if self.currentY == self.jumpY:
                        self.jumping = False
                        self.jumpspeed = 20
        running = True
        WHITE = (255, 255, 255)
        BLACK = (0, 0, 0)
        white_rect_sprite = ("rectangle_white.png")
        player_sprite = ("player_sprite.png")
        white_rect = rectangle(white_rect_sprite, (400, 480 - 130))
        player = player([250, 480 - 145], 3, player_sprite)
        screen = pygame.display.set_mode((640, 480), 0, 32)
        pygame.display.set_caption("My Platformer")
        while running:
            screen.blit(white_rect.image, (white_rect.currentX, white_rect.currentY))
            screen.blit(player.image, (player.currentX, player.currentY))
    #---------collision detection----- 
            if player.rect.colliderect(white_rect):
    if you delete the collision detection line(marked with a comment) then it will play as it should without a black screen
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    Ask in a dedicated pygame forum.
    Asking here is reasonable, and some questions involving modules outside the standard python distribution are answered well. Your question isn't working, try a different approach.

    Before you abandon us, please consider asking a reasonable question. Provide a complete (simple please) program that exhibits the problem. I have installed pygame. I've never written code for it.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo