1

I have a function which checks for collision

def collision_check1 (pboard_rect):
    if pboard_rect.colliderect(up_border_rect):
        pboard_rect = pboard.get_rect(topleft = (695,20)
        print("hi")
if game_active:
   collision_check1(pboard_rect)

Here it prints 'hi' but it doesn't move my pboard_rect what to do

edit:

pboard_x = 695
pboard_y = 200



pboard = pygame.image.load("data/pboard.png")
pboard_rect = pboard.get_rect(topleft = (pboard_x,pboard_y))


screen.blit(pboard,pboard_rect) 
6
  • your call to get_rect is incomplete. you assign pboard_rect inside the function, it is a different variable compared to argument used in collision_check1(pboard_rect). Do not do side effects in a function. This function checks and should return a True/False Commented Oct 29, 2020 at 12:08
  • What do I do then to change the postion of my object if it collides up_border Commented Oct 29, 2020 at 12:17
  • I havent assigned the pboard_rect in the function I mean i have assigned it earlier then to change its position i didnt know what to do so i re assigned it Commented Oct 29, 2020 at 12:18
  • the function argument is a different variable with the SAME name, at the start it contains the same handle, not anymore after the pboard_rect = pboard.get_rect(), Read the Python docs on variable scope Commented Oct 29, 2020 at 12:22
  • Ok then what do I do to change the location of my object if it collides with up border Commented Oct 29, 2020 at 13:34

1 Answer 1

1
def collision_check1 (pboard_rect):
    if pboard_rect.colliderect(up_border_rect):
        print("hi")
        return pboard.get_rect(topleft = (695,20))

if game_active:
   pboard_rect = collision_check1(pboard_rect)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for helping me and finding the error in my code
@Rice If this solves your question you can mark this answer as solution
I put this in my code and it generated a error : invali destination to blit (pboard,pboard_rect)
@Rice I can't see more code than you posted, your code was incomplete, you had an update of pboard_rect problem, caused by variable scope (never use global variables if possible), Examine the rect and find out what its content is and why it is illegal to blit
was the edit helpful or should I add some more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.