在Pygame拍摄

时间:2013-06-24 19:42:57

标签: python pygame

我正在尝试用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)

3 个答案:

答案 0 :(得分:3)

我们需要更多代码。

在伪代码中:

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

其中playerDirectionVector是玩家所面向的标准化向量。

答案 1 :(得分:0)

试试这个

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

请记住,此代码只允许一个子弹!

答案 2 :(得分:0)

我在psudocode中制作子弹实例时使用的内容

#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))

或用绳子画一个圆圈

如果你有疑问,一定要问希望这可以解决问题