1

I don't get why whenever I execute the program and my character collides with the star, it does not execute the following codes? What happens is that it executes the code already even if they are not colliding.

    class FinishStar:
        def __init__(self):
            self.x = screenWidth + 100
            self.y = 290
            self.image = Finish
            self.width = self.image.get_width()
            self.rect = self.image.get_rect()
    
        def update(self):
            self.x -= gameSpeed
            if self.x < -self.width:
                self.x = screenWidth + 2500
                self.y = 50
    
        def draw(self, screen):
            screen.blit(self.image, (self.x, self.y))

and this is the code in main():

            if scorePoints >= 100:
                star.draw(screen)
                star.update()
                if player.character_rect.colliderect(star.rect):
                    font = pygame.font.Font('Fonts/Bubblebody.ttf', 22)
                    font1 = pygame.font.Font('Fonts/Affogato-Bold.otf', 20)
                    font2 = pygame.font.Font('Fonts/Billgis - Personal Use.ttf', 38)
                    font3 = pygame.font.Font('Fonts/Affogato-Medium.otf', 31)
                    background_image = pygame.image.load(os.path.join("Images/Start", 
                    "Level1Complete.png"))
                    screen.blit(background_image, [0, 0])
                    text = font1.render("PRESS ANY KEY TO CONTINUE", True, (122, 166, 185))
                    screen.blit(text, [411, 400])
                    text = font2.render("level one completed", True, (163, 213, 176))
                    screen.blit(text, [467, 292])
                    text = font.render("Congratulations!", True, (163, 213, 176))
                    screen.blit(text, [473, 321])
                    text = font3.render("SCORE: 400/400", True, (122, 166, 185))
                    screen.blit(text, [450, 349])
                    pygame.display.update()
                    TriviaLvl1.mainTrivia()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. You have to update the position of the rect attribute:

class FinishStar:
    def __init__(self):
        self.x = screenWidth + 100
        self.y = 290
        self.image = Finish
        self.width = self.image.get_width()
        self.rect = self.image.get_rect(topleft = (self.x, self.y))

    def update(self):
        self.x -= gameSpeed
        if self.x < -self.width:
            self.x = screenWidth + 2500
            self.y = 50
        self.rect.topleft = (self.x, self.y)

    def draw(self, screen):
        screen.blit(self.image, (self.x, self.y))

Actually you don't need the x, y and width attribute at all. Use the location and size of the rect attribute. See pygame.Rect:

class FinishStar:
    def __init__(self):
        self.image = Finish
        self.rect = self.image.get_rect(topleft = (screenWidth + 100, 290))

    def update(self):
        self.rect.x -= gameSpeed
        if self.rect.x < -self.rect.width:
            self.rect.x = screenWidth + 2500
            self.rect.y = 50
    
    def draw(self, screen):
        screen.blit(self.image, self.rect)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174