python 有人能解释一下为什么它会给我这个错误吗:属性错误:"pygame.Surface"对象没有属性"clicked"

r7xajy2e  于 2023-02-21  发布在  Python
关注(0)|答案(2)|浏览(123)
import pygame

class Piece():
    def __init__(self,pos,image,clicked):
        self.pos = pos
        self.image = image
        self.clicked = clicked
b_bi = pygame.image.load("Chess_bdt45.svg.png")
w_bi = pygame.image.load("Chess_blt45.svg.png")
b_k = pygame.image.load("Chess_kdt45.svg.png")
w_k = pygame.image.load("Chess_klt45.svg.png")
b_kn = pygame.image.load("Chess_ndt45.svg.png")
w_kn = pygame.image.load("Chess_nlt45.svg.png")
b_p = pygame.image.load("Chess_pdt45.svg.png")
w_p = pygame.image.load("Chess_plt45.svg.png")
b_q = pygame.image.load("Chess_qdt45.svg.png")
w_q = pygame.image.load("Chess_qlt45.svg.png")
b_r = pygame.image.load("Chess_rdt45.svg.png")
w_r = pygame.image.load("Chess_rlt45.svg.png")

b_r1= Piece((0,0),b_r,False)
b_kn1= Piece((1,0),b_kn,False)
b_bi1= Piece((2,0),b_bi,False)
b_q= Piece((4,0),b_q,False)
b_k= Piece((3,0),b_k,False)
b_bi2= Piece((5,0),b_bi,False)
b_kn2= Piece((6,0),b_kn,False)
b_r2= Piece((7,0),b_p,False)
b_p1= Piece((0,1),b_p,False)
b_p2= Piece((1,1),b_p,False)
b_p3= Piece((2,1),b_p,False)
b_p4= Piece((3,1),b_p,False)
b_p5= Piece((4,1),b_p,False)
b_p6= Piece((5,1),b_p,False)
b_p7= Piece((6,1),b_p,False)
b_p8= Piece((7,1),b_p,False)

w_r1= Piece((0,7),w_r,False)
w_kn1= Piece((1,7),w_kn,False)
w_bi1= Piece((2,7),w_bi,False)
w_q= Piece((3,7),w_q,False)
w_k= Piece((4,7),w_k,False)
w_bi2= Piece((5,7),w_bi,False)
w_kn2= Piece((6,7),w_kn,False)
w_r2= Piece((7,7),w_r,False)
w_p1= Piece((8,6),w_r,False)
w_p2= Piece((1,6),w_r,False)
w_p3= Piece((2,6),w_r,False)
w_p4= Piece((3,6),w_r,False)
w_p5= Piece((4,6),w_r,False)
w_p6= Piece((5,6),w_r,False)
w_p7= Piece((6,6),w_r,False)
w_p8= Piece((7,6),w_r,False)

vector = []
vector.append(b_r1)
vector.append(b_kn1)
vector.append(b_bi1)
vector.append(b_q)
vector.append(b_k)
vector.append(b_bi2)
vector.append(b_kn2)
vector.append(b_r2)
vector.append(w_r1)
vector.append(w_kn1)
vector.append(w_bi1)
vector.append(w_k)
vector.append(w_q)
vector.append(w_bi2)
vector.append(w_kn2)
vector.append(w_r2)

b_p = pygame.transform.scale(b_p,(100,100))
w_p = pygame.transform.scale(w_p,(100,100))

for i in range(len(vector)):
    vector[i] = pygame.transform.scale(vector[i].image, (100, 100))

def draw_pieces(screen):
    for Piece in vector:
        screen.blit(Piece.image, (Piece.pos[0] * 100, Piece.pos[1] * 100))

追溯(最近调用最后调用):文件"C:\Users\hsvdg\PycharmProjects\chessapp\pieces.py",第85行,位于draw_pieces screen. blit文件中(片段图像,(片段位置[0]* 100,片段位置[1]* 100))属性错误:"pygame.Surface"对象没有属性"image"
我已经创建了片断对象的示例,并将它们添加到向量"vector"中,但是它仍然给我这个错误。任何帮助都将不胜感激。谢谢。

djmepvbi

djmepvbi1#

根据文档,函数pygame.transform.scale返回一个Surface对象。https://www.pygame.org/docs/ref/transform.html#pygame.transform.scale
在这些行中,您可以为每个向量元素指定该函数的输出:

for i in range(len(vector)):
    vector[i] = pygame.transform.scale(vector[i].image, (100, 100))

因此向量中没有Piece对象,而是从该调用返回的Surface对象,它们确实没有image属性。
另外,Piece在for循环中是一个错误的名称(这个循环):

def draw_pieces(screen):
    for Piece in vector:
        screen.blit(Piece.image, (Piece.pos[0] * 100, Piece.pos[1] * 100))

例如,它应该是piece

def draw_pieces(screen):
    for piece in vector:
        screen.blit(piece.image, (piece.pos[0] * 100, piece.pos[1] * 100))

Piece隐藏了你上面定义的类的名字,如果你解决了我上面提到的问题(错误的向量赋值/转换),它会工作,但是这样命名它仍然是一个不好的做法,它会让人困惑。

0s7z1bwu

0s7z1bwu2#

看起来for循环中的Piece被当作是对pygame.Surface对象而不是Piece类的引用。
这可能是因为您正在为循环变量重用名称“Piece”,而该名称与类的名称相同。请尝试将循环变量名称更改为其他名称,如“piece”:

def draw_pieces(screen):
for piece in vector:
    screen.blit(piece.image, (piece.pos[0] * 100, piece.pos[1] * 100))

相关问题