patch #2: Amazing Maze

steps

In this patch, we build create a virtual invisible maze of 10*10 fields. A player can move through the maze using the arrow keys. We use the table object to save the positions of the walls and treasures and the sfplay~ object to play back the sounds.

We can imagine the invisible soundscape like this:

 

We give every virtual field a number, so we easily can easily specify moving in it. A bit like in a chessboard, however, no letters,

only numbers:

It is clear, if we would like to move in such a field, a step to the right would equal “+1″, a move to the top would equal “+10″. Similarly, moving down would equal “-10″ and moving left “-1″. We can move in our imagined but agreed upon space like this:

 

Due to the constrains of our field, we would have to make sure, the player stays within the the boundaries of 0 to 99.

 

Also, on a spot like 19, the player can not move to 20, as he/she is standing on an edge. Let’s try simulating this with max. We can use % (modulo) for this. Whenever we are a field with a “field number%10=9″ we do not allow to move to the right. Similarly, every field with a “field number%10=0″ does not allow for left movement. We use gates to allow or not allow for the movements…Let’s also highlight the postion with a background color:

 

We also have to make sure, we allow upwards movement only on fields with a number smaller than 90 and allow downwards movement only for fields with a number bigger than 9.



We now can move through our 10*10 field. Let’s place some walls which they user can not cross or move upon.

Let’s put the walls like this (grey) and define 0 as start and 99 target position.

We can for example save the walls in a table object. We can add a table with a size of 99 and a range of 3, that way we can define for all spots on the maze whether there is nothing special (a 0), a wall (a 1), or the target point (a 2).

The table is made, let’s put in the walls and target position:

 

Now, with every move, we have to check whether there is a wall in the way. If there isn’t, we make the move, if there is one, we don’t and print “Outch!”

It is now time to have some auditive feedback played with every successful step and some hit-the-wall-sound with every the player hits the wall. A soundplayer is needed. Max provides several options. We use sfplay~ for that. (The tilde indicates it is a MSP object, you can think of it as a little soundwave behind the object’s name).We copy a basic player from the help patches examples and preload the two samples we are interested in. (Make sure they are in the same folder as your patch).  The two samples are triggered by the “2” and “3” messages. (A “1” causes the currently loaded sample to play).

 

 

We connect the player to our maze by using making sure the “2” is triggered by valid movements and the “3” is triggered when the player hits a wall. Instead of using patchcords, we use send and receive objects. Have a look at the relevant part of the patch:

Now that everything is working, it is time to make our patch look a bit nicer and structure the components clearly. We use the panel object for that. This is our finished patch: