gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:18 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

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

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

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

关于python - 所以我在制作角色动画时遇到了麻烦,而且我是 python 的新手。我不断收到此错误,但我不明白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com