1"""
2This example procedurally develops a random cave based on cellular automata.
3
4For more information, see:
5https://gamedevelopment.tutsplus.com/tutorials/generate-random-cave-levels-using-cellular-automata--gamedev-9664
6
7If Python and Arcade are installed, this example can be run from the command line with:
8python -m arcade.examples.procedural_caves_cellular
9"""
10
11import random
12import arcade
13import timeit
14import os
15
16# Sprite scaling. Make this larger, like 0.5 to zoom in and add
17# 'mystery' to what you can see. Make it smaller, like 0.1 to see
18# more of the map.
19SPRITE_SCALING = 0.125
20SPRITE_SIZE = 128 * SPRITE_SCALING
21
22# How big the grid is
23GRID_WIDTH = 400
24GRID_HEIGHT = 300
25
26# Parameters for cellular automata
27CHANCE_TO_START_ALIVE = 0.4
28DEATH_LIMIT = 3
29BIRTH_LIMIT = 4
30NUMBER_OF_STEPS = 4
31
32# How fast the player moves
33MOVEMENT_SPEED = 5
34
35# How close the player can get to the edge before we scroll.
36VIEWPORT_MARGIN = 300
37
38# How big the window is
39WINDOW_WIDTH = 800
40WINDOW_HEIGHT = 600
41WINDOW_TITLE = "Procedural Caves Cellular Automata Example"
42# If true, rather than each block being a separate sprite, blocks on rows
43# will be merged into one sprite.
44MERGE_SPRITES = False
45
46
47def create_grid(width, height):
48 """ Create a two-dimensional grid of specified size. """
49 return [[0 for _x in range(width)] for _y in range(height)]
50
51
52def initialize_grid(grid):
53 """ Randomly set grid locations to on/off based on chance. """
54 height = len(grid)
55 width = len(grid[0])
56 for row in range(height):
57 for column in range(width):
58 if random.random() <= CHANCE_TO_START_ALIVE:
59 grid[row][column] = 1
60
61
62def count_alive_neighbors(grid, x, y):
63 """ Count neighbors that are alive. """
64 height = len(grid)
65 width = len(grid[0])
66 alive_count = 0
67 for i in range(-1, 2):
68 for j in range(-1, 2):
69 neighbor_x = x + i
70 neighbor_y = y + j
71 if i == 0 and j == 0:
72 continue
73 elif neighbor_x < 0 or neighbor_y < 0 or neighbor_y >= height or neighbor_x >= width:
74 # Edges are considered alive. Makes map more likely to appear naturally closed.
75 alive_count += 1
76 elif grid[neighbor_y][neighbor_x] == 1:
77 alive_count += 1
78 return alive_count
79
80
81def do_simulation_step(old_grid):
82 """ Run a step of the cellular automaton. """
83 height = len(old_grid)
84 width = len(old_grid[0])
85 new_grid = create_grid(width, height)
86 for x in range(width):
87 for y in range(height):
88 alive_neighbors = count_alive_neighbors(old_grid, x, y)
89 if old_grid[y][x] == 1:
90 if alive_neighbors < DEATH_LIMIT:
91 new_grid[y][x] = 0
92 else:
93 new_grid[y][x] = 1
94 else:
95 if alive_neighbors > BIRTH_LIMIT:
96 new_grid[y][x] = 1
97 else:
98 new_grid[y][x] = 0
99 return new_grid
100
101
102class MyGame(arcade.Window):
103 """
104 Main application class.
105 """
106
107 def __init__(self):
108 super().__init__(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, resizable=True)
109
110 # Set the working directory (where we expect to find files) to the same
111 # directory this .py file is in. You can leave this out of your own
112 # code, but it is needed to easily run the examples using "python -m"
113 # as mentioned at the top of this program.
114 file_path = os.path.dirname(os.path.abspath(__file__))
115 os.chdir(file_path)
116
117 self.grid = None
118 self.wall_list = None
119 self.player_list = None
120 self.player_sprite = None
121 self.view_bottom = 0
122 self.view_left = 0
123 self.draw_time = 0
124 self.processing_time = 0
125 self.physics_engine = None
126
127 arcade.set_background_color(arcade.color.BLACK)
128
129 def setup(self):
130 self.wall_list = arcade.SpriteList(use_spatial_hash=True)
131 self.player_list = arcade.SpriteList()
132
133 # Create cave system using a 2D grid
134 self.grid = create_grid(GRID_WIDTH, GRID_HEIGHT)
135 initialize_grid(self.grid)
136 for step in range(NUMBER_OF_STEPS):
137 self.grid = do_simulation_step(self.grid)
138
139 # Create sprites based on 2D grid
140 if not MERGE_SPRITES:
141 # This is the simple-to-understand method. Each grid location
142 # is a sprite.
143 for row in range(GRID_HEIGHT):
144 for column in range(GRID_WIDTH):
145 if self.grid[row][column] == 1:
146 wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING)
147 wall.center_x = column * SPRITE_SIZE + SPRITE_SIZE / 2
148 wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2
149 self.wall_list.append(wall)
150 else:
151 # This uses new Arcade 1.3.1 features, that allow me to create a
152 # larger sprite with a repeating texture. So if there are multiple
153 # cells in a row with a wall, we merge them into one sprite, with a
154 # repeating texture for each cell. This reduces our sprite count.
155 for row in range(GRID_HEIGHT):
156 column = 0
157 while column < GRID_WIDTH:
158 while column < GRID_WIDTH and self.grid[row][column] == 0:
159 column += 1
160 start_column = column
161 while column < GRID_WIDTH and self.grid[row][column] == 1:
162 column += 1
163 end_column = column - 1
164
165 column_count = end_column - start_column + 1
166 column_mid = (start_column + end_column) / 2
167
168 wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING,
169 repeat_count_x=column_count)
170 wall.center_x = column_mid * SPRITE_SIZE + SPRITE_SIZE / 2
171 wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2
172 wall.width = SPRITE_SIZE * column_count
173 self.wall_list.append(wall)
174
175 # Set up the player
176 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING)
177 self.player_list.append(self.player_sprite)
178
179 # Randomly place the player. If we are in a wall, repeat until we aren't.
180 placed = False
181 while not placed:
182
183 # Randomly position
184 max_x = GRID_WIDTH * SPRITE_SIZE
185 max_y = GRID_HEIGHT * SPRITE_SIZE
186 self.player_sprite.center_x = random.randrange(max_x)
187 self.player_sprite.center_y = random.randrange(max_y)
188
189 # Are we in a wall?
190 walls_hit = arcade.check_for_collision_with_list(self.player_sprite, self.wall_list)
191 if len(walls_hit) == 0:
192 # Not in a wall! Success!
193 placed = True
194
195 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
196 self.wall_list)
197
198 def on_draw(self):
199 """ Render the screen. """
200
201 # Start timing how long this takes
202 draw_start_time = timeit.default_timer()
203
204 # This command should happen before we start drawing. It will clear
205 # the screen to the background color, and erase what we drew last frame.
206 arcade.start_render()
207
208 # Draw the sprites
209 self.wall_list.draw()
210 self.player_list.draw()
211
212 # Draw info on the screen
213 sprite_count = len(self.wall_list)
214
215 output = f"Sprite Count: {sprite_count}"
216 arcade.draw_text(output,
217 self.view_left + 20,
218 self.height - 20 + self.view_bottom,
219 arcade.color.WHITE, 16)
220
221 output = f"Drawing time: {self.draw_time:.3f}"
222 arcade.draw_text(output,
223 self.view_left + 20,
224 self.height - 40 + self.view_bottom,
225 arcade.color.WHITE, 16)
226
227 output = f"Processing time: {self.processing_time:.3f}"
228 arcade.draw_text(output,
229 self.view_left + 20,
230 self.height - 60 + self.view_bottom,
231 arcade.color.WHITE, 16)
232
233 self.draw_time = timeit.default_timer() - draw_start_time
234
235 def on_key_press(self, key, modifiers):
236 """Called whenever a key is pressed. """
237
238 if key == arcade.key.UP:
239 self.player_sprite.change_y = MOVEMENT_SPEED
240 elif key == arcade.key.DOWN:
241 self.player_sprite.change_y = -MOVEMENT_SPEED
242 elif key == arcade.key.LEFT:
243 self.player_sprite.change_x = -MOVEMENT_SPEED
244 elif key == arcade.key.RIGHT:
245 self.player_sprite.change_x = MOVEMENT_SPEED
246
247 def on_key_release(self, key, modifiers):
248 """Called when the user releases a key. """
249
250 if key == arcade.key.UP or key == arcade.key.DOWN:
251 self.player_sprite.change_y = 0
252 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
253 self.player_sprite.change_x = 0
254
255 def on_resize(self, width, height):
256
257 arcade.set_viewport(self.view_left,
258 self.width + self.view_left,
259 self.view_bottom,
260 self.height + self.view_bottom)
261
262 def on_update(self, delta_time):
263 """ Movement and game logic """
264
265 start_time = timeit.default_timer()
266
267 # Call update on all sprites (The sprites don't do much in this
268 # example though.)
269 self.physics_engine.update()
270
271 # --- Manage Scrolling ---
272
273 # Track if we need to change the viewport
274
275 changed = False
276
277 # Scroll left
278 left_bndry = self.view_left + VIEWPORT_MARGIN
279 if self.player_sprite.left < left_bndry:
280 self.view_left -= left_bndry - self.player_sprite.left
281 changed = True
282
283 # Scroll right
284 right_bndry = self.view_left + WINDOW_WIDTH - VIEWPORT_MARGIN
285 if self.player_sprite.right > right_bndry:
286 self.view_left += self.player_sprite.right - right_bndry
287 changed = True
288
289 # Scroll up
290 top_bndry = self.view_bottom + WINDOW_HEIGHT - VIEWPORT_MARGIN
291 if self.player_sprite.top > top_bndry:
292 self.view_bottom += self.player_sprite.top - top_bndry
293 changed = True
294
295 # Scroll down
296 bottom_bndry = self.view_bottom + VIEWPORT_MARGIN
297 if self.player_sprite.bottom < bottom_bndry:
298 self.view_bottom -= bottom_bndry - self.player_sprite.bottom
299 changed = True
300
301 if changed:
302 arcade.set_viewport(self.view_left,
303 self.width + self.view_left,
304 self.view_bottom,
305 self.height + self.view_bottom)
306
307 # Save the time it took to do this.
308 self.processing_time = timeit.default_timer() - start_time
309
310
311def main():
312 game = MyGame()
313 game.setup()
314 arcade.run()
315
316
317if __name__ == "__main__":
318 main()