Coverage for rollnjump/worldgen.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Roll 'n' Jump
2# Written in 2020, 2021 by Samuel Arsac, Hugo Buscemi,
3# Matteo Chencerel, Rida Lali
4# To the extent possible under law, the author(s) have dedicated all
5# copyright and related and neighboring rights to this software to the
6# public domain worldwide. This software is distributed without any warranty.
7# You should have received a copy of the CC0 Public Domain Dedication along
8# with this software. If not, see
9# <http://creativecommons.org/publicdomain/zero/1.0/>.
11"""Gère la génération du monde."""
13import os
14import random as rd
15# Import classes
16import rollnjump.utilities as ut
17import rollnjump.sprites as spt
18import rollnjump.platforms as pltfrm
19import rollnjump.conf as cf
20import rollnjump.background as bg
21import rollnjump.item as it
23# Indexation des modules
24modules = []
25"""Liste préchargeant les fichiers modules"""
27MAX_JUMP = 200
28"""Hauteur maximale entre la dernière plateforme d'un module
29et la première plateforme du suivant"""
32def init_modules():
33 """Indexation des modules."""
34 for file in spt.listdir(cf.MODULES):
35 first_y, _, _ = file.split('_')
36 first_y = int(first_y)
37 module_file = open(os.path.join(cf.MODULES, file), 'r')
38 lines = module_file.readlines()
39 module_height = int(lines[0])
40 yoffset = cf.SCREEN_HEIGHT - module_height
41 blocs = []
42 for line in lines[1:]:
43 bloc = line.split(';')
44 blocs.append(bloc)
45 module_file.close()
46 modules.append((first_y, yoffset, blocs.copy()))
49# Fonctions de création
50def platform_creation(bloc, xoffset, yoffset):
51 """
52 Crée une plateforme.
54 Parameters
55 ----------
56 bloc : str list
57 Liste des chaînes de caractères définissant la plateforme
58 xoffset : int
59 Décalage en abscisses du module
60 yoffset : int
61 Décalage en ordonnées du module
62 """
63 top_left = bloc[1][1:-1].split(',')
64 top_left_y, top_left_x = int(top_left[0]), int(top_left[1])
65 bot_right = bloc[2][1:-2].split(',')
66 width = int(bot_right[1]) - top_left_x
67 if bloc[0] == 'Plateforme':
68 height = int(bot_right[0]) - top_left_y
69 sprite = spt.PLTFRM_IMG
70 else:
71 height = cf.SCREEN_HEIGHT
72 sprite = spt.BAT_IMG
73 plat = pltfrm.Platform((top_left_x + xoffset,
74 top_left_y + yoffset),
75 (width, height),
76 sprite)
77 ut.add_to_group(plat, spt.ground)
78 return plat
81def initgen():
82 """Initialise le monde."""
83 # Crée quelques nuages
84 for _ in range(4):
85 pos = (rd.randint(0, cf.SCREEN_WIDTH),
86 rd.randint(0, cf.SCREEN_HEIGHT // 2))
87 i = rd.randint(0, spt.img_dict["n_cloud"] - 1)
88 bg.Cloud(pos, i)
89 # Crée quelques arbres
90 for _ in range(4):
91 pos_x = rd.randint(0, cf.SCREEN_WIDTH)
92 i = rd.randint(0, spt.img_dict["n_tree"] - 1)
93 bg.Tree(pos_x, i)
95 # Lance la création du sol
96 # on rajoute des bouts de sol, on additionne leur longueur
97 # et quand on a couvert tout l'écran on s'arrête.
98 total_width = 0
99 while total_width < cf.SCREEN_WIDTH:
100 # On en met un nouveau à la fin
101 plat = pltfrm.Ground(total_width)
102 ut.add_to_group(plat, spt.ground)
103 total_width += spt.GROUND_WIDTH
106def genere_module(last_pltfrm):
107 """
108 Choisit et affiche un nouveau module à la fin de l'écran.
110 Parameters
111 ----------
112 last_pltfrm : Plateform
113 Dernière plateforme du module en cours
114 """
115 # Offset dépendant de la vitesse
116 module_offset = cf.SPEED * 10
117 xoffset = cf.SPEED * 10
118 # Début du nouveau module
119 xoffset = last_pltfrm.rect.right + module_offset
120 # Sélection des modules possibles
121 modules_possibles = [mod for mod in modules
122 if last_pltfrm.rect.top - mod[0] < MAX_JUMP]
123 # Choix aléatoire d'un module
124 _, yoffset, blocs = rd.choice(modules_possibles)
125 # Chargement du module
126 for bloc in blocs:
127 platform_creation(bloc, xoffset, yoffset)
130def stop_ground():
131 """Arrête la création infinie du sol."""
132 for bloc in spt.ground:
133 if isinstance(bloc, pltfrm.Ground):
134 bloc.stop_creation()
137def update():
138 """Met à jour tous les objets du monde autres que Player."""
139 cf.DISPLAYSURF.fill(cf.BlueSky) # Le ciel
140 spt.clouds.update()
141 spt.trees.update()
142 spt.ground.update()
143 spt.items.update()
145 try:
146 last_pltfrm = max(spt.ground, key=lambda bloc: bloc.rect.right)
147 if last_pltfrm.rect.right < cf.SCREEN_WIDTH:
148 genere_module(last_pltfrm)
149 except ValueError:
150 genere_module(pltfrm.Platform())
152 if (not cf.FLAG_ITEM) and (cf.SECONDS == cf.NEW_ITEM_TIME):
153 newitem = it.Item()
154 ut.add_to_group(newitem, spt.items)