0
votes

I am trying to detect when two of my sprites collide. The first thing I did was create a rectangle around my player (called player.img) then another around the trees that I want to detect (called background.treesrect). I set the coordinates of the players rectangle equal to the coordinates that update when the player moves by the user pressing keys but the players rectangle doesnt move. Then I used the sprite.colliderect(sprite) function to detect if they collide and it doesnt detect. Can someone show my why my player rectangle isnt updating and anything else that may be wrong?

EDIT I just figured out the collision by putting the function that draws the rectangle into the game loop instead of the player class but I ran into another weird problem. The rectangle moves faster than the player sprite for some reason and I cant figure out how to make the player sprite on top of the background and not show the player rectangle.

                            import pygame
                            import sys
                            from pygame.locals import *

                            #starts the program
                            pygame.init()

                            white = (255, 255, 255)
                            black = (0, 0, 0)
                            red = (255, 0, 0)
                            blue = (0, 0, 255)
                            green = (0, 255, 0)
                            yellow = (255, 255, 153)

                            #creates a window of 800x600
                            setDisplay = pygame.display.set_mode((800, 600))
                            pygame.display.set_caption('Menu')

                            img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png')

                            class Player(pygame.sprite.Sprite):
                                    def __init__(self):
                                            pygame.sprite.Sprite.__init__(self)
                                            self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png').convert()
                                            self.imgx = 10
                                            self.imgy = 10
                                            self.setDisplay = pygame.display.get_surface()
                                            self.x = self.imgx
                                            self.y = self.imgy
                                            self.rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(self.x, self.y, 32, 32))

                                    def draw(self):
                                            self.setDisplay.blit(self.img)

                                    def load(self, filename):
                                            self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png').convert_alpha()               



                            class Background(pygame.sprite.Sprite):
                                    def __init__(self):
                                             pygame.sprite.Sprite.__init__(self)
                                             self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\background.png').convert()
                                             self.img2 = pygame.image.load('C:\\Users\\Ben\\Documents\\trees1.png').convert()
                                             self.treesx = 0
                                             self.treesy = 70
                                             self.treesrect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255),pygame.Rect(self.treesx, self.treesy, 376, 100))
                                    def draw(self):
                                             self.setDisplay.blit(self.img)
                                             self.setDisplay.blit(self.img2)

                                    def load(self, filename):
                                             self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\background.png').convert_alpha()
                                             self.img2 = pygame.image.load('C:\\Users\\Ben\\Documents\\trees1.png').convert_alpha()


                            def detectCollision(sprite1, sprite2):
                                    if sprite1.colliderect(sprite2):
                                            print("worked")
                            player = Player()              
                            background = Background()
                            def gameLoop():



                                imgx = 10
                                imgy = 10
                                lead_x_change = 0
                                lead_y_change = 0
                                move_variable = 5

                                while True:



                                    pygame.display.flip()

                                    for event in pygame.event.get():
                                        #print (event)

                                        if event.type == QUIT:
                                            pygame.quit()
                                            sys.exit()
                                        setDisplay.blit(background.img, [0, 0])
                                        setDisplay.blit(background.img2, [0, 0])
                                        setDisplay.blit(player.img, [player.imgx, player.imgy])


                                        if player.rect.colliderect(background.treesrect):
                                                print("collided")

                                        if event.type == pygame.KEYDOWN:
                                            if event.key == pygame.K_LEFT:
                                                lead_x_change = -move_variable
                                                player.x -= 10
                                            elif event.key == pygame.K_UP:
                                                lead_y_change = -move_variable
                                            elif event.key == pygame.K_RIGHT:
                                                player.imgx += 10
                                                player.x += 10
                                            elif event.key == pygame.K_DOWN:
                                                lead_y_change = move_variable
                                                player.y += 10
                                        if event.type == pygame.KEYUP:
                                            if event.key == pygame.K_LEFT:
                                                lead_x_change = 0
                                            elif event.key == pygame.K_UP:
                                                lead_y_change = 0
                                            elif event.key == pygame.K_RIGHT:
                                                lead_x_change = 0
                                            elif event.key == pygame.K_DOWN:
                                                lead_y_change = 0
                                        print(player.x, player.y)

                                        player.imgx += lead_x_change
                                        player.imgy += lead_y_change
                                    pygame.display.flip()
                                    pygame.display.update()

                            gameLoop()

                            #start (0, 71)
                            #length (376, 71)
                            #width (0, 168)
1

1 Answers

1
votes

I think this may be due to the fact that, in the class Player, self.rect isn't right. Instead try:

self.rect = self.img.get_rect()

also, in your main loop, why are you blit-ing stuff in the event for loop?
Only put the key presses in for event in pygame.event.get()
There are other things that are very wrong in the code.
May I recommend this excellent tutorial for making games with sprites in pygame.