0

I have been messing around with pygame lately and I have encountered a problem. I'm coding a platformer and I have given the map creation a class. On the map module, I have a function that creates chunks based on the movement -specifically the "scroll" of the camera. My problem is that my game map is divided on a grid of 16x16 tiles and my platform tiles are 48x16 pixels which may be 3 tiles on my grid. I don't really know how to make my code understand it is 3 tiles so I can randomly generate platforms as the player goes up. Let me know if you know how to implement it.

import pygame, sys, random
from pygame.locals import *

class Map:
    def __init__(self):
        self.path_img = pygame.image.load("assets/imgs/path.png").convert()
        self.walls_img = pygame.image.load("assets/imgs/walls.png").convert()
        self.mossy_wall_img = pygame.image.load("assets/imgs/mossy_wall.png").convert()
        self.game_map = {}
        self.TILE_SIZE = self.walls_img.get_width()
        self.CHUNK_SIZE = 8
        self.tile_index = {1:self.path_img,
                            2:self.walls_img,
                            3:self.mossy_wall_img}

        self.platforms = []


    def draw_map(self, screen, scroll):
        self.tile_rects = []
        
        for y in range(3):
            for x in range(4):
                self.target_x = x - 1 + int(round(scroll[0]/(self.CHUNK_SIZE*16)))
                self.target_y = y - 1 + int(round(scroll[1]/(self.CHUNK_SIZE*16)))
                self.target_chunk = str(self.target_x) + ';' + str(self.target_y)
                if self.target_chunk not in self.game_map:
                    self.game_map[self.target_chunk] = self.generate_chunk(self.target_x,self.target_y)
                for tile in self.game_map[self.target_chunk]:
                    screen.blit(self.tile_index[tile[1]],(tile[0][0]*16-scroll[0],tile[0][1]*16-scroll[1]))
                    if tile[1] in [1,2,3,4]:
                        self.tile_rects.append(pygame.Rect(tile[0][0]*16,tile[0][1]*16,16,16))    
                
                

    def generate_chunk(self,x,y):
        self.chunk_data = []
        platform = False

        for y_pos in range(self.CHUNK_SIZE):
            for x_pos in range(self.CHUNK_SIZE):
                self.target_x = x * self.CHUNK_SIZE + x_pos 
                self.target_y = y * self.CHUNK_SIZE + y_pos
                tile_type = 0

                if self.target_x > 30:
                    if random.randint(1,5) == 1:
                        tile_type = 3
                    else:
                        tile_type = 2

                if self.target_x < 0:
                    if random.randint(1,5) == 1:
                        tile_type = 3
                    else:
                        tile_type = 2

                if self.target_y > 10:
                    if random.randint(1,5) == 1:
                        tile_type = 3
                    else:
                        tile_type = 2

                if self.target_y == 10:

                    if self.target_x > 30 or self.target_x < 0: 
                        if random.randint(1,5) == 1:
                            tile_type = 3
                        else:
                            tile_type = 2
                    else:
                        tile_type = 1

                if self.target_y < 10 and self.target_x < 30 and self.target_x > 0:

                    if random.randint(1,100) == 1:
                        platform = True


                if tile_type != 0:
                    self.chunk_data.append([[self.target_x, self.target_y],tile_type])

                if platform:
                    self.chunk_data.append([[self.target_x, self.target_y],1])
                    self.chunk_data.append([[self.target_x + 1, self.target_y],1])
                    self.chunk_data.append([[self.target_x + 2, self.target_y],1])

                    
        return self.chunk_data

1 Answers1

0

Would have to see one generated to get a better idea. Something like this might do, but then I'm guessing you'd want your CHUNK_SIZE to be evenly divisible by 3, so self.CHUNK_SIZE = 9 instead?

for y_pos in range( self.CHUNK_SIZE ):
    for x_pos in range( self.CHUNK_SIZE /3 ):

        ...

        self.target_x = x *self.CHUNK_SIZE +x_pos *3

        ...

        if tile_type != 0:
            self.chunk_data.append([ [ self.target_x, self.target_y ], tile_type ])
            self.chunk_data.append([ [ self.target_x +1, self.target_y ], tile_type +1 ])
            self.chunk_data.append([ [ self.target_x +2, self.target_y ], tile_type +2 ])
Doyousketch2
  • 2,060
  • 1
  • 11
  • 11
  • Your solution was not enough (mainly because I was not specific enough when posting my problem) but you gave me an easier way to approach the problem; just create individual tiles for the platforms, not 3x1 tiles. Thanks! – Diego Cruz Jun 18 '21 at 23:26