def addBlank(savedter, key, weight, height, x, y): terrain = utils.mycopy(savedter) for i in range(len(terrain)): for j in range(len(terrain[0])): if j >= x and j < x + weight and i >= y and i < y + height: terrain[i][j] = key return terrain
def addRiver(savedter, key, x_dep, y_dep, x_ar, y_ar): terrain = utils.mycopy(savedter) #terrain = addBlank(terrain,5,x_ar-x_dep+1,y_ar-y_dep+1,x_dep,y_dep) points = [[x_dep, y_dep]] nbx, nby = (x_ar - x_dep) // 10 - 1, (y_ar - y_dep) // 20 - 1 if nbx < 0: nbx = 0 if nby < 0: nby = 0 x_dabord = nbx >= nby ix, iy = 0, 0 #print(x_dabord,nbx,nby) while not (nbx == 0 and nby == 0): if x_dabord: if nbx > 0: y = random.randint(-5, 5) points.append([x_dep + (ix + 1) * 10, (y_ar + y_dep) // 2 + y]) ix += 1 nbx -= 1 #print(nbx,nby,points) x_dabord = False else: if nby > 0: x = random.randint(-5, 5) points.append([y_dep + (iy + 1) * 20, (x_ar + x_dep) // 2 + x]) iy += 1 nby -= 1 #print(nbx,nby,points) x_dabord = True """ for i in range((x_ar-x_dep)//10-1): y = random.randint(-5,5) points.append([x_dep+(i+1)*10,(y_ar-y_dep//2)+y]) """ points.append([x_ar, y_ar]) #print(points) path = [] for i in range(len(points) - 1): path += drawPath(points[i][0], points[i][1], points[i + 1][0], points[i + 1][1]) for i in range(len(terrain)): for j in range(len(terrain[0])): if [j, i] in path: terrain[i][j] = key return terrain
def addForest(savedter, key, w, h, x, y, dt=random.randint(2, 4), dtlinear=2): # dt determine le nombre de rectangles de foret créé # dtlinear determine la facon dont sera créée la foret : # plus dtlinear est petit, plus les rectangles seront linéaires #terrain = addBlank(terrain,0,w,h,x,y) terrain = utils.mycopy(savedter) for k in range(dt): x_forest = random.randint(x, x + w - 1 - dtlinear) y_forest = random.randint(y, y + h - 1 - dtlinear) #terrain = addBlank(terrain,9,w-(x_forest-x),h-(y_forest-y),x_forest,y_forest) w_forest = random.randint(1 + dtlinear, w - (x_forest - x)) h_forest = random.randint(1 + dtlinear, h - (y_forest - y)) terrain = addBlank(terrain, key, w_forest, h_forest, x_forest, y_forest) return terrain
def addMultForest(savedter, key): #print(savedter) terrain = utils.mycopy(savedter) #for y in terrain: # print(' y: ',y) w_blank = len(terrain[0]) h_blank = len(terrain) if w_blank // 10 > 2: delta_w = w_blank // 10 elif w_blank // 10 == 0: return terrain else: delta_w = 3 if h_blank // 10 > 2: delta_h = h_blank // 10 elif h_blank // 10 == 0: return terrain else: delta_h = 3 nb_forest = random.randint(1, w_blank // 10 + h_blank // 10) #print(w_blank,h_blank,nb_forest) for i in range(nb_forest): x = random.randint(0, w_blank - delta_w) y = random.randint(0, h_blank - delta_h) w = random.randint(delta_w, w_blank - x) h = random.randint(delta_h, h_blank - y) #print(x,y,w,h) terrain = addForest(terrain, key, w, h, x, y) return terrain
def modifyPlatform(plat, w, h): # Fonction appelée dans events() # effectuée à chaque fois qu'on appuie sur la touche declencheur # permet de recreer une platforme ajoutée à celle originale """print(platform) for i in range(len(platform)): print('lvl',i) for y in platform[i]: print(' y: ',y)""" platform = utils.mycopy(plat) platform[1] = addMultForest(platform[1], random.choice(DIC_OF_RULES['tree'][0])) platform[0] = addMultForest(platform[0], random.choice(DIC_OF_RULES['water'][0])) #platform[0] = addRiver(platform[0],random.choice(DIC_OF_RULES['water'][0]),0,h//2,w,h//2) platform = verify(platform) return platform
def createZone(savedter, style, w, h, x=0, y=0): """ DOCUMENTATION CREATION DES BIOMES: savedter (list) : le terrain de base, qui ne sera pas modifié #key (int) : la couleur de dirt considérée x,y (int,int) : position du biom généré intégré direct au terrain final w,h (int,int) : dimension du biom genéré style (str) : decrit le mode de creation du biom LISTE DES STYLES: forest ocean mountain plain river jungle desert FORMAT DU RETOUR: terrain (donc [ levels [ casey [ casex ] ] ] ) avec: 1 pour de la dirt 0 pour du vide 2,3,4.. pour des charactères spéciaux (eau,pierre,mineraux..) #voir DIC_ZONES_CREATION """ tab = utils.mycopy(savedter) if style == 'forest': #freq = w , h keys = [0, 0, -1] noise = get_diff_noised_list(w, h, len(keys), freq[style], 2, 3) """for n in noise: print(n)""" for j in range(len(noise)): for i in range(len(noise[j])): if keys[noise[j][i]] != 0: #print(keys[noise[j][i]],tab[j+y][i+x]) try: tab[j + y][i + x] = keys[noise[j][i]] #print(i,j,'good') except: a = 0 ##print('coloring tree') v, w = 0, 0 for j in range(-y, len(noise) - y): for i in range(-x, len(noise[j]) - x): if tab[j + y][i + x] == -1: #print('lets go pour la',w+1,'e fois','key=',DIC_OF_RULES['tree'][0][v]) color(tab, (i, j), DIC_OF_RULES['tree'][0][v]) v += 1 w += 1 if v >= len(DIC_OF_RULES['tree'][0]): v = 0 if tab[j + y][i + x] in DIC_OF_RULES['tree'][0]: if random.choice([False] * 20 + [True]): tab[j + y][i + x] = random.choice( DIC_OF_RULES['stick'][0]) if style == 'stone': #freq = w , h keys = [0, 0, 0, -1] noise = get_diff_noised_list(w, h, len(keys), freq[style], 2, 3) for j in range(len(noise)): for i in range(len(noise[j])): if keys[noise[j][i]] != 0: try: tab[j + y][i + x] = keys[noise[j][i]] except: a = 0 #print('coloring rock') v, w = 0, 0 for j in range(-y, len(noise) - y): for i in range(-x, len(noise[j]) - x): if tab[j + y][i + x] == -1: #print('lets go pour la',w+1,'e fois','key=',DIC_OF_RULES['rock'][0][v]) color(tab, (i, j), DIC_OF_RULES['rock'][0][v]) v += 1 w += 1 if v >= len(DIC_OF_RULES['rock'][0]): v = 0 if tab[j + y][i + x] == 49: new_pos = [(i + x + 1, j + y), (i + x, j + y - 1), (i + x - 1, j + y), (i + x, j + y + 1), (i + x + 1, j + y + 1), (i + x + 1, j + y - 1), (i + x - 1, j + y - 1), (i + x - 1, j + y + 1)] for k in range(len(new_pos)): x2, y2 = new_pos[k] if y2 >= 0 and y2 < len(tab) and x2 >= 0 and x2 < len( tab[y2]): if tab[y2][x2] != 49 and tab[y2][x2] != 48: tab[y2][x2] = 48 if style == 'palet': #freq = w , h keys = [0, 0, 0, 0, -1] noise = get_diff_noised_list(w, h, len(keys), freq[style], 2, 3) for j in range(len(noise)): for i in range(len(noise[j])): if keys[noise[j][i]] != 0: try: tab[j + y][i + x] = keys[noise[j][i]] except: a = 0 #print('coloring palet') v, w = 0, 0 for j in range(-y, len(noise) - y): for i in range(-x, len(noise[j]) - x): if tab[j + y][i + x] == -1: #print('lets go pour la',w+1,'e fois','key=',DIC_OF_RULES['wooden palet'][0][v]) color(tab, (i, j), DIC_OF_RULES['wooden palet'][0][v]) v += 1 w += 1 if v >= len(DIC_OF_RULES['wooden palet'][0]): v = 0 if tab[j + y][i + x] in DIC_OF_RULES['wooden palet'][ 0] and random.choice([False, False, False, True]): new_pos = [(i + x + 1, j + y), (i + x, j + y - 1), (i + x - 1, j + y), (i + x, j + y + 1), (i + x + 1, j + y + 1), (i + x + 1, j + y - 1), (i + x - 1, j + y - 1), (i + x - 1, j + y + 1)] for k in range(len(new_pos)): x2, y2 = new_pos[k] if y2 >= 0 and y2 < len(tab) and x2 >= 0 and x2 < len( tab[y2]): if tab[y2][x2] not in DIC_OF_RULES['wooden palet'][ 0] + DIC_OF_RULES['stripped wood'][0]: tab[y2][x2] = random.choice( DIC_OF_RULES['stripped wood'][0]) return tab