2013-06-24 56 views
0

我想在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

我们在这里需要更多的代码。

在伪代码:

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

凡playerDirectionVector是在播放器正面对的方向的归一化矢量。

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 

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

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

或绘制与所述帘线的圆子弹实例使用哪些

如果你有问题一定要问希望这个清理干净的东西