Normally it is ok if they are actually inside your house and attacking you. Some states have some "excessive force" laws that will get you in a lot of trouble for defending your own home. What the local police have told me is if you happen to kill them as they are running away, to make sure they (somehow) find their way back inside your house before you call to report it, that obviously won't work in some cases. Your supposed to let them get all the way inside before executing them and you usually have to prove that you actually were being attacked first. If you kill someone with a "boobie trap" that makes you a murderer, even if they were a violent criminal trying to break in. Check your local courts history to get an idea on what kind of cases have had judgements made against defenders of their own homes for a better idea of what you may have to prove. I have been the victim of a home invasion, and I WON'T back down if I see a stranger trying to get in my house!
If you shoot them in the front, then it's self defense, just don't shot them from behind bc they may think they were trying to get away an you shot anyway, but I also believe it depends where you live and what the local laws are