Sign in to follow this  
Jonte135

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

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: [url=http://thepythongamebook.com/en:part2:pygame:step020]link[/url].

The code to calculate the actual position is the calculate_origin function, that I have copied here straight from the tutorial:
[code]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)
[/code]

Now my code doesn't look like that of course, my calculate_origin function looks like this:
[code] 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)[/code]
Obviously that did not work, so here's where you come in :) Here is a link to all my relevant files: [url=http://www.megaupload.com/?d=XBWM5NU6]link[/url]. 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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this