I'm making a pygame game designed so that bullets will shoot in the direction of the mouse. I'm using a Class to define the bullets in a list like this:

class Bullet:
    def __init__(self,pos,speed,size):
        self.pos = pos
        self.speed = speed
        self.size = size
    def move(self):
        self.pos[0] = int(self.pos[0] + self.speed[0])
        self.pos[1] = int(self.pos[1] + self.speed[1]) 

I'm using this trigonometry function to get the vector of the angle in which I'm going to be shooting bullets.

def getUnitVector(x1, y1, x2, y2):
        delx = x2 - x1
        dely = y2 - y1
        m = math.sqrt(delx * delx + dely * dely)
        unit = (delx / m, dely / m)
        return unit
level = [

I'm not using angles because I have to work around a pygame rounding error. these are the variables I'm plugging into the function.

mousex, mousey = pygame.mouse.get_pos()
    startx = 50
    starty = 400
aim = getUnitVector(startx, starty, mousex, mouse

This how i'm handling the aim and making the bullets shoot from the start x,y

if pygame.mouse.get_pressed()[0]:
        if reload>10:
            bx = BULLETSPEED * aim[0]
            by = BULLETSPEED * aim[1]           
            bullet = Bullet([startx,starty], [bx,by],10)
        reload = reload + 1

I just want to let you know. I'm working on a school assignment and I will be learning more in depth about vectors and trig next unit so I don't really want to spend too much time learning this stuff right now :L . Also if you know any active python forums that might be more helpful in answer this question please comment. I cant find any. Thank you for your time.

I might just build a work-around by only allowing it to shoot if the mouse is within 20 pixels or something so the error is minimized.

  • 3
    We're not going to look through a codedump and fix your code for you with no effort on your part. Please only post the relevant code, and elaborate on the problem; "not shooting straight" is not helpful at all. – Doorknob Dec 22 '13 at 1:04
  • 4
    Please do not post your whole code. Try to reduce the code to the minimum required to reproduce the problem – HugoRune Dec 22 '13 at 1:04
  • Also, the problem is with your physics calculation. Rethink your method again. – aIKid Dec 22 '13 at 1:05
  • 1
    @Trows I just saw that this actually is not your first question, apparently this is the third time you posted pretty much the same question in the last days. Don't do that. – HugoRune Dec 22 '13 at 1:24
  • 3
    I was just about to submit an answer when this question was closed. Never mind. Here is what I was going to post: pastebin.com/uAzrF41N Trows, you might have more luck asking these sorts of questions on sites for beginner game developers. I imagine there must be a lot of such sites out there. – dan-gph Dec 22 '13 at 3:15

The bullet class should have a rectangle attribute so that the bullet's direction of travel runs through the bullet's center.

Doing this would first require reading the Pygame documentation here: Pygame.Rect Docs

Centering the bullet rectangle on the direction of travel could be implemented in a movement method as such:

    def move(self):
        self.dir = self.get_direction(self.target) # get direction
        if self.dir: # if there is a direction to move

            self.trueX += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
            self.trueY += (self.dir[1] * self.speed)
            self.rect.center = (round(self.trueX),round(self.trueY)) # apply values to bullet.rect.center

More information can be found in this helpful sprite movement example:Pygame Sprite Movement

Your Answer

