I wrote this when I first noticed that the box was travelling partially off the screen. Could this also work for the purpose of detecting the edges of our window in the axe game? The variable axe_h is the box height. If the box were irregularly shaped we could then use axe_w for the width of the box.
//Move the axe and reverse direction
axe_y += direction;
if(axe_y > (height - (axe_h / 2) ) || axe_y < (axe_h / 2 ))
{
direction = -direction;