Python Forum
Problem with Tiles and Mobs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Tiles and Mobs
#1
Hello.

I am programming a tile based game with pygame.
I am loading different objects, like trees, walls, houses etc. on the surface.
For that, I am using different files.
In the first file, I have my images defined:

WALL_IMG = 'stone_wall2.png'
TREE_IMG = 'tree.png'
HOUSE_IMG = 'houseSmallAlt1.png'
WATER_IMG = 'water.png'
In the second, I am loading them and loading them onto the map:

class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        self.load_data()

    def load_data(self):
        #Folders
        game_folder = path.dirname(__file__)
        img_folder = path.join(game_folder, 'img')
        mob_folder = path.join(game_folder, 'mobimg')
        player_folder = path.join(game_folder, 'playerimg')
        tile_folder = path.join(game_folder, 'tileimg')

        # Map
        self.map = Map(path.join(game_folder, 'map.txt'))
        #Player IMG
        self.player_img = pg.image.load(path.join(player_folder, PLAYER_IMG)).convert_alpha()
        # Zombie IMGs
        self.zombie_img = pg.image.load(path.join(mob_folder, ZOMBIE_IMG)).convert_alpha()
        # TILE IMG
        self.wall_img = pg.image.load(path.join(tile_folder, WALL_IMG)).convert_alpha()
        self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
        self.tree_img = pg.image.load(path.join(tile_folder, TREE_IMG))
        self.tree_img = pg.transform.scale(self.tree_img, (TILESIZE, TILESIZE))

    def new(self):
        # initialize all variables and do all the setup for a new game
        self.all_sprites = pg.sprite.Group()
        self.walls = pg.sprite.Group()
        self.trees = pg.sprite.Group()
        self.zombies = pg.sprite.Group()
        self.flubbers = pg.sprite.Group()
        self.water = pg.sprite.Group()
        self.blowpipes = pg.sprite.Group()
        for row, tiles in enumerate(self.map.data):
            for col, tile in enumerate(tiles):
                if tile == '1':
                    Wall(self, col, row)
                if tile == '2':
                    Water(self, col, row)
                if tile == '3':
                    Tree(self, col, row)
                if tile == '4':
                    House(self, col, row)
                if tile == 'Z':
                    Zombie(self, col, row)
                if tile == 'P':
                    self.player = Player(self, col, row)
                self.camera = Camera(self.map.width, self.map.height)

    def update(self):
        # update portion of the game loop
        self.all_sprites.update()
        self.camera.update(self.player)
        # And here follows stuff, you don't need to solve the problem
That above is just a part of the whole code. And there is also another file, called tile map. My problem is explained below.

class Map:
    def __init__(self, filename):
        self.data = []
        with open(filename, 'rt') as f:
            for line in f:
                self.data.append(line.strip())

        self.tilewidth = len(self.data[0])
        self.tileheight = len(self.data)
        self.width = self.tilewidth * TILESIZE
        self.height = self.tileheight * TILESIZE

class Camera:
    def __init__(self, width, height):
        self.camera = pg.Rect(0, 0, width, height)
        self.width = width
        self.height = height

    def apply(self, entity):
        return entity.rect.move(self.camera.topleft)

    def update(self, target):
        x = -target.rect.centerx + int(WIDTH / 2)
        y = -target.rect.centery + int(HEIGHT / 2)

        # limit scrolling to map size
        x = min(0, x)  # left
        y = min(0, y)  # top
        x = max(-(self.width - WIDTH), x)  # right
        y = max(-(self.height - HEIGHT), y)  # bottom
        self.camera = pg.Rect(x, y, self.width, self.height)
NOW MY PROBLEM:
I can move my player around the screen (not shown in the code above). And if I walk through trees, that are above my y spawning position, I am on top of them, but if there are trees below that y position I spawned at, I am under them. This is pretty annoying, e.g. I am not seeing myself when I am in water, that is below my y position I spawned at. Because I don't wanna spawn at the edge of the map.
Do you have any idea, how I could fix this problem? I have the same problem with mobs.
I just want to be able to tell python, if I am supposed to be below or upon the object.
Thank you so much for your help (sorry for posting so much code, was necessary).

Piethon
Reply


Messages In This Thread
Problem with Tiles and Mobs - by Piethon - Sep-24-2019, 02:32 PM
RE: Problem with Tiles and Mobs - by metulburr - Sep-24-2019, 06:42 PM
RE: Problem with Tiles and Mobs - by Piethon - Sep-25-2019, 02:32 PM
RE: Problem with Tiles and Mobs - by metulburr - Sep-25-2019, 11:36 PM
RE: Problem with Tiles and Mobs - by Piethon - Oct-04-2019, 10:29 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020