pygame中的碰撞檢測


 

1.兩個精靈之間的矩形檢測

import pygame
import random

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

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()

pygame.init()
b=pygame.mouse.set_visible(False)
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
sprite_list = pygame.sprite.Group()
block = Block(BLACK, 50, 50)
block.rect.x = random.randint(50,screen_width-50)
block.rect.y = random.randint(50,screen_height-50)
player = Block(RED, 50, 50)
sprite_list.add(block)
sprite_list.add(player)

done = False
clock = pygame.time.Clock()
i=0

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill(WHITE)
    pos = pygame.mouse.get_pos()
    player.rect.x = pos[0]
    player.rect.y = pos[1]

    #b = pygame.sprite.collide_rect(block, player)  #兩個精靈之間的矩形碰撞檢測
    #如果碰撞返回True

    b = pygame.sprite.collide_rect_ratio(0.9)(block, player)  #兩個精靈之間的矩形碰撞檢測
    #兩個精靈的矩形面積相互重疊達到10%就返回True
    if b:
        print(i,'兩個精靈碰撞了')
    else:
        print(i,'兩個精靈沒有碰撞')

    sprite_list.draw(screen)
    pygame.display.flip()
    i+=1
    clock.tick(2)

pygame.quit()

 

2.兩個精靈之間的圓檢測

import pygame
import random

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

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()

pygame.init()
b=pygame.mouse.set_visible(False)
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
sprite_list = pygame.sprite.Group()
block = Block(BLACK, 50, 50)
block.rect.x = random.randint(50,screen_width-50)
block.rect.y = random.randint(50,screen_height-50)
player = Block(RED, 50, 50)
sprite_list.add(block)
sprite_list.add(player)

done = False
clock = pygame.time.Clock()
i=0

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill(WHITE)
    pos = pygame.mouse.get_pos()
    player.rect.x = pos[0]
    player.rect.y = pos[1]

    #b = pygame.sprite.collide_circle(block, player)  #兩個精靈之間的圓形碰撞檢測
    #【以矩形的對角線為直徑形成檢測圓,相碰就返回真】

    b = pygame.sprite.collide_rect_ratio(0.9)(block, player)  #兩個精靈之間的圓形碰撞檢測
    # 【以矩形的對角線為直徑形成檢測圓,相碰就返回真】
    # 兩個精靈的圓形面積相互重疊達到10%就返回True
    if b:
        print(i,'兩個精靈碰撞了')
    else:
        print(i,'兩個精靈沒有碰撞')

    sprite_list.draw(screen)
    pygame.display.flip()
    i+=1
    clock.tick(2)

pygame.quit()

 

3.兩個精靈的精准碰撞檢測

import pygame,os
pygame.init()

os.environ['SDL_VIDEO_WINDOW_POS']="%d,%d"%(1400,100)  #窗口自定義坐標
screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("碰撞")
clock = pygame.time.Clock()
i=0

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height]).convert_alpha()
        self.image.set_colorkey((0, 0, 0))  # 設置透明色
        pygame.draw.rect(self.image, color, (10,10,width//2,height//2))
        self.rect = self.image.get_rect()
        self.mask= pygame.mask.from_surface(self.image)  #給精靈添加一個mask屬性


zu = pygame.sprite.Group()

b1=Block((0,0,255),50,50)
b1.rect.x=10
b1.rect.y=200
zu.add(b1)

p1=Block((255,0,0),50,50)
p1.rect.x=20
p1.rect.y=50
zu.add(p1)



while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_DOWN:
            p1.rect.y +=10

        if event.key == pygame.K_UP:
            p1.rect.y -= 10

    b = pygame.sprite.collide_mask(p1,b1)  #兩個精靈的mask碰撞檢測--精准檢測
    #參數:兩個精靈
    #返回值:碰撞區域的左上角坐標
    #
    print(i,b)
    screen.fill((255,255,255))
    zu.draw(screen)

    i+=1
    clock.tick(4)
    pygame.display.update()

 

 

 

4.精靈和組之間的沖突檢測 

import pygame
pygame.init()

screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("碰撞")
clock = pygame.time.Clock()
i=0

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height]).convert_alpha()
        self.image.set_colorkey((0, 0, 0))  # 設置透明色
        pygame.draw.rect(self.image, color, (0,0,width//2,height//2))
        self.rect = self.image.get_rect()
        self.mask= pygame.mask.from_surface(self.image) #給精靈添加一個mask屬性


zu = pygame.sprite.Group()
zu1=pygame.sprite.Group()

b1=Block((0,0,255),50,50)
b1.rect.x=10
b1.rect.y=10
zu.add(b1)
b2=Block((0,255,0),50,50)
b2.rect.x=200
b2.rect.y=200
zu.add(b2)
b3=Block((255,255,0),50,50)
b3.rect.x=220
b3.rect.y=190
zu.add(b3)


p=Block((255,0,0),50,50)
p.rect.x=200
p.rect.y=200
zu1.add(p)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill((255,255,255))
    pos = pygame.mouse.get_pos()
    p.rect.x = pos[0]
    p.rect.y = pos[1]

    #b = pygame.sprite.spritecollide(p, zu,False) #精靈和組之間的矩形碰撞檢測
    #參數1就是單個精靈
    #參數2是精靈組
    #第三個參數是一個bool值。當為True的時候,碰撞后會刪除組中所有沖突的精靈(把精靈踢出組),False的時候不會刪除沖突的精靈
    #返回值:列表  返回組中被碰撞的精靈
    #[<Block sprite(in 1 groups)>, <Block sprite(in 1 groups)>]


    #b = pygame.sprite.spritecollideany(p, zu, False) #精靈和組之間的矩形碰撞檢測
    # 參數1就是單個精靈
    # 參數2是精靈組
    # 參數3是一個bool值。當為True的時候,碰撞后會刪除組中所有沖突的精靈(把精靈踢出組),False的時候不會刪除沖突的精靈
    # 返回值:布爾值  是否有碰撞
 b = pygame.sprite.spritecollide(p, zu, False,pygame.sprite.collide_mask) #精靈和組之間精准碰撞檢測
    ##參數1就是單個精靈
    #參數2是精靈組
    #參數3:是一個bool值。當為True的時候,碰撞后會刪除組中所有沖突的精靈(把精靈踢出組),False的時候不會刪除沖突的精靈
    #參數4:用精靈的mask進行碰撞檢測(透明區域不在碰撞檢測范圍內)--必須給精靈添加mask屬性
    #返回值:列表  返回組中被碰撞的精靈
    #[<Block sprite(in 1 groups)>, <Block sprite(in 1 groups)>]


    print(i, b)

    if b:
        print(i,'兩個精靈碰撞了')
    else:
        print(i,'兩個精靈沒有碰撞')


    zu.draw(screen)
    zu1.draw(screen)

    i+=1
    clock.tick(2)
    pygame.display.update()

 

5.精靈組之間的沖突檢測 

import pygame
pygame.init()

screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("碰撞")
clock = pygame.time.Clock()
i=0

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height]).convert_alpha()
        self.image.set_colorkey((0, 0, 0))  # 設置透明色
        pygame.draw.rect(self.image, color, (10,10,width//2,height//2))
        self.rect = self.image.get_rect()
        self.mask= pygame.mask.from_surface(self.image)  #給精靈添加一個mask屬性


zu = pygame.sprite.Group()
zu1=pygame.sprite.Group()

b1=Block((0,0,255),50,50)
b1.rect.x=10
b1.rect.y=200
zu.add(b1)
b2=Block((0,255,0),50,50)
b2.rect.x=200
b2.rect.y=200
zu.add(b2)
b3=Block((255,255,0),50,50)
b3.rect.x=220
b3.rect.y=190
zu.add(b3)


p1=Block((255,0,0),50,50)
p1.rect.x=20
p1.rect.y=50
zu1.add(p1)

p2=Block((255,0,0),50,50)
p2.rect.x=210
p2.rect.y=20
zu1.add(p2)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_DOWN:
            p1.rect.y +=10
            p2.rect.y += 10
        if event.key == pygame.K_UP:
            p1.rect.y -= 10
            p2.rect.y -= 10

    #b=pygame.sprite.groupcollide(zu1, zu, False, False)   #精靈組之間的矩形沖突檢測
    #參數1:組1
    #參數2:組2
    #參數3:發生碰撞時,組1中碰撞的精靈是否踢出組
    #參數4:發生碰撞時,組2中碰撞的精靈是否踢出組
    #返回值-字典:鍵是組1中碰撞的精靈,值是組1中的碰撞精靈與組2中發生碰撞的精靈列表
    #{<Block sprite(in 1 groups)>: [<Block sprite(in 1 groups)>], <Block sprite(in 1 groups)>: [<Block sprite(in 1 groups)>, <Block sprite(in 1 groups)>]}

    b = pygame.sprite.groupcollide(zu1, zu, False, False,pygame.sprite.collide_mask)  #精靈組之間的精准碰撞檢測
    # 參數1:組1
    # 參數2:組2
    # 參數3:發生碰撞時,組1中碰撞的精靈是否踢出組
    # 參數4:發生碰撞時,組2中碰撞的精靈是否踢出組
    #參數5:用精靈的mask進行碰撞檢測(透明區域不在碰撞檢測范圍內)--必須給精靈添加mask屬性
    # 返回值-字典:鍵是組1中碰撞的精靈,值是組1中的碰撞精靈與組2中發生碰撞的精靈列表
    # {<Block sprite(in 1 groups)>: [<Block sprite(in 1 groups)>], <Block sprite(in 1 groups)>: [<Block sprite(in 1 groups)>, <Block sprite(in 1 groups)>]}

    print(i,b)
    screen.fill((255,255,255))
    zu.draw(screen)
    zu1.draw(screen)

    i+=1
    clock.tick(4)
    pygame.display.update()

 

6.兩個圖像的精准檢測

看:https://www.cnblogs.com/liming19680104/p/13272989.html    實例2

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM