0

So I'm trying to check im my bird images are touching my could images and if they are to print print('Collided1!').

My Issue is that print('Collided1!') goes off no matter what and is not checking whether the images are touching. colliderect was the solution I found online but I don't seem to know how it works because this is not working.

Do You Know how to fix this? and check whether my images are touching or not?

from random import randint
import pygame, sys
import random
import time

pygame.init()
pygame.display.set_caption('Lokaverkefni')
DISPLAYSURF = pygame.display.set_mode((1224, 724))
fpsClock = pygame.time.Clock()
FPS = 60

a = 1
b = 1
c = 15

x = 100
y = 480

start = 0
score = 0
landX = 1205
totalScore = 0

level = 'low'
directionForBird = 'none'

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)



BASICFONT = pygame.font.Font('freesansbold.ttf', 30)

background_resized = pygame.image.load('sky.jpg')
background = pygame.transform.scale(background_resized, (1224, 724))



bird1 = pygame.image.load('bird1.png')
bird1_resized = pygame.transform.scale(bird1, (170, 150))
bird1Surface = bird1_resized.get_rect()

bird2 = pygame.image.load('bird2.png')
bird2_resized = pygame.transform.scale(bird2, (170, 150))
bird2Surface = bird2_resized.get_rect()





cloudsList = ['cloud1.png', 'cloud2.png', 'cloud3.png', 'cloud4.png']

clouds = random.choice(cloudsList)
cloud = pygame.image.load(clouds)
cloud_resized = pygame.transform.scale(cloud, (352, 352))
cloudSurface = cloud_resized.get_rect()



while True:
    for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if level == 'low':
                    if (event.key == K_SPACE ):
                        directionForBird = 'up'
                        level = 'high'
                        FPS += 2
                        c += 1

                        
    if directionForBird == 'up':
        y -= 10
        if y == 10:
            directionForBird = 'down'
        
    if directionForBird == 'down':
        y += 10
        if y == 480:
            directionForBird = 'none'
            

                
    if a == 1:
        DISPLAYSURF.blit(background, (0, 0))
        DISPLAYSURF.blit(bird1_resized, (x, y))
        DISPLAYSURF.blit(cloud_resized, (landX, 300))
        b += 1
        if b == c:
            a += 1
    
    if a == 2:
        DISPLAYSURF.blit(background, (0, 0))      
        DISPLAYSURF.blit(bird2_resized, (x, y))
        DISPLAYSURF.blit(cloud_resized, (landX, 300))
        b -= 1
        if b == 1:
            a -= 1



    start += 1
    
    if start == 100:
        start -= 1
        directionForLand = 'left'

        if directionForLand == 'left':
            landX -= 15
            if landX == -550:
                landX = 1205
                level = 'low'

                clouds = random.choice(cloudsList)
                cloud = pygame.image.load(clouds)
                cloud_resized = pygame.transform.scale(cloud, (352, 352))
                
            
    score += 1
    
    if score == 30:
        score = 0
        totalScore += 1
        
    scoreText = BASICFONT.render('Stig : %s' % (totalScore), True, (BLACK))
    scoreRect = scoreText.get_rect()
    scoreRect.topleft = (1070, 10)
        
    DISPLAYSURF.blit(scoreText, scoreRect)
            
    # This is Supossed to Be what checks if the bird images
    # colide with the cloud images
    
    if bird1Surface.colliderect(cloudSurface):
        print('Collided1!')
    if bird2Surface.colliderect(cloudSurface):
        print('Collided1!')
    
    
        
    pygame.display.update()
    fpsClock.tick(FPS) 
G U I
  • 17
  • 4

1 Answers1

0

bird1Surface, bird2Surface and cloudSurface always have an upper left of (0,0), so they are always on top of each other.. You don't change the rectangles when you move the birds. You need to track the bird x,y and the cloud x,y, and construct new rectangles with the current x,y and the known width and height before you do the collision check.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30