How to Prevent Multiple Bullet Firing Simultaneously
In the original code, multiple bullets were being fired at once due to the fact that the pygame.key.get_pressed() function returns True for as long as a key is pressed down. This behavior is suitable for controlling player movement but not for firing bullets, where you want only one bullet to be fired each time a key is pressed.
To fix this issue, we can take advantage of the pygame.KEYDOWN event, which occurs only once when a key is initially pressed. By using this event instead of pygame.key.get_pressed(), we can ensure that only one bullet is fired when the spacebar is pressed.
Additionally, we can implement a simple cooldown mechanism to prevent rapid-fire bullet shooting. This can be done by introducing a variable called next_bullet_threshold, which represents the earliest time that the next bullet can be fired. Each time a bullet is fired, we update next_bullet_threshold to the current time plus the desired cooldown period. By checking if the current time is greater than next_bullet_threshold, we can prevent any bullets from being fired outside of the desired cooldown period.
Here is the modified code with the implemented cooldown mechanism:
<code class="python">import pygame pygame.init() red = 255,0,0 blue = 0,0,255 black = 0,0,0 screenWidth = 800 screenHeight = 600 gameDisplay = pygame.display.set_mode((screenWidth,screenHeight)) ## screen width and height pygame.display.set_caption('JUST SOME BLOCKS') ## set my title of the window clock = pygame.time.Clock() class player(): ## has all of my attributes for player 1 def __init__(self,x,y,width,height): self.x = x self.y = y self.height = height self.width = width self.vel = 5 self.left = False self.right = False self.up = False self.down = False class projectile(): ## projectile attributes def __init__(self,x,y,radius,colour,facing): self.x = x self.y = y self.radius = radius self.facing = facing self.colour = colour self.vel = 8 * facing # speed of bullet * the direction (-1 or 1) def draw(self,gameDisplay): pygame.draw.circle(gameDisplay, self.colour , (self.x,self.y),self.radius) ## put a 1 after that to make it so the circle is just an outline def redrawGameWindow(): for bullet in bullets: ## draw bullets bullet.draw(gameDisplay) pygame.display.update() #mainloop player1 = player(300,410,50,70) # moves the stuff from the class (when variables are user use player1.var) bullets = [] run = True next_bullet_threshold = 0 # Initialize the cooldown threshold bullet_cooldown = 500 # Set the desired cooldown period in milliseconds while run == True: clock.tick(27) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False for bullet in bullets: if bullet.x < screenWidth and bullet.x > 0 and bullet.y < screenHeight and bullet.y > 0: ## makes sure bullet does not go off screen bullet.x += bullet.vel else: bullets.pop(bullets.index(bullet)) keys = pygame.key.get_pressed() ## check if a key has been pressed ## red player movement if keys[pygame.K_w] and player1.y > player1.vel: ## check if that key has been pressed down (this will check for w) and checks for boundry player1.y -= player1.vel ## move the shape in a direction player1.up = True player1.down = False if keys[pygame.K_a] and player1.x > player1.vel: ### this is for a player1.x -= player1.vel player1.left = True player1.right = False if keys[pygame.K_s] and player1.y < screenHeight - player1.height - player1.vel: ## this is for s player1.y += player1.vel player1.down = True player1.up = False if keys[pygame.K_d] and player1.x < screenWidth - player1.width - player1.vel: ## this is for d player1.x += player1.vel player1.right = True player1.left = False if keys[pygame.K_SPACE]: # shooting with the space bar current_time = pygame.time.get_ticks() if current_time >= next_bullet_threshold: # Check the cooldown timer if player1.left == True: ## handles the direction of the bullet facing = -1 else: facing = 1 if len(bullets) < 5: ## max amounts of bullets on screen bullets.append(projectile(player1.x + player1.width //2 ,player1.y + player1.height//2,6,black,facing)) ##just like calling upon a function next_bullet_threshold = current_time + bullet_cooldown # Update cooldown timer ## level gameDisplay.fill((0,255,0)) ### will stop the shape from spreading around and will have a background pygame.draw.rect(gameDisplay,(red),(player1.x,player1.y,player1.width,player1.height)) ## draw player pygame.display.update() redrawGameWindow() pygame.quit()</code>
The above is the detailed content of How to Implement a Cooldown Mechanism to Prevent Excessive Bullet Firing?. For more information, please follow other related articles on the PHP Chinese website!