所以我在给角色设置动画时遇到麻烦,而我是python的新手。我不断收到此错误,但我听不懂

时间:2019-10-06 15:01:10

标签: python python-3.x pygame pygame-surface

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()```

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

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