Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame中检查我的图像是否冲突的问题_Python_Pygame - Fatal编程技术网

Python 在pygame中检查我的图像是否冲突的问题

Python 在pygame中检查我的图像是否冲突的问题,python,pygame,Python,Pygame,所以我试着检查im我的鸟图像是否与我的CAN图像接触,以及它们是否要打印print('Collided1!') 我的问题是无论发生什么,print('Collided1!')都会关闭,并且不会检查图像是否接触colliderect是我在网上找到的解决方案,但我似乎不知道它是如何工作的,因为它不起作用 你知道怎么解决这个问题吗?并检查我的图像是否接触 from random import randint import pygame, sys import random import time p

所以我试着检查im我的鸟图像是否与我的CAN图像接触,以及它们是否要打印
print('Collided1!')

我的问题是无论发生什么,
print('Collided1!')
都会关闭,并且不会检查图像是否接触colliderect是我在网上找到的解决方案,但我似乎不知道它是如何工作的,因为它不起作用

你知道怎么解决这个问题吗?并检查我的图像是否接触

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) 

bird1Surface
bird2Surface
cloudSurface
的左上角始终为(0,0),因此它们始终位于彼此的顶部。。当你移动鸟的时候,你不会改变矩形。在执行碰撞检查之前,需要跟踪鸟x、y和云x、y,并使用当前x、y和已知的宽度和高度构造新的矩形