TetrArcade/arcade/examples/procedural_caves_cellular.py

318 lines
11 KiB
Python

"""
This example procedurally develops a random cave based on cellular automata.
For more information, see:
https://gamedevelopment.tutsplus.com/tutorials/generate-random-cave-levels-using-cellular-automata--gamedev-9664
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.procedural_caves_cellular
"""
import random
import arcade
import timeit
import os
# Sprite scaling. Make this larger, like 0.5 to zoom in and add
# 'mystery' to what you can see. Make it smaller, like 0.1 to see
# more of the map.
SPRITE_SCALING = 0.125
SPRITE_SIZE = 128 * SPRITE_SCALING
# How big the grid is
GRID_WIDTH = 400
GRID_HEIGHT = 300
# Parameters for cellular automata
CHANCE_TO_START_ALIVE = 0.4
DEATH_LIMIT = 3
BIRTH_LIMIT = 4
NUMBER_OF_STEPS = 4
# How fast the player moves
MOVEMENT_SPEED = 5
# How close the player can get to the edge before we scroll.
VIEWPORT_MARGIN = 300
# How big the window is
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
WINDOW_TITLE = "Procedural Caves Cellular Automata Example"
# If true, rather than each block being a separate sprite, blocks on rows
# will be merged into one sprite.
MERGE_SPRITES = False
def create_grid(width, height):
""" Create a two-dimensional grid of specified size. """
return [[0 for x in range(width)] for y in range(height)]
def initialize_grid(grid):
""" Randomly set grid locations to on/off based on chance. """
height = len(grid)
width = len(grid[0])
for row in range(height):
for column in range(width):
if random.random() <= CHANCE_TO_START_ALIVE:
grid[row][column] = 1
def count_alive_neighbors(grid, x, y):
""" Count neighbors that are alive. """
height = len(grid)
width = len(grid[0])
alive_count = 0
for i in range(-1, 2):
for j in range(-1, 2):
neighbor_x = x + i
neighbor_y = y + j
if i == 0 and j == 0:
continue
elif neighbor_x < 0 or neighbor_y < 0 or neighbor_y >= height or neighbor_x >= width:
# Edges are considered alive. Makes map more likely to appear naturally closed.
alive_count += 1
elif grid[neighbor_y][neighbor_x] == 1:
alive_count += 1
return alive_count
def do_simulation_step(old_grid):
""" Run a step of the cellular automaton. """
height = len(old_grid)
width = len(old_grid[0])
new_grid = create_grid(width, height)
for x in range(width):
for y in range(height):
alive_neighbors = count_alive_neighbors(old_grid, x, y)
if old_grid[y][x] == 1:
if alive_neighbors < DEATH_LIMIT:
new_grid[y][x] = 0
else:
new_grid[y][x] = 1
else:
if alive_neighbors > BIRTH_LIMIT:
new_grid[y][x] = 1
else:
new_grid[y][x] = 0
return new_grid
class MyGame(arcade.Window):
"""
Main application class.
"""
def __init__(self):
super().__init__(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, resizable=True)
# Set the working directory (where we expect to find files) to the same
# directory this .py file is in. You can leave this out of your own
# code, but it is needed to easily run the examples using "python -m"
# as mentioned at the top of this program.
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
self.grid = None
self.wall_list = None
self.player_list = None
self.player_sprite = None
self.view_bottom = 0
self.view_left = 0
self.draw_time = 0
self.physics_engine = None
arcade.set_background_color(arcade.color.BLACK)
def setup(self):
self.wall_list = arcade.SpriteList(use_spatial_hash=True)
self.player_list = arcade.SpriteList()
# Create cave system using a 2D grid
self.grid = create_grid(GRID_WIDTH, GRID_HEIGHT)
initialize_grid(self.grid)
for step in range(NUMBER_OF_STEPS):
self.grid = do_simulation_step(self.grid)
# Create sprites based on 2D grid
if not MERGE_SPRITES:
# This is the simple-to-understand method. Each grid location
# is a sprite.
for row in range(GRID_HEIGHT):
for column in range(GRID_WIDTH):
if self.grid[row][column] == 1:
wall = arcade.Sprite("images/grassCenter.png", SPRITE_SCALING)
wall.center_x = column * SPRITE_SIZE + SPRITE_SIZE / 2
wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2
self.wall_list.append(wall)
else:
# This uses new Arcade 1.3.1 features, that allow me to create a
# larger sprite with a repeating texture. So if there are multiple
# cells in a row with a wall, we merge them into one sprite, with a
# repeating texture for each cell. This reduces our sprite count.
for row in range(GRID_HEIGHT):
column = 0
while column < GRID_WIDTH:
while column < GRID_WIDTH and self.grid[row][column] == 0:
column += 1
start_column = column
while column < GRID_WIDTH and self.grid[row][column] == 1:
column += 1
end_column = column - 1
column_count = end_column - start_column + 1
column_mid = (start_column + end_column) / 2
wall = arcade.Sprite("images/grassCenter.png", SPRITE_SCALING,
repeat_count_x=column_count)
wall.center_x = column_mid * SPRITE_SIZE + SPRITE_SIZE / 2
wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2
wall.width = SPRITE_SIZE * column_count
self.wall_list.append(wall)
# Set up the player
self.player_sprite = arcade.Sprite("images/character.png", SPRITE_SCALING)
self.player_list.append(self.player_sprite)
# Randomly place the player. If we are in a wall, repeat until we aren't.
placed = False
while not placed:
# Randomly position
max_x = GRID_WIDTH * SPRITE_SIZE
max_y = GRID_HEIGHT * SPRITE_SIZE
self.player_sprite.center_x = random.randrange(max_x)
self.player_sprite.center_y = random.randrange(max_y)
# Are we in a wall?
walls_hit = arcade.check_for_collision_with_list(self.player_sprite, self.wall_list)
if len(walls_hit) == 0:
# Not in a wall! Success!
placed = True
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
self.wall_list)
def on_draw(self):
""" Render the screen. """
# Start timing how long this takes
draw_start_time = timeit.default_timer()
# This command should happen before we start drawing. It will clear
# the screen to the background color, and erase what we drew last frame.
arcade.start_render()
# Draw the sprites
self.wall_list.draw()
self.player_list.draw()
# Draw info on the screen
sprite_count = len(self.wall_list)
output = f"Sprite Count: {sprite_count}"
arcade.draw_text(output,
self.view_left + 20,
self.height - 20 + self.view_bottom,
arcade.color.WHITE, 16)
output = f"Drawing time: {self.draw_time:.3f}"
arcade.draw_text(output,
self.view_left + 20,
self.height - 40 + self.view_bottom,
arcade.color.WHITE, 16)
output = f"Processing time: {self.processing_time:.3f}"
arcade.draw_text(output,
self.view_left + 20,
self.height - 60 + self.view_bottom,
arcade.color.WHITE, 16)
self.draw_time = timeit.default_timer() - draw_start_time
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """
if key == arcade.key.UP:
self.player_sprite.change_y = MOVEMENT_SPEED
elif key == arcade.key.DOWN:
self.player_sprite.change_y = -MOVEMENT_SPEED
elif key == arcade.key.LEFT:
self.player_sprite.change_x = -MOVEMENT_SPEED
elif key == arcade.key.RIGHT:
self.player_sprite.change_x = MOVEMENT_SPEED
def on_key_release(self, key, modifiers):
"""Called when the user releases a key. """
if key == arcade.key.UP or key == arcade.key.DOWN:
self.player_sprite.change_y = 0
elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
self.player_sprite.change_x = 0
def on_resize(self, width, height):
arcade.set_viewport(self.view_left,
self.width + self.view_left,
self.view_bottom,
self.height + self.view_bottom)
def update(self, delta_time):
""" Movement and game logic """
start_time = timeit.default_timer()
# Call update on all sprites (The sprites don't do much in this
# example though.)
self.physics_engine.update()
# --- Manage Scrolling ---
# Track if we need to change the viewport
changed = False
# Scroll left
left_bndry = self.view_left + VIEWPORT_MARGIN
if self.player_sprite.left < left_bndry:
self.view_left -= left_bndry - self.player_sprite.left
changed = True
# Scroll right
right_bndry = self.view_left + WINDOW_WIDTH - VIEWPORT_MARGIN
if self.player_sprite.right > right_bndry:
self.view_left += self.player_sprite.right - right_bndry
changed = True
# Scroll up
top_bndry = self.view_bottom + WINDOW_HEIGHT - VIEWPORT_MARGIN
if self.player_sprite.top > top_bndry:
self.view_bottom += self.player_sprite.top - top_bndry
changed = True
# Scroll down
bottom_bndry = self.view_bottom + VIEWPORT_MARGIN
if self.player_sprite.bottom < bottom_bndry:
self.view_bottom -= bottom_bndry - self.player_sprite.bottom
changed = True
if changed:
arcade.set_viewport(self.view_left,
self.width + self.view_left,
self.view_bottom,
self.height + self.view_bottom)
# Save the time it took to do this.
self.processing_time = timeit.default_timer() - start_time
def main():
game = MyGame()
game.setup()
arcade.run()
if __name__ == "__main__":
main()