Так что у меня проблемы с анимацией персонажа, и я новичок в питоне. Я продолжаю получать эту ошибку, и я не понимаю ее - PullRequest
2 голосов
/ 06 октября 2019
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 Ответ

1 голос
/ 06 октября 2019

Я предполагаю, что char - это список pygame.Surface объектов с одним элементом.

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

Если список "charmsgstr "содержит одну поверхность, затем не создавайте список. Вместо этого просто назначьте загруженный объект Surface переменной char (просто снимите квадратные скобки [, ]):

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

или используйте 1-й объект Surface (char[0]) в списке в draw:

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