Detecting walls is fairly simple as all you need to do is check if the current sprite is touching a certain colour, in this case, a black wall. After the sprite has touched that colour you can tell the script to do anything such as lose a life using variables or move back to a certain location inside of the if statement.
Try out the code in the link below and see if you can change the code so that the sprite does nothing on black walls but goes back to the start point on red walls.