使用tgx和pygame时出错

时间:2014-03-04 07:44:12

标签: python pygame zlib tmx

我正在尝试按照Python http://www.youtube.com/watch?v=mTmJfWdZzbo中关于使用模块pygame的教程/讲座。我还在视频中引用了所有文件,https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d?at=default。尝试使用.tmx文件作为游戏的地图时,我的问题出现了,第14步。我已经尝试编写自己的代码,但我也尝试使用讲座提供的代码。无论我做什么,我似乎无法克服以下错误:

C:\Users\me\Copy\Div\pygame\Platformer>python platformer.py
Traceback (most recent call last):
  File "platformer.py", line 74, in <module>
    Game().main(screen)
  File "platformer.py", line 50, in main
    self.tilemap = tmx.load('map.tmx', (640,480))
  File "C:\Users\me\Copy\Div\pygame\Platformer\newtmx.py", line 573, in load
    return TileMap.load(filename, viewport)
  File "C:\Users\me\Copy\Div\pygame\Platformer\newtmx.py", line 456, in load
    layer = Layer.fromxml(tag, tilemap)
  File "C:\Users\me\Copy\Div\pygame\Platformer\newtmx.py", line 242, in fromxml
    data = data.decode('base64').decode('zlib')
  File "C:\Python27\lib\encodings\zlib_codec.py", line 43, in zlib_decode
    output = zlib.decompress(input)
zlib.error: Error -5 while decompressing data: incomplete or truncated stream

这里“platformer.py”是我提供的文件中名为“14-tillemap.py”的文件的精确副本(以及提供的文件中的“map.tmx”)。

import pygame
import tmx

class Player(pygame.sprite.Sprite):
    def __init__(self, location, *groups):
        super(Player, self).__init__(*groups)
        self.image = pygame.image.load('player.png')
        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 -= 300 * dt
        if key[pygame.K_RIGHT]:
            self.rect.x += 300 * 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('background.png')

        self.tilemap = tmx.load('map.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)

        while 1:
            dt = clock.tick(30)

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

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

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    Game().main(screen)

我真的无法弄清楚这个错误,也没有找到任何与此相关的问题。

我在64位Windows 8.1上运行Python 2.7(如果这很重要)

0 个答案:

没有答案