Python 解决pygame binascii。错误:填充不正确?

Python 解决pygame binascii。错误:填充不正确?,python,pygame,runtime-error,2d-games,tiled,Python,Pygame,Runtime Error,2d Games,Tiled,我正在为我班上的最后一个项目做游戏。我对pygame和整个游戏开发都很陌生。我以此为向导 我使用Tiled为我的游戏制作了一个平铺贴图,但当我尝试使用pygame运行它时,我得到了以下结果: Traceback (most recent call last): File "C:\Users\Rose\Desktop\mp2 shits\shit.py", line 114, in <module> Game().main(screen) File "C:\Users\Rose\Des

我正在为我班上的最后一个项目做游戏。我对pygame和整个游戏开发都很陌生。我以此为向导

我使用Tiled为我的游戏制作了一个平铺贴图,但当我尝试使用pygame运行它时,我得到了以下结果:

Traceback (most recent call last):
File "C:\Users\Rose\Desktop\mp2 shits\shit.py", line 114, in <module>
Game().main(screen)
File "C:\Users\Rose\Desktop\mp2 shits\shit.py", line 58, in main
self.tilemap = tmx.load('tileMap.tmx', screen.get_size())
File "C:\Users\Rose\Desktop\mp2 shits\tmx.py", line 835, in load
return TileMap.load(filename, viewport)
File "C:\Users\Rose\Desktop\mp2 shits\tmx.py", line 714, in load
layer = Layer.fromxml(tag, tilemap)
File "C:\Users\Rose\Desktop\mp2 shits\tmx.py", line 255, in fromxml
data = data.decode('base64').decode('zlib')
File "C:\Python27\lib\encodings\base64_codec.py", line 42, in base64_decode
output = base64.decodestring(input)
File "C:\Python27\lib\base64.py", line 325, in decodestring
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
[Finished in 5.0s]
回溯(最近一次呼叫最后一次):
文件“C:\Users\Rose\Desktop\mp2 shits\shit.py”,第114行,在
游戏()主(屏幕)
文件“C:\Users\Rose\Desktop\mp2 shits\shit.py”,第58行,在main中
self.tilemap=tmx.load('tilemap.tmx',screen.get\u size())
文件“C:\Users\Rose\Desktop\mp2 shits\tmx.py”,第835行,已加载
返回TileMap.load(文件名,视口)
文件“C:\Users\Rose\Desktop\mp2 shits\tmx.py”,第714行,已加载
layer=layer.fromxml(标记,tilemap)
文件“C:\Users\Rose\Desktop\mp2 shits\tmx.py”,第255行,格式为fromxml
data=data.decode('base64')。decode('zlib'))
文件“C:\Python27\lib\encodings\base64\u codec.py”,第42行,base64\u decode
输出=base64.decodestring(输入)
文件“C:\Python27\lib\base64.py”,第325行,解码字符串
返回binascii.a2b_base64(s)
binascii.错误:填充不正确
[完成时间为5.0秒]
这是我的密码:

import pygame
import tmx

class Player(pygame.sprite.Sprite):
def __init__(self, location, *groups):
    super(Player, self).__init__(*groups)
    self.image = pygame.image.load('red.gif').convert()
    self.rect = pygame.rect.Rect(location, self.image.get_size())
    self.resting = False 
    self.dy = 0 

def update(self, dt, game):
    last = self.rect.copy()

    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        self.rect.x -= 150 * dt
    if key[pygame.K_RIGHT]:
        self.rect.x += 150 * dt

    if self.resting and key[pygame.K_SPACE]:
        self.dy = -500
    self.dy = min(400, self.dy + 40)

    self.rect.y += self.dy * dt

    new = self.rect
    self.resting = False
    for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
        if last.right <= cell.left and new.right > cell.left:
            new.right = cell.left
        if last.left >= cell.right and new.left < cell.right:
            new.left = cell.right
        if last.bottom <= cell.top and new.bottom > cell.top:
            self.resting = True
            new.bottom = cell.top
            self.dy = 0
        if last.top >= cell.bottom and new.top < cell.bottom:
            new.top = cell.bottom
            self.dy = 0

    game.tilemap.set_focus(new.x, new.y)

class Game(object):
def main(self, screen):
    clock = pygame.time.Clock()

    background = pygame.image.load('bg.png')

    self.tilemap = tmx.load('tileMap.tmx', screen.get_size())

    self.sprites = tmx.SpriteLayer()
    start_cell = self.tilemap.layers['triggers'].find('player')[0]
    self.player = Player((start_cell.px, start_cell.py), self.sprites)
    self.tilemap.layers.append(self.sprites)

    fps = 30
    running = True
    while running:
        dt = clock.tick(fps)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False

        self.tilemap.update(dt/1000., self)
        pygame.display.set_caption("Little Red")
        screen.blit(background, (0,0))
        self.tilemap.draw(screen)
        pygame.display.flip()

if __name__ == '__main__':
    pygame.init()
    width = 1024
    height = 480
    screen = pygame.display.set_mode((width, height))
    Game().main(screen)
导入pygame
导入tmx
职业玩家(pygame.sprite.sprite):
定义初始化(自身、位置、*组):
超级(玩家,自我)。\uuuuu初始组(*组)
self.image=pygame.image.load('red.gif').convert()
self.rect=pygame.rect.rect(位置,self.image.get_size())
self.resting=False
self.dy=0
def更新(自我、dt、游戏):
last=self.rect.copy()
key=pygame.key.get_pressed()
如果键[pygame.K_左]:
自校正x-=150*dt
如果键[pygame.K_RIGHT]:
自校正x+=150*dt
如果self.resting和key[pygame.K_SPACE]:
self.dy=-500
self.dy=min(400,self.dy+40)
self.rect.y+=self.dy*dt
new=self.rect
self.resting=False
对于游戏中的单元格.tilemap.layers['triggers'].collide(新的'blockers'):
如果last.right cell.left:
new.right=cell.left
如果last.left>=cell.right和new.left=cell.bottom和new.top
提前感谢那些愿意帮忙的人

编辑:这就是我的地图的样子。。。

它看起来像是
tmx.py
无条件地尝试base64解码和zlib解码数据,而不检查数据的实际格式。最新版本的平铺默认为CSV格式的数据,因此请确保在平铺中的地图属性中将平铺层数据格式更改为base64+zlib


创建新地图时,您只需设置一次,因为平铺将记住您的首选项。

您能否发布或描述您的
tmx.py
?(tmx包的当前版本没有提供
tmx.load()
)是的,它现在链接到问题的底部。谢谢。不,我是说tmx.py。你的堆栈跟踪表明它与你的游戏在同一个目录中。我很抱歉。这是: