【问题标题】:Python/Pygame - Shooting a bullet the direction the sprite is facing [duplicate]Python / Pygame - 向精灵面对的方向射击子弹[重复]
【发布时间】:2014-02-21 21:03:49
【问题描述】:

您好最近我一直在做一个平台游戏来学习 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

谢谢!

【问题讨论】:

标签: python pygame sprite 2d-games


【解决方案1】:

在您的子弹类中,您需要创建一个变量来跟踪 x 方向的速度,就像您在播放器类中所做的那样。然后在更新方法中设置 self.rect.x += xvel。然后,当您射击时,根据玩家面向的方向设置 xvel 正值或负值。

【讨论】:

  • 现在的问题是子弹朝着我的精灵所面对的方向移动。前任。如果我朝右射击子弹然后向左走,我朝右射击的子弹将改变方向并继续向左。编辑子弹类的新代码:pastebin.com/05Xvdn2P
  • 在创建子弹之前,您应该只设置一次速度。在这部分代码if shoot: bullet = Bullet() bullet.rect.x = player.rect.x bullet.rect.y = player.rect.y bullets.add(bullet) 中添加一行,根据玩家当前方向设置子弹速度
猜你喜欢
  • 2020-05-15
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
相关资源
最近更新 更多