We can do great things with point slope form here.
m = (y1-y0)/(x1-x0)
m(x1-x0) = y1-y0
y1 = m(x1-x0)-y0
y = m(x-x0)-y0
If a line is perpendicular to y=mx+b, then the slope is -1/m
The original line is
y = slope1(x - gun.xpos) - gun.ypos
slope2 = -1/slope1
The perpendicular line is
y = slope2(x - target.xpos) - target.ypos
Setting these two equal we get
slope1(x - gun.xpos) - gun.ypos = slope2(x - target.xpos) - target.ypos
slope1*x -slope2*x = slope1 * gun.xpos - slope2* target.xpos + target.ypos -target.ypos
x = (that mess above)/(slope1 - slope2)