No prevention, but some inhibition.
Gerrymandering relies on clever mappings to maximize the number of voting districts with small but statistically significant majorities for your own party (which as a corollary gives to other parties preferentially districts with huge majorities from them). It's only important for non-proportional representative voting systems though, like for example in the US or UK. Indeed gerrymandered voting maps tend to be highly non-compact, sometimes not even connected voting districts while voting district maps drawn by neutral commissions tend to be quite compact.
Restricting the freedom to choose arbitrarily shaped district maps will restrict the ability to maximize gerrymandering. One can expect that with your compactness requirement the maximal possible amount of gerrymandering cannot be achieved.
However, since voters of the same political orientations seem to be concentrated geographically (city, countryside, ...) even compact voting district maps can be gerrymandered a lot. Therefore it doesn't prevent it.
Suppose there is a state with only three voting districts and two regions of voters (one leaning for one party and another leaning for another party). You can surely cut the regions so that either party gets two out of the three voting districts but still have fairly compact regions. Therefore compactness is not sufficient to ensure fairness of elections in non-proportional voting systems.
By the way, Wikipedia lists a few more options to achieve more competitive voting maps without much human intervention: Minimum district to convex polygon ratio, Shortest splitline algorithm, Minimum isoperimetric quotient or Efficiency gap calculation. Or simply change the voting system to something that is proportional.