【发布时间】: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