def __init__(self): '''(Zone) -> NoneType''' WIDTH, HEIGHT = get_zone_dim() TILE_SIZE = get_tile_size() NUM_TILES_W = int(WIDTH / TILE_SIZE) # Assumed to get integer in division NUM_TILES_H = int(HEIGHT / TILE_SIZE) self.rect = Rect(0, 0, WIDTH, HEIGHT) self.num_tiles_w = NUM_TILES_W self.num_tiles_h = NUM_TILES_H self.map = [[Tile(i * 50, j * 50) for j in range(NUM_TILES_H)] \ for i in range(NUM_TILES_W)] self.player = None
def __init__(self, X, Y): '''(Tile, int, int) -> NoneType (X,Y) top-left coordinate of tile.''' SIZE = get_tile_size() self.rect = Rect(X, Y, SIZE, SIZE) self.img = None self.value = None # In case text value should not be rendered self.wall = False self.wall_img = None self.trap = False self.trapimg = None self.key = False self.key_img = None self.lock = False self.lock_img = None self.exit = False self.exit_img = None
def update_rect(self): '''(Player) -> NoneType Updates the location of self.rect. ''' SIZE = get_tile_size() self.rect = Rect(self.x * SIZE, self.y * SIZE, SIZE, SIZE)
def load(self, ZONE_N): '''(Zone, str) -> NoneType Loads Zone from Zone given in ZONE_N. Assumes success. Format for map files: min <int_value> max <int_value> img <index> <img_name> ... map XYZ ... ... min, max - optional entries to specific inclusive range for randomly generated integers for tile values, which is used if given a ? character img - optional entry or entries to specify images for tiles (images are not required by tiles). <img_name> is added to a list, and <index> corresponds with the index in the list. map - an array of tile entries, which are X,Y,Z,U , separated by spaces. X is the type of tile to be loaded: 0 for default tile, 1 for trap, 2 is wall, 3 is key, 4 is lock, 5 is exit. Y is the img index for the primary img to be used, where 0 means no img. Z is the value to be given to the tile, where ? means a random value generated using min, max. U is the secondary img to be used, 0 for no img.''' TILE_SIZE = get_tile_size() # Seed random in case random values are to be generated random.seed() PATH = _get_zone_path(ZONE_N) with open(PATH) as FILE: min = None max = None img_list = [None] # None there for use of 0 for no img line = FILE.readline() while line: data = line.split() if data[0] == "min": min = int(data[1]) elif data[0] == "max": max = int(data[1]) elif data[0] == "start": coord = (int(data[1]), int(data[2])) elif data[0] == "img": img_list.append(data[2]) elif data[0] == "map": # Map is rest of file line = FILE.readline() for i in range(self.num_tiles_h): # Row number of data data = line.split() for j in range(self.num_tiles_w): # Column number of data tile = data[j].split(",") tile_s = Tile(j * TILE_SIZE, i * TILE_SIZE) img_index = int(tile[1]) sec_img_index = int(tile[3]) #loads the image of the tile if img_index: tile_s.load_img(img_list[img_index]) #gives the tile a value if tile[2] == "?": # Randomize value given by min, max tile_s.value = random.randint(min, max) else: tile_s.value = int(tile[2]) # Determine tile type if tile[0] == "0": # Normal pass elif tile[0] == "1": # Trap tile_s.trap = True tile_s.load_trapimg(img_list[sec_img_index]) elif tile[0] == "2": # Wall tile_s.wall = True tile_s.wall_img = load_img(img_list[sec_img_index]) elif tile[0] == "3": # Key tile_s.key = True tile_s.key_img = load_img(img_list[sec_img_index]) elif tile[0] == "4": # Lock tile_s.lock = True tile_s.lock_img = load_img(img_list[sec_img_index]) elif tile[0] == "5": # Exit tile_s.exit = True tile_s.exit_img = load_img(img_list[sec_img_index]) self.map[j][i] = tile_s line = FILE.readline() line = FILE.readline() self.player = Player(*coord) # Starting coords