Python / Pygame - 射击精灵面向精灵的方向

时间:2014-01-29 20:12:04

标签: python pygame sprite 2d-games

您好,我最近一直在开发平台游戏来学习pygame并了解更多关于python的信息。我已经能够顺利实现我所需的大部分功能,现在我正在研究子弹系统。我已经将这个教程用于子弹:http://programarcadegames.com/python_examples/show_file.php?file=bullets.py

问题是我的子弹只会向一个方向射击而我需要它们射击我的精灵所面对的方向。

到目前为止,我已经尝试在按下A键(左)或D键(右)时创建True / False值,并相应地更改添加到项目符号的值。(这只会导致子弹移动只有当按下键时才将其删除)

这是我的子弹课程:

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

这是我的玩家类:

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)

可在此处查看完整代码:http://pastebin.com/ttLJTdv4

谢谢!

1 个答案:

答案 0 :(得分:2)

在子弹类中您需要创建一个变量来跟踪x方向的速度,就像在播放器类中一样。然后在update方法中,您将设置self.rect.x + = xvel。然后在你拍摄时根据玩家面向的方向设置xvel正面或负面。