【问题标题】:Shooting in Pygame在 Pygame 中射击
【发布时间】:2013-06-24 19:42:56
【问题描述】:

我正在尝试在 pygame 中制作射击游戏。我可以让玩家四处移动,当按下空间时,子弹会回到它的位置。我想知道如何让它远离玩家,直到它碰到屏幕边缘。这是我目前所拥有的:

if AMMO > 0:
    if event.type == pygame.KEYDOWN and Gun.image == NOGUN:
        if event.key == pygame.K_SPACE and Gun.image == NOGUN:
            Bullet.rect.center = Player.rect.center
            if Player.direction == 0:
                Bullet.direction = 0 #WHERE THE BULLET WILL MOVE

            shot.play()
            print "BANG"
            AMMO = AMMO - 1
            time.sleep(0.09)

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    我们需要更多代码。

    在伪代码中:

    def frameUpdate( timeBetweenFrame, bulletSpeed, playerDirectionVector ):
           bullet.position = bullet.position + playerDirectionVector.MultiplyByScalar(bulletSpeed * timeBetweenFrame);
    

    其中 playerDirectionVector 是玩家面向的方向上的归一化向量。

    【讨论】:

      【解决方案2】:

      试试这个

      if AMMO > 0:
      if event.type == pygame.KEYDOWN and Gun.image == NOGUN:
          if event.key == pygame.K_SPACE and Gun.image == NOGUN:
              #Initialize Bullet so it's not null and do the rest
              Bullet.rect.center = Player.rect.center
              if Player.direction == 0:
                  Bullet.direction = 0
      
       if Bullet != null
          Bullet.X += 10 
          if Bullet.X > ScreenWidth()
              Bullet = null 
      #These are all examples so you can understand the logic, I don't remember exactly how pygame works, but since you can move a character around you can find this :P
      

      请记住,此代码只允许一个项目符号!

      【讨论】:

        【解决方案3】:

        我在伪代码中创建项目符号实例时使用什么

        #making your starting bullet cords
        Bulletx = playerx
        Bullety = playery
        #this stores the angle the bullet was   shot from
        Bulletangle = degreesplayerisrotated
        

        这会将角度转换为弧度 Bulletangle = Bulletangle×pi/180

        #this line updates the cords speed is a set value of how fast you want the bullet to move
        Bulletx = bulletx-cos(bulletangle)×speed
        Bullety = bullety-sin (bulletangle)×speed
        
        Screen.blit (bullet, (bulletx, bullety))
        

        或者用绳子画一个圆圈

        如果您有任何问题,请务必提出希望这能解决问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-03
          • 2020-05-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多