Labyrinth/maze¶
Objectives¶
This station is a repetition of techniques already shown. But wait, it is also the starting point for a game that we build in the End of these course. It is named »Sokoban«. If you don’t know this game you can read more about it at Game Sokoban |
Labyrinth¶
Our starting point is a maze build out of cubes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #! bpy
"""
Name: 'labyrinth.py'
Blender: 269
Group: 'Example'
Tooltip: 'Creating a maze'
"""
import bpy
level_00 = ["###################",
"#. #",
"# $ #",
"# #",
"# #",
"# @ #",
"# #",
"# #",
"# #",
"# #",
"###################"]
level_01 = [" ##### ",
" # # ",
" #$ # ",
" ### $## ",
" # $ $ # ",
"### # ## # ###### ",
"# # ## ##### ..# ",
"# $ $ ..# ",
"##### ### #@## ..# ",
" # ######### ",
" ####### "]
def sokobanLevel(level):
"""Create a maze by adding cube objects"""
cols = len(level[0])
rows = len(level)
for row in range(rows):
for i in range(cols):
if level[row][i] == '#':
bpy.ops.mesh.primitive_cube_add(location=(row*2, i*2, 0))
if __name__ == '__main__':
sokobanLevel(level_00)
|
Iterate with for¶
The function len gives us the number to iterate in a for loop through the lists.
cols = len(level[0])
rows = len(level)
Comparison¶
With each comparison we decide to generate a new object otherwise nothing happened.
for row in range(rows):
for i in range(cols):
if level[row][i] == '#':
bpy.ops.mesh.primitive_cube_add(location=(row*2, i*2, 0))
Function call with parameter¶
The function is now called with one of the available levels (level_00, level_01,level_02...).
if __name__ == '__main__':
sokobanLevel(level_00)