【问题标题】:How can I detect if my two images are touching in Pygame? [duplicate]如何检测我的两个图像是否在 Pygame 中接触? [复制]
【发布时间】:2019-07-22 19:39:36
【问题描述】:

我正在制作一个游戏,其中 ASL 手势掉下来,你需要在桶里接住它们。当标志接触桶时,我希望它打印出他们正在接触。如果他们不是,我希望它打印出他们没有接触。如何检测图像是否接触?我已经有一些矩形碰撞代码,但它只是不断地打印出它们正在触摸其中一个标志,即使它们不是。提前感谢您能给我的任何帮助!附言很抱歉把我所有的代码都放进去,但我不确定需要看什么来帮助解决我的问题。

我查看了许多涉及图像碰撞的问题和答案,但我似乎无法在我的代码中使用它。我“突出显示”了我认为导致问题的代码。

import random
import pygame
pygame.init()
pygame.mixer.init()
#define functions
def newPlacement():
    screen.blit(choiceASL, (x,y))
    screen.blit(choiceASL2, (x2,y2))
    screen.blit(choiceASL3, (x3,y3))
def bucketStuff():
    screen.blit(bucketPic, (bucketX, bucketY))
'''
def ifCollided():
    if bucketRect.colliderect(choiceASLRect):
        print("You collided with choice1!")
    elif bucketRect.colliderect(choiceASL2Rect):
        print("You collided with choice2!")
    elif bucketRect.colliderect(choiceASL3Rect):
        print("You collided with choice3!")
    else:
        print("Not touching.")
'''

#define stuff below: var, funct, def, etc.
aslABCSList = ("AslA.png", "AslB.png", "AslC.png", "AslD.png")
randHandsign = random.choice(aslABCSList)
randHandsign2 = random.choice(aslABCSList)
randHandsign3 = random.choice(aslABCSList)
choiceASL = pygame.image.load(randHandsign)
choiceASLRect = choiceASL.get_rect()
choiceASL2 = pygame.image.load(randHandsign2)
choiceASL2Rect = choiceASL2.get_rect()
choiceASL3 = pygame.image.load(randHandsign3)
choiceASL3Rect = choiceASL3.get_rect()
bucketPic = pygame.image.load("bucketPic.png")
bucketRect = bucketPic.get_rect()

screen = pygame.display.set_mode((700, 600))
Black = [0, 0, 0]
White = [255, 255, 255]
y = 0
y2 = 0
y3 = 0
x = int(random.randrange(1,650,25))
x2 = int(random.randrange(1,650,25))
x3 = int(random.randrange(1,650,50))
bucketX = 300
bucketY = 540

#to make the main work
done = False
#allows fps rate
clock = pygame.time.Clock()

#basically main, put anything that you want to run consistently until the user exits below.
while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
        #vvv Put all main code here! vvv
        screen.fill(White)
        bucketStuff()
        newPlacement()
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_LEFT]: bucketX -= 3
        if pressed[pygame.K_RIGHT]: bucketX += 3
        ifCollided()

        y+=1
        y2+=2
        y3+=3



        #below is basically a refresh, so that everything is updated. Put code before this!
        pygame.display.flip()
        #60 fps
        clock.tick(50)

【问题讨论】:

    标签: python image pygame collision-detection


    【解决方案1】:

    pygame.Surface 对象的.get_rect() 返回一个与表面大小相同但位于 (0, 0) 位置的矩形。
    您必须设置图像的位置 (pygame.Rect):

    例如:

    choiceASL = pygame.image.load(randHandsign)
    choiceASLRect = choiceASL.get_rect()
    
    # [...]
    
    bucketPic = pygame.image.load("bucketPic.png")
    bucketRect = bucketPic.get_rect()
    
    # [...]
    
    while not done:
    
        # [...]
    
        if pressed[pygame.K_LEFT]: bucketX -= 3
        if pressed[pygame.K_RIGHT]: bucketX += 3
    
        bucketRect.topleft = (bucketX, bucketY)
        choiceASLRect.topleft = (x, y)
        # [...]
    
        ifCollided()
    

    一旦矩形的位置设置正确,碰撞测试就会按预期进行。

    【讨论】:

    • 您好,感谢您的帮助!我根据您的回答相应地更改了代码,但现在它一直在说它们即使在接触时也没有接触。知道为什么吗?
    • @MoniCoo choiceASLRect.topleft = (x, y) 必须在 ifCollided() 之前完成。我已经改变了答案。当然choiceASL2RectchoiceASL3Rect 也必须更新。
    猜你喜欢
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2014-03-09
    • 2018-10-26
    • 2016-04-24
    • 2013-01-17
    相关资源
    最近更新 更多