Skip to main content
 首页 » 编程设计

python之所以我在制作角色动画时遇到了麻烦,而且我是 python 的新手。我不断收到此错误,但我不明白

2024年11月01日10mq0036

pygame 1.9.6 
Hello from the pygame community. https://www.pygame.org/contribute.html 
Traceback (most recent call last): 
 File "C:\Users\\OneDrive\Documents\Turtle\turtletest.py", line 52, in draw 
  `win.blit(char, (self.x,self.y))` 
TypeError: argument 1 must be pygame.Surface, not list 

现在这是我的代码

    class player(object): 
    def __init__(self, x, y, width, height) : 
        self.x = x 
        self.y = y 
        self.width = 42 
        self.height = 45 
        self.vel = 5 
        self.left = False 
        self.right = False 
        self.up = False 
        self.down = False 
        self.walkCount = 0     
 
    def draw(self,win) : 
        if self.walkCount + 1 >= 27: 
            self.walkCount = 0 
        if self.left: 
            win.blit(walkLeft[self.walkCount//3], (self.x,self.y)) 
            self.walkCount += 1 
        elif self.right: 
            win.blit(walkRight[self.walkCount//3], (self.x,self.y)) 
            self.walkCount += 1 
        if self.up: 
            win.blit(walkUp[self.walkCount//3], (self.x,self.y)) 
            self.walkCount += 1 
        elif self.down: 
            win.blit(walkDown[walkcount//3], (self.x,self.y)) 
            self.walkCount += 1 
        else: 
            win.blit(char, (self.x,self.y)) 
 
 
def redrawGameWindow(): 
 # fills screen with a background color     
    win.fill(WHITE) 
    turtle.draw(win) 
    pygame.display.update() 
 
 
 
#main loop for 
turtle = player(300, 410, 42, 45) 
run = True 
while run: 
    clock.tick(27) 
 
    for event in pygame.event.get() : 
        if event.type == pygame.QUIT: 
            run = False 
 
    keys = pygame.key.get_pressed() 
#left 
    if keys[pygame.K_LEFT] and trutle.x > turtle.vel: 
        turtle.x -= turtle.vel 
        turtle.left = True 
        turtle.right = False 
#right 
    elif keys[pygame.K_RIGHT] and turtle.x < windowX - turtle.width - turtle.vel: 
        turtle.x += turtle.vel 
        turtle.right = True 
        turtle.left = False 
    else: 
        turtle.right = False 
        turtle.left = False 
        turtle.walkCount = 0 
 
# up 
    if keys[pygame.K_UP] and turtle.y > turtle.vel: 
        turtle.y -= turtle.vel 
        turtle.up = True 
        turtle.down = False 
#down 
    if keys[pygame.K_DOWN] and turtle.y < windowY - turtle.width - turtle.vel: 
        turtle.y += turtle.vel 
        turtle.down = True 
        turtle.up = False 
 
    redrawGameWindow() 
 
 
pygame.quit()``` 

请您参考如下方法:

我假设 char 是 pygame.Surface 的列表具有单个元素的对象。

char = [pygame.image.load(...)] 

如果列表“char”包含单个表面,则不要创建列表。相反,只需将加载的 Surface 对象分配给变量 char(只需删除方括号 [, ]):

char = pygame.image.load(...) 

或者在draw中使用列表中的第一个Surface对象(char[0]):

win.blit(char[0], (self.x,self.y))