iPhone 3GS suddenly shuts down while playing games
Usually when an iOS device suddenly powers off, it's a voltage drop in the battery due to a hardware problem. If the device draws more current than the battery can provide, it will shut off.

Are you able to take it in for service to have the device checked out? You could chase a software restore, but those usually leave a log file or the device reboots on its own. Also, over temp could cause the issue, but the device logs that and shows an over temp error on the display rather than just blacking out most of the times heat is a cause.