python pygame蒙版碰撞 [英] python pygame mask collision

查看:47
本文介绍了python pygame蒙版碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用这段代码时,它仅适用于1个对象,而不适用于每个对象.我正在尝试修改此视频中的代码(


要在不重叠的随机位置找到障碍物,必须评估是否有新障碍物撞到了以前的障碍物中.
创建一个随机位置:

  x,y = random.randint(100,400),random.randint(100,400) 

并评估是否存在 任何 与新位置重叠的障碍物.

  isect = any(j的(obstacle_mask [j] .overlap(obstacle_mask [i],(x-ox [j],y-oy [j])))在范围(i)中) 

如果是这种情况,请重复该过程.创建一个新的随机位置并测试是否重叠:

  obstacle = []barrier_mask = []oy = []牛= []对于我在范围(4)中:barrier.append(pygame.image.load("obstacle-400x399.png").convert_alpha())barrier_mask.append(pygame.mask.from_surface(obstacle [i]))isect =真而isect:x, y = random.randint(100, 400), random.randint(100, 400)isect =任何(范围(i)中j的(obstacle_mask [j] .overlap(obstacle_mask [i],(x-ox [j],y-oy [j]))))ox.append(x)oy.append(y) 

when I try to use this piece of code, it only works for 1 object, not for every. I'm trying to modify code from this video (https://www.youtube.com/watch?v=Idu8XfwKUao). Is there is a simpler way to get a result ?.If there is,please let me know

#part of code that doesn't matter
randomx = [100,400,300]
randomy = [100,0,300]
green_blob = pygame.image.load("greenblob-59x51.png").convert_alpha()
orange_blob = pygame.image.load("orangeblob-59x51.png").convert_alpha()
blob_mask = pygame.mask.from_surface(green_blob)
blob_color = green_blob
obstacle = []
obstacle_mask = []
oy = []
ox = []
for i in range(4):
    obstacle.append(pygame.image.load("obstacle-400x399.png").convert_alpha())
    obstacle_mask.append(pygame.mask.from_surface(obstacle[i]))
    ox.append(random.choice(randomx))
    oy.append(random.choice(randomy))
# main loop
while True:
    events()
    for i in range(4):
        mx, my = pygame.mouse.get_pos()

        offset = ((mx - int(ox[i]))), ((my - int(oy[i])))
        result = obstacle_mask[i].overlap(blob_mask, offset)
        if result:
            blob_color = orange_blob
        else:
            blob_color = green_blob
        screen.blit(obstacle[i], (ox[i], oy[i]))
        screen.blit(blob_color, (mx, my))



    pygame.display.update()
    CLOCK.tick(FPS)
    screen.fill(BLACK)

解决方案

Your application works fine. The issue is, that result of each evaluation of overlap cause a change of blob_color. So the last obstacle in the loop "wins". If the last obstacle overlaps the blob, then the color is orange_blob, else it is green_blob.
Set the green color before the loop. If any obstacle overlaps the blob, then change it to orange. The blob has to be drawn once after the loop. e.g.:

while True:
    events()

    blob_color = green_blob
    for i in range(4):
        mx, my = pygame.mouse.get_pos()

        offset = (int(mx - ox[i]), int(my - oy[i]))
        if obstacle_mask[i].overlap(blob_mask, offset):
            blob_color = orange_blob

        screen.blit(obstacle[i], (ox[i], oy[i]))

    screen.blit(blob_color, (mx, my))


To find obstacle at random positions, which are not overlapping, you have to evaluate if a new obstacle hits any of the former obstacles.
Create a random position:

x, y = random.randint(100, 400), random.randint(100, 400)

And evaluate if there is any obstacle that overlaps the new position.

isect = any(obstacle_mask[j].overlap(obstacle_mask[i], (x-ox[j], y-oy[j])) for j in range(i))

If that is the case, then repeat the process. Create a new random position and test for overlapping:

obstacle = []
obstacle_mask = []
oy = []
ox = []
for i in range(4):
    obstacle.append(pygame.image.load("obstacle-400x399.png").convert_alpha())
    obstacle_mask.append(pygame.mask.from_surface(obstacle[i]))

    isect = True
    while isect:
        x, y = random.randint(100, 400), random.randint(100, 400)
        isect = any(obstacle_mask[j].overlap(obstacle_mask[i], (x-ox[j], y-oy[j])) for j in range(i))

    ox.append(x)
    oy.append(y)

这篇关于python pygame蒙版碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆