pygame中的碰撞检测不能正常工作

时间:2014-09-13 00:30:06

标签: python pygame collision-detection

我有一段代码应该在屏幕上放置1000个方块,没有重叠。

但是当我运行它时,我只得到一堆正方形,其中一些正在重叠,任何想法我哪里出错?

import time, pygame, ctypes, random

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()

white = (255,255,255)
black = (0, 0, 0)
user32 = ctypes.windll.user32
sw = user32.GetSystemMetrics(0)
sh = user32.GetSystemMetrics(1)

Bloqs = {}
#Bloq atributes: Position (upper left), Size, Color, Genetics

pygame.init()
all_sprites_list = pygame.sprite.Group()

def Collision(bloq):
    global all_sprites_list
    hit = pygame.sprite.spritecollide(bloq, all_sprites_list, True)
    if len(hit) == 0:
        return False
    if len(hit) > 0:
        return True

def InitializeSim():
    global Bloqs
    global white
    global black
    global sw
    global sh
    global all_sprites_list

    size = sw, sh
    screen = pygame.display.set_mode(size, pygame.FULLSCREEN )
    screen.fill(white)
    count = 0
    while count < 1000:
        size = 10
        pos = random.seed()
        posx = random.randint(0, sw-size)
        posy = random.randint(0, sh-size)
        #pygame.draw.rect(screen, color, (x,y,width,height), thickness)
        CurrentBloq = "bloq" + str(count)
        Bloqs[CurrentBloq]={}
        Bloqs[CurrentBloq]["position"] = (sw, sh)
        bloq = Block(black, size, size)
        bloq.rect.x = posx
        bloq.rect.y = posy
        Bloqs[CurrentBloq]["sprite"] = bloq
        all_sprites_list.add(bloq)
        all_sprites_list.draw(screen)
        pygame.display.update()
        while Collision(bloq) == True:
            all_sprites_list.remove(bloq)
            posx = random.randint(0, sw-size)
            posy = random.randint(0, sh-size) 
            bloq.rect.x = posx
            bloq.rect.y = posy
            all_sprites_list.add(bloq)
            all_sprites_list.draw(screen)
            pygame.display.update()
        count += 1

InitializeSim()
time.sleep(5)
pygame.quit()

2 个答案:

答案 0 :(得分:1)

您的代码对于您要执行的操作似乎相当复杂......

作为一个整体接近它然而对你来说感觉很自然,但是当生成块时,我会遵循以下内容:

for i in range(numberofblocksyouwant):

    while true:
        create a block at a random x,y pos
        if block collides with any existing blocks:
            remove the block you just created
        else: 
            break

对于你想要的每个块,这种方法将继续重建每个块,直到它选择一个不会与任何其他块碰撞的随机x,y。

但是,如果您的块填满了屏幕并且无法做到这一点,嵌套的while循环将永远不会结束。

希望有所帮助,让我知道这是否有意义。

答案 1 :(得分:1)

以Max的答案为基础:

如果有很多迭代没有成功,你可能希望系统中止。

我会写的:

for i in range(numberofblocksyouwant):

while true:
    numberofattempts = 0
    create a block at a random x,y pos
    if block collides with any existing blocks:
        remove the block you just created
        numberofattempts += 1
        if numberofattempts > 1000:
           return "took too many attempts"
    else:
       break

while true: numberofattempts = 0 create a block at a random x,y pos if block collides with any existing blocks: remove the block you just created numberofattempts += 1 if numberofattempts > 1000: return "took too many attempts" else: break