2013-08-31 72 views
-1

下面是代碼,請詳細說明幫助。我需要知道如何設定射擊子彈的限制。如果你能告訴我如何用我的代碼做到這一點很好。我包括球員類,子彈類,主循環和射擊機器人。如何限制子彈射擊

# This class represents the Player   
class Player(pygame.sprite.Sprite): 
    def __init__(self): 
     # Call the parent class (Sprite) constructor 

     pygame.sprite.Sprite.__init__(self) 

     self.image = pygame.Surface([20,20]) 
     self.image.fill(red) 

     self.rect = self.image.get_rect() 


# This class represents the bullet   
class Bullet(pygame.sprite.Sprite): 

    def __init__(self): 
     # Call the parent class (Sprite) constructor 
     pygame.sprite.Sprite.__init__(self) 

     self.image = pygame.Surface([10, 4]) 
     self.image.fill(black) 

     self.rect = self.image.get_rect() 


class Shoot(pygame.sprite.Sprite): 

    def __init__(self): 
     # Call the parent class (Sprite) constructor 
     pygame.sprite.Sprite.__init__(self) 

     self.image = pygame.Surface([10, 4]) 
     self.image.fill(black) 

     self.rect = self.image.get_rect() 


# Initialize Pygame 
pygame.init() 

# Set the height and width of the screen 
screen_width=700 
screen_height=400 
screen=pygame.display.set_mode([screen_width,screen_height]) 

# This is a list of every sprite. All blocks and the player block as well. 
all_sprites_list = pygame.sprite.Group() 


# List of each bullet 
bullet_list = pygame.sprite.Group() 

shoot_list = pygame.sprite.Group() 

# Create a red player block 
player = Player() 
all_sprites_list.add(player) 

#Loop until the user clicks the close button. 
done=False 

# Used to manage how fast the screen updates 
clock=pygame.time.Clock() 

score = 0 
player.rect.y=370 


bullet_count=0 




# -------- Main Program Loop ----------- 
while done==False: 
    # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT 
    for event in pygame.event.get(): # User did something 
     if event.type == pygame.QUIT: # If user clicked close 
      done=True # Flag that we are done so we exit this loop 
     if event.type == KEYDOWN: 

      if event.key == K_LEFT: 
       bullet = Bullet() 
       bullet.rect.x = player.rect.x 
       bullet.rect.y = player.rect.y 
       all_sprites_list.add(bullet) 
       bullet_list.add(bullet) 



      if event.key == K_RIGHT: 
       shoot = Shoot() 
       shoot.rect.x = player.rect.x 
       shoot.rect.y = player.rect.y 
       all_sprites_list.add(shoot) 
       shoot_list.add(shoot)       


    # ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT 

    # ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT 

    # Calculate mechanics for each bullet 

    bulletCounter = 0 

    for bullet in bullet_list: 
     bullet.rect.x -= 5 


    for shoot in shoot_list: 
     shoot.rect.x -= -5 

     # Remove the bullet if it flies up off the screen 
     if bullet.rect.y < -10: 
      bullet_list.remove(bullet) 
      all_sprites_list.remove(bullet) 

    # Get the current mouse position. This returns the position 
    # as a list of two numbers. 
    pos = pygame.mouse.get_pos() 

    # Set the player x position to the mouse x position 
    player.rect.x=pos[0] 

    # ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT 

    # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT 

    # Clear the screen 
    screen.fill(white) 

    # Draw all the spites 
    all_sprites_list.draw(screen) 

    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT 

    # Limit to 20 frames per second 
    clock.tick(20) 

    # Go ahead and update the screen with what we've drawn. 
    pygame.display.flip() 

pygame.quit() 
+0

我想這個時候deatailed解釋 –

+0

*警告:變量' bulletCounter'已定義,但從未使用* ...... *警告:變量'bullet_count'已定義,但從未使用* ..... *警告:變量'score'已定義,但從未使用過* – scohe001

+0

Count有多少人已被解僱,然後使用這些信息來防止在該數字超過限制時再拍攝。 – martineau

回答

1

您可以創建一個槍班,它有子彈計數屬性和射擊將是類的功能。

class Gun(object): 
    def __init__(self): 
     self.bullet_count = 5 

    def __shoot(self, bullet_sprite, bullet_list): 
     if self.bullet_count > 0: 
     self.bullet_count -= 1 
     bullet_sprite.rect.x = player.rect.x 
     bullet_sprite.rect.y = player.rect.y 
     all_sprites_list.add(bullet_sprite) 
     bullet_list.add(bullet_sprite)   

    def shoot_left(self, bullet_list): 
     self.__shoot(Bullet(), bullet_list) 

    def shoot_right(self, bullet_list): 
     self.__shoot(Shoot(), bullet_list) 

------------------------------------------------------- 

然後,在你輸入你的while循環,你將創建手槍類的一個實例,按如下方式使用它:

if event.type == pygame.KEYDOWN and gun.bullet_count > 0: 
    print "KEY DOWN" 

    if event.key == K_LEFT: 
     gun.shoot_left(bullet_list) 

    if event.key == K_RIGHT: 
     gun.shoot_right(shoot_list)