Press space when the squares are all touching the grid. If you press space at any other time, you die.
Everything by me except for the coding for the score which is by @InternalFlame .