Question

Hi recently I have been working on a platform game to learn pygame and learn more about python in general. I have been able to implement most of my desired features smoothly and I am now currently working on the bullet system. I have used this tutorial for the bullets: http://programarcadegames.com/python_examples/show_file.php?file=bullets.py

The problem is that my bullets will only shoot one direction and I need them to shoot the direction my play sprite is facing.

So far I have tried to create True/False values for when the A key(left) or the D key(right) is pressed and change the value being added to the bullet accordingly.(This only resulted in the bullets moving only when the key was pressed so I removed them)

Here is my bullet class:

class Bullet(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("C:/Users/Tom/Data/Art/bullet.png")
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.x += 10

Here is my player class:

class Player(Entity):

    walking_frames_l = []
    walking_frames_r = []

    jumping_r = []
    jumping_l = []

    run_frames_r = []
    run_frames_l = []

    def __init__(self, x, y):
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player1.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player2.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player22.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player3.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player33.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player4.png")
            self.walking_frames_r.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player44.png")
            self.walking_frames_r.append(image)

            image = pygame.image.load("C:\Users\Tom\Data\Art\Player1.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\Player2.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\player22.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\player3.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\player33.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\player4.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)
            image = pygame.image.load("C:\Users\Tom\Data\Art\player44.png")
            image = pygame.transform.flip(image,True,False)
            self.walking_frames_l.append(image)

            self.frame_r = 0
            self.frame_l = 0

            self.last_key_pressed = None

            Entity.__init__(self)
            self.xvel = 0
            self.yvel = 0
            self.onGround = False
            self.image = self.walking_frames_r[self.frame_r]
            self.rect = Rect(x,y,23,31)



    def update(self,shoot,up, down, left, right,select, pickups, platforms, inventory, player,bullets):

            if shoot:
                    bullet = Bullet()
                    bullet.rect.x = player.rect.x
                    bullet.rect.y = player.rect.y
                    bullets.add(bullet)
            if right:
                    print "right"
            if left:
                    print "left"

            if up:
                    # only jump if on the ground
                    if self.onGround: self.yvel -= 7
            if down:
                    pass
            if left:

                    self.last_key_pressed = "LEFT"
                    self.xvel = -4
                    self.frame_l += 1
                    self.image = self.walking_frames_l[self.frame_l]
                    if self.frame_l == 8: self.frame_l = 0
            if right:

                    self.last_key_pressed = "RIGHT"
                    self.xvel = 4
                    self.frame_r += 1
                    self.image = self.walking_frames_r[self.frame_r]
                    if self.frame_r == 8: self.frame_r = 0

            if not (left or right):
                    if self.last_key_pressed == "LEFT":
                            self.image = self.walking_frames_l[1]

                    if self.last_key_pressed == "RIGHT":
                            self.image = self.walking_frames_r[1]


            if not self.onGround:
                    # only accelerate with gravity if in the air
                    self.yvel += 0.3
                    # max falling speed
                    if self.yvel > 30: self.yvel = 30
            if not(left or right):
                    self.xvel = 0
            # increment in x direction
            self.rect.left += self.xvel
            # do x-axis collisions
            self.collide(self.xvel, 0,pickups, platforms,select,inventory)
            # increment in y direction
            self.rect.top += self.yvel
            # assuming we're in the air
            self.onGround = False;
            # do y-axis collisions
            self.collide(0, self.yvel, pickups, platforms,select,inventory)

The complete code can be viewed here: http://pastebin.com/ttLJTdv4

Thanks!

Was it helpful?

Solution

In your bullet class You need to create a variable to keep track of the velocity in the x direction like you did in the player class. Then in the update method you will set the self.rect.x += xvel. Then when you shoot set the xvel positive or negative based on which direction the player is facing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top