我如何检查两个列表包含的表面 pygame 对象是否相等?

问题描述

我编写了一个游戏,当两个包含 Sufaces 对象的列表相等但运算符 == 不起作用时,该游戏结束

代码的目标是将图像切成 9 部分,以便将其作为网格查看

这是一段代码


def resize_image(img,left,top,right,bottom):
    size=(left,bottom)
    subimg=img.crop(size)
    # transform PIL image in suface for work in pygame
    subimg=pygame.image.fromstring(subimg.tobytes(),subimg.size,subimg.mode)
    return subimg

def build_Image(N):
    images=laod_images() #function that loads two images
    img=images[0]
    img1=images[1]
    left=0
    top=0
    right=100
    bottom=100


    tab=emptyMatrix(N) #function that create a empty matrix
    for i in range(N):
        for j in range(N):
            if i==N-1 and j==N-1:
                img1 = resize_image(img1,bottom)
                tab[i][j] = img1

                pos[i][j]=(left,top) matrix that save coordinate of surface objects

            else:
                subimg = resize_immagine(img1,bottom)
                tab[i][j] =subimg

                pos[i][j]=(left,top)

            left += 100
            right += 100

        top+=100
        bottom+=100
        left=0
        right=100
    return tab

tab=build_Image()
tab1=build_Image()
print(tab==tab1) #print False

解决方法

当比较包含相同顺序的相同对象的两个长度相同的列表时,比较运算符的计算结果为 True
但是,如果使用 pygame.image.load() 两次加载相同的图像,则会生成两个不同的 Surface 对象。此对象不相等。