【问题标题】:Pygame how to change image collision pointsPygame如何改变图像碰撞点
【发布时间】:2016-10-30 15:06:03
【问题描述】:

我正在尝试在 Pygame 中制作匹配纸牌游戏,我可以将所有 52 张纸牌渲染到正确的位置,但图像的矩形不会改变。

我使用以下方法创建所有卡片表面;

def generateGrid():
    cardB = pygame.image.load(os.path.join('data', 'back.png'))
    for i in range(index):
            grid.append(cardB)
            grid[i] = pygame.transform.scale(cardB, (85,130))

然后我通过在 main 方法中调用以下代码来渲染我的卡片;

generateGrid()
pos = 25

for i in range(index):
    print(pos)
    if i >0 and i< 14:
        r1 = grid[i].get_rect()
        r1.move_ip(pos,50)
        pos +=95
        background.blit(grid[i], r1)
        pygame.display.update()
    elif i >0 and i<27:
        r2 = grid[i].get_rect()
        pos -=95
        r2.move_ip(pos,195)
        background.blit(grid[i], r2)
        pygame.display.update()
    elif i >0 and i <40:
        r3 = grid[i].get_rect()
        r3.move_ip(pos,350)
        pos +=95

        background.blit(grid[i],r3)
        pygame.display.update()
    elif i >0 and i< 53:
        r4 = grid[i].get_rect()
        pos -=95
        r4.move_ip(pos,500)
        background.blit(grid[i],r4)
        pygame.display.update()
    print("Card", i+1, "rendered")

最后在我的主循环中,我尝试通过以下方式找到鼠标悬停在哪张卡片上:

for i in range(index):
    if grid[i].get_rect().collidepoint(pygame.mouse.get_pos()):
        print("Colling with:", i)
    else:
        pass

我的问题是所有表面都有相同的碰撞矩形:&lt;rect(0, 0, 85, 130)&gt;。如何更改它以匹配每张卡的坐标?

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    问题是因为与grid 中的图像关联的矩形不反映呈现卡片的代码将它们放置在哪里。这可以通过创建一个包含每个矩形信息的平行矩形列表来纠正。如果这个附加列表被命名为cardPosn,那么它的创建方法如下:

    pos = 25
    cardPosn = [None] * len(grid)  # preallocate
    for i in range(index):
        print(pos)
        if 0 < i < 14:
            r1 = grid[i].get_rect()
            r1.move_ip(pos, 50)
            pos += 95
            background.blit(grid[i], r1)
            cardPosn[i] = r1
            pygame.display.update()
        elif 0 < i < 27:
            r2 = grid[i].get_rect()
            pos -= 95
            r2.move_ip(pos, 195)
            background.blit(grid[i], r2)
            cardPosn[i] = r2
            pygame.display.update()
        elif 0 < i < 40:
            r3 = grid[i].get_rect()
            r3.move_ip(pos, 350)
            pos += 95
            background.blit(grid[i], r3)
            cardPosn[i] = r3
            pygame.display.update()
        elif 0 < i < 53:
            r4 = grid[i].get_rect()
            pos -= 95
            r4.move_ip(pos, 500)
            background.blit(grid[i], r4)
            cardPosn[i] = r4
            pygame.display.update()
        print("Card", i+1, "rendered")
    

    完成后,检查碰撞点的循环可以这样编写:

    mouse_pos = pygame.mouse.get_pos()
    for i in range(index):
        if cardPosn[i] and cardPosn[i].collidepoint(mouse_pos):
            print("Colliding with:", i)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多