2013-06-24 45 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)) 

或繪製與所述簾線的圓子彈實例使用哪些

如果你有問題一定要問希望這個清理乾淨的東西