why my Tiled collisions doesn´t work in my pygame project?

106 views Asked by At

Im following a tutorial to make a game working with some pygame tiled and pytmx stuff, but however, we added a new object´s layer with some empty rectangles over the tiles where the player is not being able to enter, then we maked a wall´s list to put inside this "walls" and then we maded a couple of functions to check if the player is over the collision obstacle, and if that´s the case move the player one step back, but when I run the game, it opens the map and I can actually move my player around, but when I test the obstacle surface and collisions, it´s not working, and the player can go over the "walls". could you please help me to solve this, here I leave my wall´s code (inside of my Game Class):

def update(self):
    self.group.update()
    #verification du collision
    for sprite in self.group.sprites():
        if sprite.feet.collidelist(self.walls) > - 1:
            sprite.move_back()
def run(self):
    clock = pygame.time.Clock()
    #boucle du jeu 
    running = True
    while running:
        self.player.save_location()
        self.handle_input()
        self.update()
        self.group.center(self.player.rect)
        self.group.draw(self.screen)
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        clock.tick(60)
pygame.quit()

But... on the tutorial in Tiled software on the Obstacle information, there´s an option to put the "type" of object, at least that says on the tutorial, but in my Tiled, says "Class" instead of " Type" I think is the same, but however, I tried to put Class instead of Type in collision code, but... you know that Class is a reserved word by python, so... I put back " Type", I´m not sure if this sort of information matters, but maybe yes, in my tmx_map, on the obstacles layer I already set the "class" ("type object") of each object as "Collision" anyway, here´s the Player´s code

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
    super().__init__()
    self.sprite_sheet = pygame.image.load('player.png')
    self.image = self.get_image(0,0)
    self.image.set_colorkey([0,0,0])
    self.rect = self.image.get_rect()
    self.position = [x,y]
    self.images = {
        'down':self.get_image(0,0),
        'left' :self.get_image(0,32),
        'right':self.get_image(0,64),
        'up' :self.get_image(0,96),
    }
    self.feet = pygame.Rect(0,0, self.rect.width * 0.5, 12)
    self.old_position = self.position.copy()
    self.speed = 2

def save_location(self): self.old_position = self.position.copy()

def update(self):
    self.rect.topleft = self.position
    self.feet.midbottom = self.rect.midbottom

def move_back(self):
    self.position = self.old_position
    self.rect.topleft = self.position
    self.feet.midbottom = self.rect.midbottom

thanks in advance

1

There are 1 answers

0
Thorbjørn Lindeijer On

Since Tiled 1.9, "type" was renamed to "class", for consistency between all different kinds of objects, which can now all have a custom class.

Likely pytmx has not been updated yet with this change. Fortunately, since this change affected compatibility, Tiled provides an option to use the "Tiled 1.8" format, in which case the class is still written out as "type".

You can change the "Compatibility version" option in the Project Properties (you need to create a Tiled project to set this option).