It happened to me once and I swore not to let it happen again. So here is what I do:
1) If I feel gassy, I avoid sex altogether till everything relaxes there.
2) If I'm gassy and horny and can't put off sex, I go to the toilet and empty y bowels. If I don't feel like taking a shit, I use glycerin suppositories. I don't know about your country, but in my country, you can get them in every pharmacy. Stick 2 into your butt and after 210 minutes, you'll rush to the toilet and clean your body completely from shit and fart.
3) Avoid eating cruciferous veggies (broccoli) and beans. These foods are explosive.
4) If you fear you're going to fart in the middle of the act, switch to the missionary position and avoid doggy and spooning positions. Assuming these two positions weakens your anus muscles and increases the chances that a fart will manage to escape its shitty prison.