Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 6th, 2013, 03:23 PM
slidon slidon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 slidon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 20 sec
Reputation Power: 0
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...

Code:
if player.rect.colliderect(white_rect.rect):
    screen.fill(BLACK)

and
Code:
if pygame.sprite.collide_rect(player, white_rect):
    screen.fill(BLACK)

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?

EDIT:
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
Code:
import pygame, sys
pygame.init()
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:
                    sys.exit()
                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:
        player.detectMoving()
        player.move()
        player.jump()
        screen.fill(BLACK)
        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):
            screen.fill(BLACK)
        pygame.display.flip()
main()

if you delete the collision detection line(marked with a comment) then it will play as it should without a black screen

Reply With Quote
  #2  
Old February 6th, 2013, 03:48 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Colliderect() not working?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap