I'm creating a shooting game and I don't want to allow unlimited bullets to be shot on the screen, so how can I limit the amount of bullets fired.

heres the code

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://programarcadegames.com/
# http://simpson.edu/computer-science/
import pygame, sys, random
from pygame.locals import *
from threading import Timer



# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)
blue     = (   0,   0, 255)

bullet_count = 0

# 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







# -------- 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()
  1. Put the bullets in to a variable for example b = 50
  2. Before you run the code to shoot the bullet check that b != 0
  3. When the piece of code is run -1 from this variable.
  4. You can print the number of bullets on the screen by using the following:

    myfont = pygame.font.SysFont("monospace", 17
    label = myfont.render(BulletsVar, 1, (255,255,0))

    In the main loop:
    self.screen.blit(label, (10, 35))

Hope this helps - Mat

actually i was wondering if i could just have sort of a delay between the bullets so u cant rapid fire it. Sorry if i phrased the question wrong

try this?

import time

# In a loop

# Fire Bullet
time.sleep(1)   # This will pause the fireing for 1 second just position it so it doesn't pause the whole program

So it would be in the function for firing the bullets, not in the actual main loop.

This question has already been answered. Start a new discussion instead.