【问题标题】:How can i chek if two list contained surface pygame object are equal?如何检查两个列表是否包含表面 pygame 对象是否相等?
【发布时间】:2021-04-07 21:36:47
【问题描述】:

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

此代码的目标是将图像切成 9 部分以将其视为网格

这是一段代码


def resize_image(img,left,top,right,bottom):
    size=(left,top,right,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, left, top, right, bottom)
                tab[i][j] = img1

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

            else:
                subimg = resize_immagine(img1, left, top, right, 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

【问题讨论】:

  • 然后阅读documentation 了解为什么它不起作用。
  • 你必须展示一些代码。当比较包含相同对象的两个相同长度的列表时,比较运算符起作用。但是,如果您两次加载相同的图像,则会生成两个不同的 Surface 对象。此对象不相等。
  • @Pjay resize_image 做什么?我认为您误解了 Surfaces 的基本概念
  • 我已经更新了代码。

标签: python pygame pygame-surface


【解决方案1】:

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

【讨论】:

  • 好的有方法或函数二比较这个矩阵吗?
猜你喜欢
  • 2019-11-04
  • 2022-08-22
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
相关资源
最近更新 更多