最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文詳解pygame.sprite的精靈碰撞

 更新時(shí)間:2023年01月24日 10:27:56   作者:peanutfish  
精靈其實(shí)在一個(gè)游戲程序中,精靈本質(zhì)指的是一張張小尺寸的圖片,比如游戲中的各種道具、人物、場景裝飾等,它們都可以看做成一張張小的“精靈”圖,下面這篇文章主要給大家介紹了關(guān)于pygame.sprite精靈碰撞的相關(guān)資料,需要的朋友可以參考下

前言

pygame中的精靈碰撞是可見游戲中用的最基礎(chǔ)的東西,這里結(jié)合官方文檔和小甲魚的網(wǎng)站上的內(nèi)容做個(gè)小總結(jié),方便日后使用。

pygame.sprite.Sprite - 可見游戲?qū)ο蟮暮唵位悺?/h2>

Sprite(*groups) -> Sprite

  • pygame.sprite.Sprite.add - 將精靈添加到組中
  • pygame.sprite.Sprite.remove - 從組中刪除精靈
  • pygame.sprite.Sprite.kill - 從所有組中刪除Sprite
  • pygame.sprite.Sprite.alive - 精靈屬于任何組的檢測
  • pygame.sprite.Sprite.groups - 包含此Sprite的組列表
  • pygame.sprite.Sprite.update - 控制精靈行為的方法

pygame.sprite.Group - 用于保存和管理多個(gè) Sprite 對象的容器類。

Group(*sprites) -> Group

  • pygame.sprite.Group.sprites - 此組包含的 Sprite 列表
  • pygame.sprite.Group.copy - 復(fù)制組
  • pygame.sprite.Group.add - 將 Sprite 添加到此組
  • pygame.sprite.Group.remove - 從組中刪除 Sprite
  • pygame.sprite.Group.has - 測試一個(gè) Group 是否包含 Sprite
  • pygame.sprite.Group.update - 在包含的 Sprite 上調(diào)用 update 方法
  • pygame.sprite.Group.draw - blit Sprite 的圖像
  • pygame.sprite.Group.clear - 在 Sprites 上繪制背景
  • pygame.sprite.Group.empty - 刪除所有 Sprite`

上面兩個(gè)基類是pygame中最常用,相當(dāng)輕量級,只為大多數(shù)游戲常見的代碼提供了一個(gè)起始點(diǎn)。

Sprite 類旨在用作游戲中不同類型對象的基類,為我們碰撞檢測做準(zhǔn)備。還有一個(gè)基本的 Group 類,它只存儲(chǔ) sprite 對象, 這樣方便不同類型的精靈進(jìn)行碰撞檢測, 通常的操作 in / len / bool / iter 都對這個(gè)group使用。

in      test if a Sprite is contained
len     the number of Sprites contained
bool    test if any Sprites are contained
iter    iterate through all the Sprites

pygame.sprite.spritecollide() - 在與另一個(gè)精靈相交的組中查找精靈

spritecollide(sprite, group, dokill, collided = None) -> Sprite_list

  • 返回一個(gè)sprite列表,其中包含與另一個(gè) Sprite 相交的 Group 中的所有 Sprite 。 通過比較每個(gè) Sprite 的 Sprite.rect 屬性來確定交集。
  • dokill 參數(shù)是一個(gè)布爾值。如果設(shè)置為 True,則將從組中刪除所有碰撞的 Sprite 。
  • collided 碰撞參數(shù)是一個(gè)回調(diào)函數(shù),用于計(jì)算兩個(gè)精靈是否發(fā)生碰撞。 它應(yīng)該將兩個(gè)精靈作為值,并返回一個(gè) bool 值,指示它們是否發(fā)生碰撞。 如果未傳遞碰撞,則所有精靈必須具有 rect 值,該值是 sprite 區(qū)域的矩形,將用于計(jì)算碰撞。

可用的回調(diào)函數(shù)

collide_rect, collide_rect_ratio, collide_circle,
collide_circle_ratio, collide_mask
  • pygame.sprite.collide_rect - 使用 rects 檢測兩個(gè)精靈之間的碰撞。
    • Collision detection between two sprites, using rects. 使用函數(shù)pygame rect colliderect檢測碰撞并將結(jié)果返回給*collide, 精靈必須具有 ‘rect’ 屬性。

collide_rect(left, right) -> bool

  • pygame.sprite.collide_rect_ratio - 使用按比例縮放的 rects 檢測兩個(gè)精靈之間的碰撞。
    • Collision detection between two sprites, using rects scaled to a ratio. 使用 ratio 創(chuàng)建,然后將實(shí)例作為碰撞回調(diào)函數(shù)傳遞給 *collide 函數(shù)。
      ratio 是浮點(diǎn)數(shù) - 1.0 是相同的大小,2.0 是兩倍大,0.5 是大小的一半。

collide_rect_ratio(ratio) -> collided_callable

  • pygame.sprite.collide_circle - 使用圓圈檢測兩個(gè)精靈之間的碰撞。
    • *Collision detection between two sprites, using circles.*測試兩個(gè)精靈之間的碰撞,通過測試以查看精靈中心的兩個(gè)圓是否重疊。

如果精靈具有 radius(半徑) 屬性,用于創(chuàng)建圓,否則會(huì)創(chuàng)建一個(gè)足夠大的圓,以完全包圍由 rect 屬性給出的精靈矩形。作為碰撞回調(diào)函數(shù)傳遞給 *collide 函數(shù)。精靈必須具有 rect 和可選的 radius 屬性

請?zhí)砑訄D片描述

collide_circle(left, right) -> bool

  • pygame.sprite.collide_circle_ratio - 使用按比例縮放的圓圈檢測兩個(gè)精靈之間的碰撞。
    • Collision detection between two sprites, using circles scaled to a ratio. 使用浮點(diǎn)數(shù) ratio 創(chuàng)建,然后將實(shí)例作為碰撞回調(diào)函數(shù)傳遞給 *collide 函數(shù)。ratio 是浮點(diǎn)數(shù) - 1.0 是相同的大小,2.0 是兩倍大,0.5 是大小的一半。

兩個(gè)精靈之間的碰撞創(chuàng)建的可調(diào)用測試,通過測試以查看以精靈為中心的兩個(gè)圓是否重疊,在通過存儲(chǔ)的比例縮放圓半徑之后。如果精靈具有 radius 半徑屬性,用于創(chuàng)建圓,否則會(huì)創(chuàng)建一個(gè)足夠大的圓,以完全包圍由 rect 屬性給出的精靈矩形。打算作為碰撞回調(diào)函數(shù)傳遞給 *collide 函數(shù)。

精靈必須具有 rect 和可選的 radius 屬性

collide_circle_ratio(ratio) -> collided_callable

  • pygame.sprite.collide_mask - 使用蒙版在兩個(gè)精靈之間進(jìn)行碰撞檢測。
    • *Collision detection between two sprites, using masks. *返回 masks 碰撞的 mask 上的第一個(gè)點(diǎn),如果沒有碰撞,則返回 None 。

通過測試它們的 bitmasks( pygame.mask.Mask.overlap()) 是否重疊來測試兩個(gè)精靈之間的碰撞。 如果精靈具有 mask 屬性,該屬性用作 mask,否則將從精靈圖像創(chuàng)建 mask 。 作為碰撞回調(diào)函數(shù)傳遞給 *collide 函數(shù)。

精靈必須具有 rect 和可選的 mask 屬性

如果要多次檢查碰撞,應(yīng)該考慮在加載時(shí)為精靈創(chuàng)建一個(gè)mask。這將提高性能,否則這可能是一個(gè)昂貴的功能,因?yàn)樗鼤?huì)在每次檢查碰撞時(shí)創(chuàng)建 mask 。

# Example of mask creation for a sprite.
sprite.mask = pygame.mask.from_surface(sprite.image)

collide_mask(sprite1, sprite2) -> (int, int)

collide_mask(sprite1, sprite2) -> None

pygame.sprite.groupcollide - 查找在兩個(gè)組之間發(fā)生碰撞的所有精靈。

  • Find all sprites that collide between two groups.
  • This will find collisions between all the Sprites in two groups. Collision is determined by comparing the Sprite.rect attribute of each Sprite or by using the collided function if it is not None.
    Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect. 像這個(gè)格式{group1_sprite: group2_sprite}
  • 如果*collide沒有指定回調(diào)函數(shù),則所有的sprite需要有rect屬性

groupcollide(group1, group2, dokill1, dokill2, collided = None) -> Sprite_dict

EG:
class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
       self.image = pygame.Surface([width, height])
       self.image.fill(color)

       # Fetch the rectangle object that has the dimensions of the image
       # Update the position of this object by setting the values of rect.x and rect.y
       # give radius and mask attribute
       self.rect = self.image.get_rect()
       self.radius = self.rect.width / 2
       self.mask = pygame.image.from_surface(self.image)


class File(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
       self.image = pygame.Surface([width, height])
       self.image.fill(color)

       # Fetch the rectangle object that has the dimensions of the image
       # Update the position of this object by setting the values of rect.x and rect.y
       self.rect = self.image.get_rect()
       self.radius = self.rect.width / 2
       self.mask = pygame.image.from_surface(self.image)
    
block_group = pygame.sprite.Group()
for i in range(5):
    block = Block(color, width, height)
    block_group.add(block)
    
    # there is another sprite group called file_group, which have same setting as block_group.
file_group = pygame.sprite.Group()
for i in range(5):
    file = File(color, width, height)
    file_group.add(file)
    
    # the collide check will like:
    hit_list1 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_rect
    hit_list2 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_rect_ratio(.75))
    hit_list3 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_circle
    hit_list4 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_circle_ratio(.25))
    hit_list5 = pygame.sprite.spritecollide(block, file_group, False, pygame.sprite.collide_mask
    # select the collided function whatever you like
    hit_list6 = pygame.sprite.spritecollide(block_group, file_group, False, False, pygame.sprite.collide_XXX)

總結(jié)

到此這篇關(guān)于pygame.sprite精靈碰撞的文章就介紹到這了,更多相關(guān)pygame.sprite精靈碰撞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件

    詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件

    這篇文章主要介紹了詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • python在指定目錄下查找gif文件的方法

    python在指定目錄下查找gif文件的方法

    這篇文章主要介紹了python在指定目錄下查找gif文件的方法,涉及Python操作文件的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • window環(huán)境pip切換國內(nèi)源(pip安裝異常緩慢的問題)

    window環(huán)境pip切換國內(nèi)源(pip安裝異常緩慢的問題)

    這篇文章主要介紹了window環(huán)境pip切換國內(nèi)源(pip安裝異常緩慢的問題),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 詳解Python中神奇的字符串駐留機(jī)制

    詳解Python中神奇的字符串駐留機(jī)制

    字符串駐留機(jī)制是Python針對字符串對象采取的一種內(nèi)存優(yōu)化技術(shù)。其目標(biāo)是減少內(nèi)存使用并提高程序的性能。這篇文章主要介紹了字符串駐留機(jī)制的簡單應(yīng)用,需要的可以參考一下
    2023-04-04
  • python按照行來讀取txt文件全部內(nèi)容(去除空行處理掉\t,\n后以列表方式返回)

    python按照行來讀取txt文件全部內(nèi)容(去除空行處理掉\t,\n后以列表方式返回)

    這篇文章主要介紹了python按照行來讀取txt文件全部內(nèi)容 ,去除空行,處理掉\t,\n后,以列表方式返回,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • 對python 通過ssh訪問數(shù)據(jù)庫的實(shí)例詳解

    對python 通過ssh訪問數(shù)據(jù)庫的實(shí)例詳解

    今天小編就為大家分享一篇對python 通過ssh訪問數(shù)據(jù)庫的實(shí)例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Numpy之布爾索引的實(shí)現(xiàn)

    Numpy之布爾索引的實(shí)現(xiàn)

    本文主要介紹了Numpy之布爾索引的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • python中extend函數(shù)舉例詳解以及對比

    python中extend函數(shù)舉例詳解以及對比

    Python中的extend函數(shù)是用于將一個(gè)列表的元素添加到另一個(gè)列表中,它會(huì)將第一個(gè)列表中的元素逐個(gè)添加到第二個(gè)列表的末尾,這篇文章主要給大家介紹了關(guān)于python中extend函數(shù)舉例詳解以及對比的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作

    python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作

    這篇文章主要介紹了python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 詳解Python函數(shù)作用域的LEGB順序

    詳解Python函數(shù)作用域的LEGB順序

    這篇文章主要為大家詳細(xì)介紹了Python函數(shù)作用域的LEGB順序的相關(guān)資料,感興趣的朋友可以參考一下
    2016-05-05

最新評論

肥乡县| 荃湾区| 元谋县| 疏勒县| 临沧市| 漳州市| 南靖县| 杂多县| 富宁县| 阿拉善右旗| 万安县| 林甸县| 巴塘县| 五原县| 晋江市| 鄂温| 吉水县| 长汀县| 武威市| 景宁| 苏州市| 菏泽市| 伊吾县| 云安县| 家居| 高阳县| 华亭县| 平阴县| 襄城县| 铜川市| 杨浦区| 溧阳市| 四川省| 临澧县| 金乡县| 沅江市| 合肥市| 阳城县| 青河县| 大洼县| 淮安市|