# [Python + PyGame]Spawn shot at end of barrel independent of angle

This topic is 2477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello there. I've been trying to make my shots come out from the end of my player's gun independent of the angle of the player (360 degrees turn). I stumbled across a tutorial that does just this, but of course nothing is that simple. The link to the tutorial is here: link.

The code to calculate the actual position is the calculate_origin function, that I have copied here straight from the tutorial:
def calculate_origin(self): # - spawn bullet at end of turret barrel instead tank center - # cannon is around Tank.side long, calculatet from Tank center # later subtracted 20 pixel from this distance # so that bullet spawns closer to tank muzzle self.pos[0] += math.cos(degrees_to_radians(self.boss.turretAngle)) * (Tank.side-20) self.pos[1] += math.sin(degrees_to_radians(-self.boss.turretAngle)) * (Tank.side-20) 

Now my code doesn't look like that of course, my calculate_origin function looks like this:
 def calculate_origin(self): # - spawn bullet at end of turret barrel instead tank center - # cannon is around Tank.side long, calculatet from Tank center # later subtracted 20 pixel from this distance # so that bullet spawns closer to tank muzzle self.pos[0] += math.cos(degrees_to_radians(self.boss.angle_to_pointer +1))# * (104) self.pos[1] += math.sin(degrees_to_radians(-self.boss.angle_to_pointer -1))# *(104)
Obviously that did not work, so here's where you come in Here is a link to all my relevant files: link. In the file game.py there is a class called Player with a method called update, that's where all the code is for the Player. In the class Projectile there is also an update method and __init__ method with the necessary code. The calculate_origin function can be found in the Projectile class.

If you have any questions, just ask.
Thanks!