0

I'm trying to follow a tutorial/lecture on use of the module pygame in Python, http://www.youtube.com/watch?v=mTmJfWdZzbo. I also have all the files referenced in the video, here https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d?at=default. My problem occurs when trying to use .tmx files as a map for the game, step 14. I've tried writing my own code, but I also tried just using the code supplied from the lecture. No matter what I do, I can't seem to get past the following error:

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

Here "platformer.py" is my exact replica of the file named "14-tillemap.py" in the supplied files (and "map.tmx" also from the supplied files).

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)

I really can't figure out this error, nor could I find any problem relevant to this answered somewhere else.

I'm running Python 2.7 on a 64-bit windows 8.1 (if that matters)

Bendik
  • 1,097
  • 1
  • 8
  • 27
  • I used TMX in a small game I made. Please post your code or something like that here to see if I could help. – Depado Mar 04 '14 at 07:47
  • I've included the program now. The tmx-module can be seen on the same page I listed at the beginning of my question. – Bendik Mar 04 '14 at 07:57
  • I'm currently using PyTMX for this, I don't know which lib you're using. (https://github.com/bitcraft/PyTMX) – Depado Mar 04 '14 at 08:03
  • The module, tmx, was made by the guy in the lecture (also in the supplied files), so that's what he used. I guess I'll try this with a different lib., Thanks anyway! – Bendik Mar 04 '14 at 08:31
  • `tiledmap = tmxloader.load_pygame(filename, pixelalpha=True)` That's how I load my map. That's why I can't help you with your code, because I don't know how that lib is working. – Depado Mar 04 '14 at 08:37

0 Answers0