python - 解决pygame binascii.Error : Incorrect padding?

标签 python pygame runtime-error 2d-games tiled

我正在为类里面的期末项目制作一个游戏。我对 pygame 和整个游戏开发很陌生。我用过这个video作为我的指导。

我使用 Tiled 为我的游戏制作了一个图 block map ,但是当我尝试使用 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]

这是我的代码:

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)

预先感谢那些愿意提供帮助的人!

编辑:这就是我的 map 的样子...... tmx file

最佳答案

看起来tmx.py无条件地尝试对数据进行base64解码和zlib解码,而不检查数据实际的格式。最近版本的Tiled默认为CSV格式的数据,所以确保在 Tiled 的 map 属性中将图 block 图层数据格式更改为 base64+zlib。

创建新 map 时,您只需设置一次,因为 Tiled 会记住您的偏好。

关于python - 解决pygame binascii.Error : Incorrect padding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37328765/

相关文章:

firebase - android studio FirebaseAuth(IllegalArgumentException:给定的String为空或null)

python - 在 PYTHON 中向字典添加第二个键

python - 如何将可能格式错误的 xml 解析为数据框?

python - 为什么我的球粘在一起?

python - 如何在opengl和pygame中用键盘按键移动立方体?

python - 在 OpenGL 中在 3D 模型后面绘制背景视频

python - 为什么 Django 的 related_model 属性返回字符串而不是模型实例?

python - 如何根据Python中的条件用2级列替换数据框中的值?

c - 将自定义 assert() 与 AddressSanitizer 集成

c - 为什么我的 C 程序显示的 int 与应有的不同