There are so many ways to make something like this it all depends on what you'd like to do with it..
Which part of this do you have difficulties with?
The red block moving from left to right and back can be done with many movement behaviours
Stopping the movement on space bar shouldn't be hard..
Comparing the position of the red sprite and determining it's distance from either 1 or 25 to get the right value could be done by comparing the x-position or distance